检查空列表元素时出错

检查空列表元素时出错

当我运行这个 MWE 时

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{ifthen}
\usepackage{xifthen}
\usepackage{etoolbox}

\newcommand{\Entry}[6][\textwidth-3cm-16pt]{        %% new entry
\parbox[t]{#1}{%
    \ifthenelse{ \equal{#3}{}           }{}{\textbf{#3}}%
    \ifthenelse{ \equal{#4}{}           }{}{\ifthenelse{\equal{#3}{}}{}{\headdelimiter}{#4}}
    \ifthenelse{ \equal{#5}{}           }{}{\hfill\slshape#5\upshape\hspace{-7mm}}%
    \ifthenelse{ \equal{#5}{}\OR\equal{#6}{}            }{}{\hspace{-7mm}\\[-3.0mm]{\color{colI!20}\rule[2pt]{#1}{0.5pt}}\vspace{-6.0mm}}%
    \ifblank{#6}                        {}{
        \begin{itemize}
            #6
        \end{itemize}
    }
}
}

\begin{document}

\Entry{D}{A}{B}{C}{
    \item asdf
}

\Entry{D}{A}{B}{C}{}

\end{document}

我得到了错误

不完整 \iffalse;第 26 行之后的所有文本均被忽略

我不知道我做错了什么。有人能帮忙吗?

答案1

一个问题是,出现在第 5 个参数中,但这并不是有意的,实际上它是用于第 6 个参数的,因此出现了列表外的 \item偏离。\item

etoolbox更喜欢而不是ifthen检查\ifblank参数是否为空。

\documentclass[a4paper,10pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{etoolbox}


\newcommand{\Entry}[6]{%
  \ifblank{#5}{}{#5}
  \ifblank{#6}{}{%
    \begin{itemize}
      #6
    \end{itemize}
  }
}

\begin{document}

\Entry{D}{A}{B}{C}{}{%
\item asdf
}

\Entry{D}{A}{B}{C}{}{}

\end{document}

具有可选参数的变体

\documentclass[a4paper,10pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{xparse}


\NewDocumentCommand{\Entry}{+m+m+m+m+o+d<>}{%
  \IfValueT{#5}{#5}
  \IfValueT{#6}{%
    \begin{itemize}
      #6
    \end{itemize}
  }%
}

\begin{document}

\Entry{D}{A}{B}{C}<%
\item asdf
>

\Entry{D}{A}{B}{C}[E]<\item And now for something completely different>

\end{document}

答案2

试试这个代码:

\documentclass{article}
\usepackage{calc}
\newcommand{\Entry}[6][\textwidth-3cm-16pt]{
\parbox[t]{3cm}{#2}%
\parbox[t]{#1}{%
    \ifx#5\relax\else\hfill\slshape#5\upshape\hspace{-7mm}\fi%
    \ifx#6\relax\else
        \begin{itemize}
            #6
        \end{itemize}
    \fi
    }
}
\begin{document}
\Entry{D}{A}{B}{}{
\item asdf
}

\Entry{D}{A}{B}{C}{}
\end{document}

相关内容