0

I am trying to build upon the answer of Christian Feuersänger here: https://tex.stackexchange.com/a/17817/193625

Instead of just on \addplot I want to have multiple in the body of my loop. However this results in the error: Use of \pgffor@scanround doesn't match its definition.

Can anyone help?

MWE:

\documentclass{article}

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

\begin{document} \begin{tikzpicture} \begin{axis} \foreach \x/\l in {-2/a, -1/b, 1/c, 2/d}{ \edef\temp{\noexpand \addplot% coordinates{ (\x, 0.5) (\x, 1) } node [above] {\l}; % comment the next three lines to make it work \addplot% coordinates{ (\x, -0.5) (\x, -1) } node [below] {\l}; } \temp } \end{axis} \end{tikzpicture} \end{document}

Franz
  • 175

1 Answers1

3

You need to place \noexpand before every \addplot macro:

\documentclass{article}

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

\begin{document} \begin{tikzpicture} \begin{axis} \foreach \x/\l in {-2/a, -1/b, 1/c, 2/d}{ \edef\temp{ \noexpand\addplot% coordinates{ (\x, 0.5) (\x, 1) } node [above] {\l}; \noexpand\addplot% coordinates{ (\x, -0.5) (\x, -1) } node [below] {\l}; } \temp } \end{axis} \end{tikzpicture} \end{document}


You can also nest \foreach loops (this may further simplify things, depending on your concrete set up):

\documentclass{article}

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

\begin{document} \begin{tikzpicture} \begin{axis} \foreach \x/\l in {-2/a, -1/b, 1/c, 2/d}{ \foreach \p/\ya/\yb in {above/0.5/1, below/-0.5/-1}{ \edef\temp{ \noexpand\addplot% coordinates{ (\x, \ya) (\x, \yb) } node [\p] {\l}; }\temp } } \end{axis} \end{tikzpicture} \end{document}

Both yield:

enter image description here

  • Ty. So simple. It is just that I never really understand how latex works and I cannot make sense of most error messages. – Franz Nov 30 '22 at 19:04