\ifdefined 在表环境中

\ifdefined 在表环境中

我尝试在表环境中使用 \ifdefined,但是它不起作用。

这是我的代码:

\begin{document}
\lipsum[1]
\lipsum[1]
\lipsum[1]

\definecolor{lightgray}{HTML}{EFEFEF}

%pass to it the text 
\newcommand{\TabularEnvironment}[1]{\begin{tabular}[c]{@{}p{\linewidth}@{}} #1 \end{tabular}}

\newcommand{\One}{}

\begin{table}[H]
\small
\begin{tabular}{| p{0.05\linewidth} | p{0.3\linewidth} | p{0.1\linewidth} | p{0.1\linewidth} | p{0.1\linewidth} | p{0.2\linewidth} |}
\hline
\ifdefined\One
\rowcolor{lightgray} 
\multicolumn{2}{|p{0.4\linewidth}|}{\textbf{BLA}} & \textbf{BLA} & \textbf{BLA} & \textbf{BLA} 
& \textbf{BLA}
\else
\multicolumn{2}{|p{0.4\linewidth}|}{\textbf{BLA}} & \textbf{BLA} & \textbf{BLA} & \textbf{BLA} 
& \textbf{BLA}
\fi
\\ \hline 
1a & BLA
&  ++ & ++ &  ++ 
& BLA BLA BLA BLA BLA BLA
\\ \hline 
1b & Informal notations 
&  ++ & + &  +
& \multirow{2}{1\linewidth}{BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA }
\\ \cline{1-5}
1c & BLA BLA BLA BLA BLA BLA 
&  ++ & ++ &  ++
& \\
 & & & & & \\ 
 & & & & & \\ 
 & & & & & \\ 
 & & & & & \\ 
 & & & & & \\ \hline 
1d & BLA BLA BLA BLA BLA BLA
&  + & + &  +
& ANYTHING
\\ \hline
\end{tabular}%
\caption{\textbf{BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA}}
\label{Table:Table11}
\end{table}

\end{document}

错误导致这一行:

\ifdefined\One

错误:

Incomplete \ifdefined; all text was ignored after line ....

有什么帮助吗?

答案1

定义一个包装命令。

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{multirow}

\definecolor{lightgray}{HTML}{EFEFEF}

\makeatletter
\newcommand{\conditionalrow}[1]{%
  \ifdefined#1%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

%\newcommand{\One}{}

\begin{document}

\begin{table}[htp]
\begin{tabular}{
  | p{0.05\linewidth}
  | p{0.3\linewidth}
  | p{0.1\linewidth}
  | p{0.1\linewidth}
  | p{0.1\linewidth}
  | p{0.2\linewidth}
  |
}
\hline
\conditionalrow\One{%
  \rowcolor{lightgray} 
  \multicolumn{2}{|p{0.4\linewidth}|}{\textbf{BLA}} &
   \textbf{BLA} & \textbf{BLA} & \textbf{BLA} & \textbf{BLA}%
}{%
  \multicolumn{2}{|p{0.4\linewidth}|}{\textbf{BLA}} &
  \textbf{BLA} & \textbf{BLA} & \textbf{BLA} & \textbf{BLA}%
}
\\ \hline 
1a & BLA
&  ++ & ++ &  ++ 
& BLA BLA BLA BLA BLA BLA
\\ \hline 
1b & Informal notations 
&  ++ & + &  +
& \multirow{2}{1\linewidth}{BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA }
\\ \cline{1-5}
1c & BLA BLA BLA BLA BLA BLA 
&  ++ & ++ &  ++
& \\
 & & & & & \\ 
 & & & & & \\ 
 & & & & & \\ 
 & & & & & \\ 
 & & & & & \\ \hline 
1d & BLA BLA BLA BLA BLA BLA
&  + & + &  +
& ANYTHING
\\ \hline
\end{tabular}%
\caption{\textbf{BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA}}
\label{Table:Table11}
\end{table}

\newcommand{\One}{}

\begin{table}[htp]
\begin{tabular}{
  | p{0.05\linewidth}
  | p{0.3\linewidth}
  | p{0.1\linewidth}
  | p{0.1\linewidth}
  | p{0.1\linewidth}
  | p{0.2\linewidth}
  |
}
\hline
\conditionalrow\One{%
  \rowcolor{lightgray} 
  \multicolumn{2}{|p{0.4\linewidth}|}{\textbf{BLA}} &
   \textbf{BLA} & \textbf{BLA} & \textbf{BLA} & \textbf{BLA}%
}{%
  \multicolumn{2}{|p{0.4\linewidth}|}{\textbf{BLA}} &
  \textbf{BLA} & \textbf{BLA} & \textbf{BLA} & \textbf{BLA}%
}
\\ \hline 
1a & BLA
&  ++ & ++ &  ++ 
& BLA BLA BLA BLA BLA BLA
\\ \hline 
1b & Informal notations 
&  ++ & + &  +
& \multirow{2}{1\linewidth}{BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA }
\\ \cline{1-5}
1c & BLA BLA BLA BLA BLA BLA 
&  ++ & ++ &  ++
& \\
 & & & & & \\ 
 & & & & & \\ 
 & & & & & \\ 
 & & & & & \\ 
 & & & & & \\ \hline 
1d & BLA BLA BLA BLA BLA BLA
&  + & + &  +
& ANYTHING
\\ \hline
\end{tabular}%
\caption{\textbf{BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA}}
\label{Table:Table11}
\end{table}

\end{document}

在此处输入图片描述

答案2

您应该将所需的内容放在...部分\\内。然后它应该可以工作(我稍微缩短了您的代码):\ifdefined\fi

\documentclass{article}
\usepackage{float, xcolor, colortbl, multicol}

\begin{document}

\definecolor{lightgray}{HTML}{EFEFEF}

%pass to it the text 
\newcommand{\TabularEnvironment}[1]{\begin{tabular}[c]{@{}p{\linewidth}@{}} #1 \end{tabular}}

\newcommand{\One}{}

\begin{table}[H]
\small
\begin{tabular}{| p{0.05\linewidth} | p{0.3\linewidth} | p{0.1\linewidth} | p{0.1\linewidth} | p{0.1\linewidth} | p{0.2\linewidth} |}
\hline
\ifdefined\One
\rowcolor{lightgray} 
\multicolumn{2}{|p{0.4\linewidth}|}{\textbf{BLA}} & \textbf{BLA} & \textbf{BLA} & \textbf{BLA} 
& \textbf{BLA} \\                         % <-- \\ here
\else
\multicolumn{2}{|p{0.4\linewidth}|}{\textbf{BLA}} & \textbf{BLA} & \textbf{BLA} & \textbf{BLA} 
& \textbf{BLA} \\                         % <-- \\ here
\fi
\hline                                    % <-- instead of here
1a & BLA
&  ++ & ++ &  ++ 
& BLA BLA BLA BLA BLA BLA
\\ \hline 
\end{tabular}%
\end{table}

\end{document}

相关内容