代码无法通过条件包装

代码无法通过条件包装

下面的例子中,如果条件为真,我希望 中的内容minipage用 突出显示。如果条件为假,则直接排版 中的内容。\colorboxminipage

然而 true-branch 的字体并没有被突出显示。我认为这是因为\fi

那么,如何修改这段代码,让其按照预期工作呢?

代码:

\documentclass{article}
\usepackage{xcolor}
\begin{document}

\ifnum 5>1 % or something like \ifx..., \if\empty..., and so on.
\colorbox{red} \fi
{\begin{minipage}{2in}The content maybe very large and complex, I don't want to duplicate it in both true-branch and false-branch\end{minipage}}

\end{document}

答案1

\documentclass{article}
\usepackage{xcolor}

\makeatletter
\newcommand\myConditionTF[1]{%
  #1% or something like \ifx..., \if\empty..., and so on.
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

\begin{document}

\myConditionTF{\ifnum 5>1\relax}
  {\colorbox{red}}
  {}
{\begin{minipage}{2in}The content maybe very large and complex, I don't want to duplicate it in both true-branch and false-branch\end{minipage}}
\qquad
\myConditionTF{\ifnum 5<1\relax}
  {\colorbox{red}}
  {}
{\begin{minipage}{2in}The content maybe very large and complex, I don't want to duplicate it in both true-branch and false-branch\end{minipage}}
\end{document}

在此处输入图片描述

相关内容