6

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

\documentclass[12pt]{article}
\usepackage[a5paper,margin=14mm]{geometry}

\usepackage{pgfplots}

\newlength{\xval}
\newlength{\fval}

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

\def\MyEnd{m}

\def\printplainbefore#1.#2\MyEnd{#1}
\def\printplainafter#1.#2\MyEnd{#2}

\newcommand{\leftdigits}[1]{\pgfmathparse{#1}\expandafter\printplainbefore\pgfmathresult\MyEnd}
\newcommand{\rightdigits}[1]{\pgfmathparse{#1}\expandafter\printplainafter\pgfmathresult\MyEnd}

\tracingmacros=1

\begin{document}
%
\par $f(x)=(x+1)(x-1)(x+5)$
\par\begin{tabular}{r@{.}l r@{.}l}
\hline
\multicolumn{2}{l}{\textbf{\em x}} & \multicolumn{2}{l}{\textbf{\em f}(\textbf{\em x})} \\
\hline
\pgfplotsforeachungrouped \kk in {1,...,5}
{%
\pgfmathsetlength{\global\xval}{\expandafter-6+\kk*2}
\leftdigits{\xval} & \rightdigits{\xval} &
\pgfmathsetlength{\global\fval}{F(\xval)}
\leftdigits{\fval} & \rightdigits{\fval} \\
\ifdim\xval=4pt\hline\fi
}
\end{tabular}
%
\end{document}

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.

5

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.

\documentclass{article}
\usepackage{siunitx}
\usepackage{pgfplots}

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

\newcommand\foreachappendto[2]{%
  \xdef#1{\unexpanded\expandafter{\temp}#2}%
}
\begin{document}
$f(x)=(x+1)(x-1)(x+5)$

\medskip

% Build the table body outside the table
\gdef\temp{}
\foreach \kk in {1,...,5}{
  \pgfmathparse{2*\kk-6}%
  \foreachappendto\temp{\pgfmathresult&}%
  \pgfmathparse{F(\pgfmathresult)}%
  \foreachappendto\temp{\pgfmathresult\noexpand\\}%
}
\begin{tabular}{
  S[table-format=-1.1]
  S[table-format=-2.1]
}
\hline
{\boldmath$x$} & {\boldmath$f(x)$} \\
\hline
\temp
\hline
\end{tabular}

\end{document}

enter image description here

1

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.

\ifdim\xval=4pt\hline\end{tabular}\fi

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.