I'm constructing a table one line at time inside a \pgfplotsforeachungrouped. An unwanted line appears after the \hline at the end of the last round of the loop as evidenced by the lone decimal point below the left column in the output. This doesn't make sense right away, surely it's the end of the last step of the loop?

I've untangled this from another question where it was a side issue that I solved on my own, so I've answered this myself in case it ends up being useful for anyone else.

Note: Please see the addendum below for clarification of why I've put this question up.

MWE Output

MWE Output

MWE Code




\pgfplotsset{compat=1.9, /pgf/declare function={F(\x)=(\x+5)*(\x-1)*(\x-5);}}





\par $f(x)=(x+1)(x-1)(x+5)$
\par\begin{tabular}{r@{.}l r@{.}l}
\multicolumn{2}{l}{\textbf{\em x}} & \multicolumn{2}{l}{\textbf{\em f}(\textbf{\em x})} \\
\pgfplotsforeachungrouped \kk in {1,...,5}
\leftdigits{\xval} & \rightdigits{\xval} &
\leftdigits{\fval} & \rightdigits{\fval} \\

Addendum: I really appreciate the answer below that demonstrates a better way of doing tables but that's not what the question asks for. I've been getting comments left and right telling me not to complicate questions and to try and focus them and ask side issues in separate questions. So I'm doing just that. This question was about why the extra line was appearing in my output and I answered it successfully.

I do want to know about better ways of doing tables and I will search this site for answers. If I don't find what I'm looking for I will ask a separate question specifically about recommended ways to do tables.


I can understand the wish for using as less packages as possible; but when you load pgfplots you're already loading a huge amount of code. So it's not clear why relying on self-made methods for printing a table, when packages that already do it better are available.


\pgfplotsset{compat=1.9, /pgf/declare function={F(\x)=(\x+5)*(\x-1)*(\x-5);}}



% Build the table body outside the table
\foreach \kk in {1,...,5}{
{\boldmath$x$} & {\boldmath$f(x)$} \\


enter image description here


The loop may be finished but the table is not. Right after the final \hline the table starts the next line. This is avoided by placing the \end{tabular} immediately after the \hline at the end of the loop body as follows.


Solution Output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.