# Missing \endcsname inserted in table contents defined within a foreach loop

! Missing \endcsname inserted. \countuno l.67 }

From what I've seen about this error the fact that it is a tex error makes it very difficult to pinpoint its origins. Hopefully this is not the case here but I've personally been unable to find the issue with this document. I have a similar document differing only slightly in that the tabular environment is enclosed by a subtable and the control structures take a different shape. I have a feeling that it might have something to do with the counters used to do some calculations, but I haven't seen anything off with them.

In my document I loop through a master data file and typeset it into a tabular environment by looping through the data file, calculating row placements with counter arithmetic, and bundling all the contents in a \tablecontents macro to be delivered to the tabular environment and expanded.

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage{filecontents}
\usepackage{indentfirst}
\usepackage{amsmath}
\usepackage{float}
\usepackage{hhline}
\usepackage{booktabs}
\usepackage[strict]{changepage}
\usepackage[position=top]{subfig}

\renewcommand\thesection{}
\makeatletter
\renewcommand\thesubsection{\@arabic\c@subsection}
\makeatother

\begin{filecontents}{data.dat}
28.1 0.7 0.9 1.2 1.4 1.5
40.0 11.2 11.7 12.0 12.2 12.9
44.7 18.2 19.3 19.7 19.8 20.4
50.9 28.0 28.9 29.4 29.6 30.1
57.0 43.2 42.2 41.6 40.7 40.4
61.5 50.2 51.0 52.1 52.6 52.7
67.0 62.8 64.5 65.5 65.6 68.5
72.2 78.3 79.2 80.7 80.8 82.7
\end{filecontents}

\pgfplotsset{compat=1.14}

\begin{document}
\newcount\countuno
\newcount\countdos

\begin{table}[H]
\begin{tabular}{lll}
\toprule
$D$ & $h$ & $h/D$\\
\midrule
\gdef\tablecontents{}%
\foreach\row in {0,...,34}
{%
\countdos = \row
\countuno = \row
\divide\countuno by 5
\divide\countdos by 5
\multiply\countuno by -5
\pgfplotstablegetelem{\countdos}{0}\of\Data\pgfmathsetmacro\elemtable{\pgfplotsretval}%
\ifnum\countuno=1
\xdef\tablecontents{%
\unexpanded\expandafter{\tablecontents}%
\noexpand\pgfmathprintnumber{\elemtable}%
}%
\fi
\pgfplotstablegetelem{\countdos}{\countuno}\of\Data\pgfmathsetmacro\elemtabledos{\pgfplotsretval}%
\pgfmathsetmacro\elemtabletres{\elemtabledos / \elemtable}
\xdef\tablecontents{%
\unexpanded\expandafter{\tablecontents}%
& \noexpand\pgfmathprintnumber{\elemtabledos} & \noexpand\pgfmathprintnumber{\elemtabletres}\noexpand\\
}%
\ifnum\countuno=5 \xdef\tablecontents{%
\unexpanded\expandafter{\tablecontents}%
\noexpand\midrule\noexpand\\
}%
\fi
}
\tablecontents
\bottomrule
\end{tabular}
\end{table}

\end{document}


EDIT: I've done some messing around, and I created a similar chunk of code to take the place of the table in the above and the error is still present, so it is not coming from the tabular environment. When I comment out all references to pointers the error goes away. When scrolling through future error messages the next error---occurring on the same line---is "! Missing number, treated as zero." and the next---also on the same line---is "! Extra \endcsname."

I also tried placing the code \countuno 1 \foreach\row in {0,...,5}{\multiply\countuno\row} \the\countuno before the table and it does not trigger an error so I'm not sure it is the counter arithmetic that is causing the error.

