通过可选参数自定义列表标签

通过可选参数自定义列表标签

我正在为一个班级编写教学大纲,我为该​​班级创建了一个自定义enumerate环境,class该环境按照我想要的方式格式化。关键是它会自动添加单词“Class X”,其中 X 是计数器。MWE:

\documentclass{scrartcl}
\usepackage{enumitem}
    \newlist{classes}{enumerate}{1}
    \setlist[classes]{
        label=\textbf{Class \arabic*: },
        labelindent=0pt,
        itemindent=0pt,
        leftmargin=!
        style = multiline,
        labelwidth = 50pt,
        before=\setlength{\listparindent}{-\leftmargin},    
    }
\begin{document}
\section*{Syllabus}
Syllabus description.
\begin{classes}
\subsection*{I. Introduction and framework}
\item foo.
\end{classes}
\end{document}

我没能做到的是添加一个可选参数来\item标识课程是理论课程还是实践课程。比如

\item[t] theoretical class
\item[p] practical class

这个想法是,如果指定了该选项,则标签下方会打印单词“理论”或“实践”。因此输出应如下所示:

在此处输入图片描述

答案1

[t]带有或支持的版本[p](包装标签线)

\documentclass{scrartcl}
\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{letltxmacro}

\newtoggle{classtoggle}%
\newif\iftheoreticalclass


\usepackage{enumitem}

\newlength{\classcellwidth}
\setlength{\classcellwidth}{60pt}

\DeclareRobustCommand{\theoclass}[1]{%
  \begin{tabular}[t]{p{\classcellwidth}}%
    \textbf{Class \number\value{#1}:}  \tabularnewline
    \bfseries Theoretical
  \end{tabular}%
}

\DeclareRobustCommand{\practicalclass}[1]{%
  \begin{tabular}[t]{p{\classcellwidth}}%
    \textbf{Class \number\value{#1}:}  \tabularnewline
    \bfseries Practical
  \end{tabular}%
}

\DeclareRobustCommand{\defaultclass}[1]{%
  \begin{tabular}[t]{p{\classcellwidth}}%
    \textbf{Class \number\value{#1}:}  \tabularnewline
  \end{tabular}%
}


\makeatletter
\newcommand{\classctr}[1]{\classctrint{#1}}
\newcommand{\classctrint}[1]{%
  \iftoggle{classtoggle}{%
    \iftheoreticalclass%
    \theoclass{#1}%
    \else
    \practicalclass{#1}%
    \fi%
  }{%
    \defaultclass{#1}%
  }%
}
\makeatother

\AddEnumerateCounter*{\classctr}{\classctrint}{40}

    \newlist{classes}{enumerate}{1}
    \setlist[classes]{%
      label=\classctr*,
      labelindent=0pt,
      itemindent=0pt,
      leftmargin=!
      style = multiline,
      labelwidth = 50pt,
      before=\setlength{\listparindent}{-\leftmargin},    
    }

\LetLtxMacro\origitem\item

\RenewDocumentCommand{\item}{o}{%
  \toggletrue{classtoggle}% Switch to use t or p mode temporarily
  \IfValueTF{#1}{%
    \theoreticalclassfalse%
    \ifstrequal{#1}{t}{%
      \theoreticalclasstrue%
      \origitem%
    }{%
      \ifstrequal{#1}{p}{%
        \theoreticalclassfalse%
        \origitem%
      }{% 
        \togglefalse{classtoggle}% No, it's neither p nor t
        \origitem[#1]%
      }%
    }%
  }{%
    \togglefalse{classtoggle}%
    \origitem
  }%
}

\begin{document}
\section*{Syllabus}
Syllabus description.
\begin{classes}
\subsection*{I. Introduction and framework}
\item[p] foo.
\item[t] bar
\item    None
\item[p]    Again
\end{classes}

\begin{enumerate}
\item[t] Test
\end{enumerate}
\end{document}

在此处输入图片描述

答案2

我设法通过创建自定义命令来实现这一点\item,但我认为可能还有更好的解决方案。无论如何,这是我的尝试:

\documentclass{scrartcl}
\usepackage{enumitem}
\newlist{classes}{enumerate}{1}
\setlist[classes]{
  label=Class \arabic*:\protect\thiscitem,
  style = multiline,
  leftmargin=!,
  labelwidth = 60pt,
}
\newcommand{\citem}[1][]{%
  \if\relax\detokenize{#1}\relax
    \def\thiscitem{}%
  \else
    \def\thiscitem{\\(#1)}%
  \fi
  \item}
\begin{document}
\section*{Syllabus}
Syllabus description.
\begin{classes}
\subsection*{I. Introduction and framework}
\citem[Theoretical] foo.
\end{classes}
\end{document}

在此处输入图片描述

相关内容