I'm using pgfplotstable to create standalone graphs of test data. I've included pgffor and I'm trying to use a \foreach loop to iterate over the three output channels for a given unit under test within a single groupplot. Lucky for me, I'm getting compilation errors that refer to the index variable (called \chan), and which point to lines outside the loop, where \chan isn't used.
Here's a stripped-down version of my file that's causing trouble:
\documentclass[tikz]{standalone}
\usepackage{pgfplotstable}
\usepackage{pgffor}
\usepackage{siunitx}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\newcommand{\voltticks}{-10,0,...,30}
\newcommand{\voltmin}{-18}
\newcommand{\voltmax}{35}
\newcommand{\currmin}{-0.5}
\newcommand{\currmax}{8}
% Different color for each channel on each unit
\newcommand{\uutchan}[2]{%
\ifcase #1 error% There's no uut #0
\or \ifcase #2 colorerror% nor any channel #0
\or black%
\or brown%
\or red%
\fi%
\or \ifcase #2 colorerror%
\or orange%
\or yellow%
\or green%
\fi%
\or \ifcase #2 colorerror%
\or blue%
\or purple%
\or grey%
\fi%
\fi}
\pgfplotsset{every axis/.append style={
xmin=0,
xmax=5,
xtick={1,2,...,5},
}}
\newcommand{\uut}{1}
% All the data files have the same filename structure
\newcommand{\lvdata}[1]{plot-data/lv-data-uut-\uut-ch-#1.csv}
\begin{document}
\begin{tikzpicture}[>=stealth]
\begin{groupplot}[
group style={
group size=1 by 2,
},
no markers,
width=6in,
]
\nextgroupplot[
ymin=\voltmin,
ymax=\voltmax,
ytick=\voltticks,
ylabel={Voltage (\si{\volt})},
xticklabels=\empty,
title={Boy, I wish this would work right},
height=1.75in,
]
\foreach \chan in {1,2,3}{%
\addplot[\uutchan{\uut}{\chan}] table [%
col sep=comma,%
x={elapsed_time},%
y={voltage},%
] {\lvdata{\chan}};%
\addlegendentry{Channel \chan}%
} % <<-- end of the \foreach loop
\nextgroupplot[
ymin=\currmin,
ymax=\currmax,
xlabel={Elapsed Time},
ylabel={Current (\si{\ampere})},
height=1.75in,
] % <<-- here's line 79
\addplot[\uutchan{\uut}{1}] table [
col sep=comma,
x={elapsed_time},
y={current},
] {\lvdata{1}};
\addlegendentry{Channel 1}
\addplot[\uutchan{\uut}{2}] table [
col sep=comma,
x={elapsed_time},
y={current},
] {\lvdata{2}};
\addlegendentry{Channel 2}
\addplot[\uutchan{\uut}{3}] table [
col sep=comma,
x={elapsed_time},
y={current},
] {\lvdata{3}};
\addlegendentry{Channel 3}
\end{groupplot}
\end{tikzpicture}
\end{document}
Here's the error message I get:
(/usr/local/texlive/2021/texmf-dist/tex/latex/translations/translations-basic-d
ictionary-english.trsl)
! Undefined control sequence.
<argument> \chan
l.79 ]
I don't understand why it looks like \chan is being called several lines after the end of the loop.
I know that run-on commands sometimes show up a little later in the file, but I've double-checked my brace count to make sure I didn't forget to close the code block, and it's correct.
Also worth noting is that if I don't use the \foreach loop, there are no problems. The way the second groupplot is written shows what that would look like.
What am I doing wrong?
Edit: Updated with a M(non-)WE instead of a code snippet.
Contents of plot-data/lv-data-uut-1-ch-1.csv:
elapsed_time,voltage,current
1,5.12088502740541,1.18375845641884
2,5.81177332147156,1.6640415277584
3,6.3936508576718,2.47855204713269
4,5.77677606311296,1.88676641772499
5,6.08716354605412,2.68562189308305
Contents of plot-data/lv-data-uut-1-ch-2.csv:
elapsed_time,voltage,current
1,14.7367967957342,3.58490117433496
2,14.2603519819053,3.15772195035904
3,15.219470298334,2.60505323186182
4,15.8054999682348,4.0885748772842
5,14.602837758358,3.92571012858714
Contents of plot-data/lv-data-uut-1-ch-3.csv:
elapsed_time,voltage,current
1,-4.68980292044475,6.07093702142046
2,-5.52369547515492,6.60095794734899
3,-3.14599831983585,6.49047991092652
4,-5.57765267954762,6.86674835669737
5,-4.3030082035212,5.32605451112347
\foreachwith\pgfplotsinvokeforeachmade it work the way I expected. I don't know whether I should delete my question or answer it, or if it should be closed as a duplicate. – Gern Blanston Mar 03 '22 at 19:34