具有可选行的表格

具有可选行的表格

我定义了一些宏来存储数据。然后,我将它们排版到表格中。如果其中一个宏未执行(相应数据不可用),则该行不应出现。

\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}

如果我注释掉该行,代码可以正常运行\revised{J. Joe}。但是如果我注释掉 \approved{A. Uthor},就会出现以下错误:

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

有人能帮我找到这个错误吗?提前谢谢了。

答案1

我不知道具体发生了什么,但这显然是一个扩展问题。可以用更直接的语法来解决:

\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}
}

没有必要将这些命令声明为健壮的。此外,tabular*它们毫无用处,并且需要校正宽度以考虑列之间的间隔。

相关内容