在 tabularx 中使用 \if

在 tabularx 中使用 \if

\if我正在尝试在环境中使用tabularx。这有效:

\documentclass{scrlttr2}

\usepackage{tabularx}

\begin{document}
    \newcounter{x} 
    \setcounter{x}{0}

    \begin{tabularx}{\linewidth}{c|l}
        \stepcounter{x}
            \ifnum\value{x}=20
            it is 20
            \else
            it is not
            \fi
            \\
    \end{tabularx} 

\end{document}

但当然没有列。当我尝试像这样添加它们时:

\documentclass{scrlttr2}

\usepackage{tabularx}

\begin{document}
    \newcounter{x} 
    \setcounter{x}{0}

    \begin{tabularx}{\linewidth}{c|l}
        \stepcounter{x}
            \ifnum\value{x}=20
            it is & 20
            \else
            it is & not
            \fi
            \\
    \end{tabularx} 

\end{document}

我收到此错误:

Incomplete \ifnum; all text was ignored after line 18. \end{tabularx}

答案1

条件语句不能跨越两个单元格。但是你可以使用\ifthenelse

\documentclass{article}

\usepackage{tabularx}
\usepackage{xifthen}

\begin{document}

\newcounter{x}
%\setcounter{x}{0}

\begin{tabular}{c|l}
\stepcounter{x}%
\ifthenelse{\value{x}=20}{it is & 20}{it is & not}\\
\setcounter{x}{20}%
\ifthenelse{\value{x}=20}{it is & 20}{it is & not}\\
\end{tabular}

\setcounter{x}{0}

\begin{tabularx}{.5\textwidth}{c|X}
\stepcounter{x}%
\ifthenelse{\value{x}=20}{it is & 20}{it is & not}\\
\setcounter{x}{20}%
\ifthenelse{\value{x}=20}{it is & 20}{it is & not}\\
\end{tabularx}

\end{document}

在此处输入图片描述

答案2

如果你必须走钢丝,那么:

在此处输入图片描述

\documentclass{scrlttr2}

\usepackage{tabularx}

\begin{document}
    \newcounter{x} 
    \setcounter{x}{0}
   \newcommand\zz[1]{#1}

\centering

    \begin{tabularx}{\linewidth}{c|X}
        \stepcounter{x}
            \ifnum\value{x}=20
            \zz{it is & 20}
            \else
            \zz{it is & not}
            \fi
            \\
    \end{tabularx} 

    \setcounter{x}{19}
    \begin{tabularx}{\linewidth}{c|X}
        \stepcounter{x}
            \ifnum\value{x}=20
            \zz{it is & 20}
            \else
            \zz{it is & not}
            \fi
            \\
    \end{tabularx} 

\end{document}

相关内容