Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend \pgfplotsforeachungrouped to accept a macro as <list> #462

Open
muzimuzhi opened this issue Jun 6, 2023 · 0 comments
Open

Extend \pgfplotsforeachungrouped to accept a macro as <list> #462

muzimuzhi opened this issue Jun 6, 2023 · 0 comments

Comments

@muzimuzhi
Copy link
Member

From https://tex.stackexchange.com/q/687866

\foreach supports both

\foreach \x in {1,2,3,0} {<loop body>}
% and
\def\mylist{1,2,3,0}
\foreach \x in \mylist {<loop body>}

but the simpler \pgfplotsforeachungrouped doesn't support the second syntax.

Adding support for this seems not expensive, for example

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\makeatletter
\long\def\pgfplotsforeachungrouped@#1/#2in{%
    \pgfutil@ifnextchar\bgroup{%
        \pgfplotsforeachungrouped@@{#1}{#2}%
    }{%
%       \pgfplots@error{Found unexpected characters: expected 'in '.}%
    \pgfplotsforeachungrouped@@@{#1}{#2}%
    }%
}

\long\def\pgfplotsforeachungrouped@@@#1#2#3{%
  \pgfkeys@expanded{\pgfutil@unexpanded{\pgfplotsforeachungrouped@@{#1}{#2}}{\pgfutil@unexpanded\expandafter{#3}}}%
}
\makeatother

\begin{document}
\setlength\parindent{0pt}
\newcommand{\xlabelsol}{0/0,1/1,2/2}

\foreach  \x/\y in \xlabelsol { (\x)(\y) \\ }
\pgfplotsforeachungrouped \x/\y in \xlabelsol { (\x)(\y) \\ }
\end{document}

Output is two copies of

(0)(0)
(1)(1)
(2)(2)

Use of \pgfkeys@expanded and \pgfutil@unexpanded (corresponding to primitives \expanded and \unexpanded) can be replaced with argument swapping and normal \expandafter tricks, at the cost of less readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant