# Problems using \addlegendentry in a loop with pgfplots

I have some spectra to plot, which I exported from the Siemens Xray toolbox, which works nicely with pgfplots.

Since these are multiple files that look the same I think I can do it with a for loop, which also works.

But now I have problems with adding a legend entry, since that does not work. When I use a \addlegendentry with siunitx I cannot compile the copde below, I get informed that the

Argument of \add@accent has an extra }.

This becomes more clear when I don't use siunitx but just want to get the values (commented line in the example below), then there's no number in the legend, only a nice k with a hat (no idea how it's called :)).

When I don't do use a for-loop, but add the plots manually for each one it works. But since in the final figure I have to plot more than 20 spectra (all much bigger than the one in the example) I'd like to do it in a loop, ideally.

\documentclass{article}
\usepackage{siunitx}
\usepackage{pgfplots}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

\usepackage{filecontents}
\begin{filecontents*}{Xray-Spectrum_040.txt}
9 2.23512e-015
10 2.18948e-009
\end{filecontents*}
\begin{filecontents*}{Xray-Spectrum_046.txt}
9 1.98718e-015
10 1.56485e-009
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\foreach \v in {40,46} {
}
\end{axis}
\end{tikzpicture}
\end{document}


## Related

I had to rename the files from Xray-Spectrum_046kV.txt to Xray-Spectrum_046.txt to make it work. Is there a way I can get the variable to be evaluated in the middle of the filename?

The following code does the job. For the problem relative to the macro expansion inside \addlegendentry see this answer: PGFplots foreach equivalent to TikZ's with multiple variables separated by a slash. For the macro in the file name you can simply leave a white space.

\documentclass{article}
\usepackage{siunitx}
\usepackage{pgfplots}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\usepackage{filecontents}

\begin{document}

\begin{filecontents*}{Xray-Spectrum_040kv.txt}
9 2.23512e-015
10 2.18948e-009
\end{filecontents*}

\begin{filecontents*}{Xray-Spectrum_046kv.txt}
9 1.98718e-015
10 1.56485e-009
\end{filecontents*}

\begin{tikzpicture}
\begin{axis}
\foreach \v in {40,46} {
\temp
}
\end{axis}
\end{tikzpicture}
\end{document}

• I happen to have stumbled upon this very issue. Thanks! Nov 16, 2020 at 22:14

For very strange reasons, a \foreach statement inside axis seems to want as argument only already defined macros. You're using \v that's an accent macro and this “explains” the strange error message.

Unfortunately, it appears that \SI doesn't behave well inside \addlegendentry and so some trick as the one mentioned by Red has to be used.

I suggest to define your macro for these cases, so not to incur, at least, in puzzling error messages. Then you can abstract the method defining an \addlegendentrySI command:

\documentclass{article}
\usepackage{siunitx}
\usepackage{pgfplots}
\usepackage{xcolor}
\pgfplotsset{compat=1.8}
\usepackage{filecontents}

\newcommand\xyz{} % in order to use a different macro
\begingroup\edef\x{\endgroup
}

\begin{document}

\begin{filecontents*}{Xray-Spectrum_040.txt}
9 2.23512e-015
10 2.18948e-009
\end{filecontents*}

\begin{filecontents*}{Xray-Spectrum_046.txt}
9 1.98718e-015
10 1.56485e-009
\end{filecontents*}

\begin{tikzpicture}
\begin{axis}
\foreach \xyz in {40,46} {

This is surely handier than writing several \edef\temo{...} lines. I added also the color=red option just to show that an optional argument can be used.
• Since I only use one loop, writing \edef... once is not too much of a hassle, but redefining it to a command is also a nice idea. I'll use this when I have to use this workaround multiple times in the same plot :)