3

I defined some macros to store data. Them, I typeset them in a table. If one of the macro is not executed (the corresponding data is not available, the row should not appear.

\documentclass{article}
\usepackage{booktabs}
\usepackage{xifthen}

\DeclareRobustCommand*{\version}[1]{%
\DeclareRobustCommand*{\theversion}{#1}%
}
\DeclareRobustCommand*{\revised}[1]{%
\DeclareRobustCommand*{\therevised}{#1}%
}
\DeclareRobustCommand*{\approved}[1]{%
\DeclareRobustCommand*{\theapproved}{#1}%
}

\newcommand{\mytable}{%
  \begin{tabular*}{\linewidth}{p{0.4\linewidth}p{0.6\linewidth}}
    \toprule
    \ifthenelse{\isundefined{\theversion}}{}{Version: &\theversion\\}%
    \ifthenelse{\isundefined{\therevised}}{}{Revised by: &\therevised\\}%
    \ifthenelse{\isundefined{\theapproved}}{}{Approved by:&\theapproved\\}%
    \bottomrule%
    \end{tabular*}
}


\version{3.2}
\revised{J. Joe}
\approved{A. Uthor}
\begin{document}
\mytable
\end{document}

The code runs fine if I comment the \revised{J. Joe} line. But if I comment \approved{A. Uthor}, them the following error is rised:

(./example.aux)
! Misplaced \noalign.
\bottomrule ->\noalign 
                       {\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global...
l.34 \mytable

Can anyone help me to find the bug? Thanks in advance.

3

I don't know precisely what's going on, but it's clearly an expansion problem. It can be solved with more direct syntax:

\newcommand*{\version}[1]{\newcommand*{\theversion}{#1}}
\newcommand*{\revised}[1]{\newcommand*{\therevised}{#1}}
\newcommand*{\approved}[1]{\newcommand*{\theapproved}{#1}}

\newcommand{\mytable}{%
  \noindent\begin{tabular}{
    p{\dimexpr 0.4\linewidth-2\tabcolsep\relax}
    p{\dimexpr 0.6\linewidth-2\tabcolsep\relax}
    }
    \toprule
    \ifdefined\theversion Version: &\theversion\\\fi
    \ifdefined\therevised Revised by: &\therevised\\\fi
    \ifdefined\theapproved Approved by:&\theapproved\\\fi
    \bottomrule
    \end{tabular}
}

There's no need to declare those commands as robust. Moreover tabular* is useless and it's necessary to correct the width to take into account the separation between columns.

  • Thanks for the solution. The actual table is defined inside a sidenote of the tufte-latex package, and it is necessary to use the starred version of tabular (I don't know why) – TeXtnik Jul 19 '11 at 7:06

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.