• @cfr The code is simply meant to place items from the leftmost column of the data file into one column of the table and the five items in the same row of the datafile all in the second column such that the first column's items are paired with five items each in the second column. Also third column in same format as second. the macro I used is \tablecontents not \tableofcontents. I will post the full error message but it contains little information, line number is 67. I believe that one of the reasons the full tex file is included is so that you can run it yourself and see what happens. – Bailey Bussiere Feb 26 '18 at 2:36
• Your syntax is wrong. You are using indices for the columns, so you need e.g. \pgfplotstablegetelem{\countdos}{[index]\countuno}\of\Data. See p. 61 of the manual. – cfr Feb 28 '18 at 1:29
• @cfr It appears to be working. Thank you very much. Feel free to post an answer. – Bailey Bussiere Feb 28 '18 at 1:52
• I don't think I can produce a really working solution, but I've posted something which is probably a bit closer to what you want. At least it compiles without error and I tweaked some other things, although this was partly based on guesswork about what you want. However, I can't get the first value into the left column and I don't know why. – cfr Feb 28 '18 at 2:10
• I wonder why the first \pgfplotstablegetelem accepted the 0, even though you'd think it would want [index]0, while the second wouldn't take the index and produced no useful error. – cfr Feb 28 '18 at 2:12

This is presumably a bit closer to what you want. The key change is to use the syntax from p. 61 of the manual, which specifies that [index] must precede a column specification which uses an index rather than a name.

Also, \countuno was never equal to 5 in the original code, so that part of the conditional was never executed. I'm guessing that you want 6 rather than 5 in the earlier definitions, but I'm not certain what the intention is exactly, so I might be mistaken about this.

The reason for the weird placement of the first value is just the effect of all the spaces accumulating in the first cell, which also makes it very wide.

\begin{filecontents}{\jobname.dat}
28.1 0.7 0.9 1.2 1.4 1.5
40.0 11.2 11.7 12.0 12.2 12.9
44.7 18.2 19.3 19.7 19.8 20.4
50.9 28.0 28.9 29.4 29.6 30.1
57.0 43.2 42.2 41.6 40.7 40.4
61.5 50.2 51.0 52.1 52.6 52.7
67.0 62.8 64.5 65.5 65.6 68.5
72.2 78.3 79.2 80.7 80.8 82.7
\end{filecontents}
\documentclass{article}
\usepackage{pgfplots,pgfplotstable}
\usepackage{booktabs}

\pgfplotsset{compat=1.15}

\begin{document}
\newcount\countuno
\newcount\countdos
\def\tablecontents{}%
\begin{tabular}{lll}
\toprule
$D$ & $h$ & $h/D$\\
\midrule
\foreach\rowx [count=\rh] in {0,...,47}
{%
\countdos = \rowx
\countuno = \rowx
\divide\countuno by 6
\divide\countdos by 6
\multiply\countuno by -6
\pgfplotstablegetelem{\countdos}{[index]0}\of\Data
\pgfmathsetmacro\elemtable{\pgfplotsretval}%
\ifnum\countuno=0
\ifnum\rh=1\relax\else
\xdef\tablecontents{%
\unexpanded\expandafter{\tablecontents}%
\noexpand\midrule
}%
\fi
\xdef\tablecontents{%
\unexpanded\expandafter{\tablecontents}%
\noexpand\pgfmathprintnumber{\elemtable}%
}%
\else
\pgfplotstablegetelem{\countdos}{[index]\countuno}\of\Data
\pgfmathsetmacro\elemtabledos{\pgfplotsretval}%
\pgfmathsetmacro\elemtabletres{\elemtabledos / \elemtable}%
\xdef\tablecontents{%
\unexpanded\expandafter{\tablecontents}%
& \noexpand\pgfmathprintnumber{\elemtabledos} & \noexpand\pgfmathprintnumber{\elemtabletres}\noexpand\\%
}%
\ifnum\countuno=5 \xdef\tablecontents{%
\unexpanded\expandafter{\tablecontents}%
}%
\fi
\fi
}%
\tablecontents
\bottomrule
\end{tabular}

\end{document}


• yeah I had really just thrown some stuff together to see if the principal worked had some tweaking to do after I got rid of that error. I'm having the same issue with the first value being in a weird position. Wonder what the cause is? – Bailey Bussiere Feb 28 '18 at 3:55
• @BaileyBussiere Got it. The answer is spurious spaces. That's also why the table is weirdly wide. All those spurious spaces accumulate in the first cell. The first number is then added to that cell, with the effect that it is shifted to the right (and very wide). – cfr Feb 28 '18 at 17:40
• @BaileyBussiere Please see edit above. I think this looks better (though it may not be quite what you want, of course) in that it solves at least the most obvious problems. – cfr Feb 28 '18 at 17:49