如何预先评估特定环境下有问题的命令?

如何预先评估特定环境下有问题的命令?

更具体地说,我的问题发生在字幕环境中,但我想这也适用于其他地方。

我想插入由 \suppOrApp 生成的文本并将其存储在变量 \sOrA 中:

\documentclass{article}


\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage{caption}
\usepackage{float}
\usepackage{ifthen}

\title{A caption test}
\author{Some one}
\date{\today}


\begin{document}

\newcommand{\suppOrApp}{%
  \ifthenelse{\boolean{thesisStyle}}
    {Appendix}
    {Supplementary}
}
\newboolean{thesisStyle}
\setboolean{thesisStyle}{false} % Might want to reconsider this here.

\def \sOrA {\suppOrApp}

\begin{table}
\begin{tabular}{cc}
1 & 2 \\
3 & 4 \\
\end{tabular}
\caption{\blindtext
\sOrA
\blindtext}
\end{table}


\end{document}

实现此目的最简单或最佳的方法是什么?

答案1

您的命令很脆弱(因为在移动参数中使用\ifthenelse所以需要\protect,例如标题:

\documentclass{article}


\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage{caption}
\usepackage{float}
\usepackage{ifthen}

\title{A caption test}
\author{Some one}
\date{\today}


\begin{document}

\newcommand{\suppOrApp}{%
  \ifthenelse{\boolean{thesisStyle}}
    {Appendix}
    {Supplementary}
}
\newboolean{thesisStyle}
\setboolean{thesisStyle}{false} % Might want to reconsider this here.


% This has additional space:
\renewcommand{\tablename}{\textbf{\suppOrApp Table}}
% But this would work fine.
%\renewcommand{\tablename}{\textbf{Supporting Table}}

\def \sOrA {\suppOrApp}

\begin{table}
\begin{tabular}{cc}
1 & 2 \\
3 & 4 \\
\end{tabular}
\caption{\blindtext
\protect\sOrA
\blindtext}
\end{table}

\end{document} 

相关内容