4

Can anyone explain why the output from these two entries is different? Does anyone know how to make the second one match the first?

\documentclass{article}
\include{fp}
\newcommand\entryOne[1]{
\ifnum #1 = 100 #1 \fi
}
\newcommand\entryTwo[1]{
\FPeval{\result}{#1}
\ifnum \result = 100 \result \fi
}
\begin{document}
\begin{table}
\centering
\begin{tabular}{|l|c|}
\hline
Entry 1 & \entryOne{100} \\
Entry 2 & \entryTwo{100} \\
\hline
\end{tabular}
\end{table}
\end{document}

my beautiful table

Thanks in advance.

5

TeX is not “free form” and spaces are significant: remember that it's a program for typesetting.

You have several spaces in your commands:

\newcommand\entryOne[1]{
\ifnum #1 = 100 #1 \fi
}

The end of line after the brace counts as a space; also the space after the second #1 counts. It's true that in a table cell both are removed, but it's not a good reason for having them in the definition. Fixed definition:

\newcommand\entryOne[1]{%
  \ifnum #1 = 100 #1\fi
}

Note that the space after the first #1 is ignored by syntax rule, like the one after 100.

Second definition:

\newcommand\entryTwo[1]{
\FPeval{\result}{#1}
\ifnum \result = 100 \result \fi
}

You have a space after {, and one at the end of the second line. This is the one that's not removed, because it isn't at the start of a cell, causing the cell to contain <space>100, which explains the misalignment.

Fixed definition:

\newcommand\entryTwo[1]{%
  \FPeval{\result}{#1}%
  \ifnum \result = 100 \result \fi
}

Note that spaces after control sequences are ignored during reading.

Final remarks: \include{fp} is wrong, it should be \usepackage{fp}; the indentation I used makes clearer where the replacement text starts and ends.

\documentclass{article}
\usepackage{fp}

\newcommand\entryOne[1]{%
  \ifnum #1 = 100 #1\fi
}
\newcommand\entryTwo[1]{%
  \FPeval{\result}{#1}%
  \ifnum \result = 100 \result \fi
}
\begin{document}
\begin{table}
\centering
\begin{tabular}{|l|c|}
\hline
Entry 1 & \entryOne{100} \\
Entry 2 & \entryTwo{100} \\
\hline
\end{tabular}
\end{table}
\end{document}

enter image description here

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.