This question extends the solution from Generate rows in table using \multido (or something similar). Based on the solution to the linked question, I understand how to use a \foreach
loop to build rows for a tabular table. My question is how can I include a horizontal line (border) between each row? In a static tabular I would include \hline
but that doesn't seem to work when using the \foreach
loop.
Edit 1 ---
MWE below.
\documentclass{article}
\usepackage{pgffor}
\makeatletter
\newcommand{\Buildtable}{%
\def\tableData{}% empty table
\foreach \n in {1,...,5}{%
\protected@xdef\tableData{\tableData \n & A\n & B\n & C\n \\} % < adding \hline here causes error
}
\begin{tabular}{|l|l|l|l|}
\hline
\textbf{Num} & \textbf{Col A} & \textbf{Col B} & \textbf{Col C} \\ \hline
\tableData \hline
\end{tabular}
}
\makeatother
\begin{document}
\Buildtable
\end{document}
Which results in
I'm looking to achieve the equivalent of "all borders" in Excel (shown below).
Adding \hline
within the braces, but after \\
, on the indicated line results in the error
Missing control sequence inserted.
<inserted text>
\inaccessible
l.19 \Buildtable
I do not understand why adding this causes an error.
\foreach
you mean the loop from pgf that adds a tex group around each iteration so is usually unsuitable for generating table rows. you may have worked around that but then whether or not\hline
works depends on code you have not shown.