在模板 .cls 文件中使列表可选

在模板 .cls 文件中使列表可选

我的简历中的 .cls 文件中有以下代码。

    \newenvironment{rSubsection}[4]{ % 4 input arguments - company name, year(s) employed, job title and location
     {\bf #1} \hfill {#2} % Bold company name and date on the right
     \ifthenelse{\equal{#3}{}}{}{ % If the third argument is not specified, don't print the job title and location line
      \\
      {\em #3} \hfill {\em #4} % Italic job title and location
      }\smallskip
      \begin{list}
      {$\cdot$}{\leftmargin=1.0em} % \cdot used for bullets, no indentation
       \itemsep -0.5em \vspace{-0.5em} % Compress items in list together for aesthetics
      }{
      \end{list}
      \vspace{0.5em} % Some space after the list of bullet points
    }

注意\begin{list}。这需要\itemlatex 文件中的。如果未能提供,\item则会引发以下错误:

LaTeX 错误:出现问题 - 也许缺少 \item。

如何使列表成为可选的?

答案1

下面使用两个包来满足您的要求:

  • environ提供在宏内捕获环境主体的方法\BODY。这样我们就可以重写每个环境作为一个\begin,当想要管理和之间的内容时,这有时会更容易\end

  • etoolbox提供\patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}。我用它来替换\item\item实际上什么都不做)\BODY环境的,并允许人们判断这种替换是否有效<success>

在此处输入图片描述

\documentclass{article}

\usepackage{etoolbox,environ}

\NewEnviron{rSubsection}[4]{% 4 input arguments - company name, year(s) employed, job title and location
  {\bfseries #1} \hfill #2 % Bold company name and date on the right
  % https://tex.stackexchange.com/q/263733/5764
  \if\relax\detokenize{#3}\relax\else % If the third argument is not specified, don't print the job title and location line
    \par
    {\itshape #3 \hfill #4} % Italic job title and location
  \fi\par\smallskip
  % \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
  \patchcmd{\BODY}{\item}{\item}{%
    \list
      {$\cdot$}
      {\leftmargin=1.0em % \cdot used for bullets, no indentation
      \itemsep -0.5em \vspace{-0.5em}} % Compress items in list together for aesthetics
      \BODY
    \endlist
  }{}%
  \vspace{0.5em} % Some space after the list of bullet points
}

\begin{document}

\begin{rSubsection}{First}{Second}{Third}{Fourth}
  \item First item
  \item Second item
  \item Third item
  \item Last item
\end{rSubsection}

\begin{rSubsection}{First}{Second}{}{Fourth}
  \item First item
  \item Second item
  \item Third item
  \item Last item
\end{rSubsection}

\begin{rSubsection}{First}{Second}{Third}{Fourth}
\end{rSubsection}

\end{document}

相关内容