Enumitem \SetEnumitemKey 与前后多个键之间发生冲突

Enumitem \SetEnumitemKey 与前后多个键之间发生冲突

我尝试通过将包的键设置为 list 来简化操作enumitem。但是,选项beforeafter在多次使用时似乎会发生冲突。

以下是显示四个示例的代码:

  • 第一个只使用密钥cols
  • 第二个只是关键dots
  • 第三个,即没有按预期工作,首先使用键cols,然后使用键dots
  • 最后也使用这两个键,但是按照不同的顺序,效果很好。

我想知道为什么第三个不起作用但最后一个工作正常。

before也许与两次使用有冲突after......

\documentclass[twocolumn]{article}
\RequirePackage[a4paper, top=0.5cm, bottom=1.6cm, left=0.7cm , right=0.7cm]{geometry}
\parindent0cm
\usepackage{multido}
\usepackage{enumitem}
\usepackage{multicol}

\newcommand{\auxpts}[0]{\makebox[\linewidth]{\dotfill} \bigbreak}
% Pointilles
\newcommand{\Pointilles}[1]{%
    \vspace*{-\medskipamount}
    \multido{\i=1+1}{#1}{%
        \auxpts{}
    }
}

\SetEnumitemKey{cols}{before*=\begin{multicols}{#1},
        after*=\end{multicols}} % col for columns

\newif\iffirstItem\firstItemfalse% We need a toggle to track whether the item is first in the list or not

\SetEnumitemKey{dots}{before*={% This saves the standard definition of \item and then redefines at the end
        \let\olditem\item% save the standard definition of \item in a macro, \olditem
        \firstItemtrue% set the toggle for first item in the list to true
        \def\item{\iffirstItem\olditem\firstItemfalse\else \newline \Pointilles{#1} \mbox{}\olditem\fi}% new, temporary defition of \item
    },
    after*={%
        \ \newline \Pointilles{#1}\mbox{}% fill for final item in list
        \let\item\olditem% restore standard definition of \item
    }%
}
%==========================================
\begin{document}

\Pointilles{2}
\hrule
\begin{enumerate}[cols=4]
    \item first
    \item second
    \item third
    \item fourth
\end{enumerate}
\hrule
\begin{enumerate}[dots=2]
    \item first
    \item second
    \item third
    \item fourth
\end{enumerate}
\hrule
\begin{enumerate}[cols=2, dots=2]
    \item first
    \item second
    \item third
    \item fourth
\end{enumerate}
\hrule
\begin{enumerate}[dots=2, cols=2]
    \item first
    \item second
    \item third
    \item fourth
\end{enumerate}
\hrule
\end{document}

答案1

当您说before*=<something>然后 时before*=<something else>,第二个声明将覆盖第一个。

您可以为此目的设置不同的键值系统。

\documentclass[twocolumn]{article}
\usepackage[a4paper, top=0.5cm, bottom=1.6cm, left=0.7cm , right=0.7cm]{geometry}
\parindent0cm
\usepackage{multido}
\usepackage{enumitem}
\usepackage{multicol}

\newcommand{\auxpts}[0]{\makebox[\linewidth]{\dotfill} \bigbreak}
% Pointilles
\newcommand{\Pointilles}[1]{%
    \vspace*{-\medskipamount}
    \multido{\i=1+1}{#1}{%
        \auxpts{}
    }
}

\newlist{innanswers}{enumerate}{1}
\setlist[innanswers]{
  label=\arabic*.,
  before=\maybemulticols\maybedots,
  after=\maybefinaldots\maybeendmulticols
}
\newenvironment{answers}[1][]
 {\setkeys{m45}{#1}\innanswers}
 {\endinnanswers}

\let\maybemulticols=\relax
\let\maybeendmulticols=\relax
\let\maybedots=\relax
\let\maybefinaldots=\relax
\def\doitemdots#1{%
  \let\olditem\item% save the standard definition of \item in a macro, \olditem
  \firstItemtrue% set the toggle for first item in the list to true
  \def\item{%
    \iffirstItem
      \olditem\firstItemfalse
    \else
      \newline\Pointilles{#1}\mbox{}\olditem
   \fi
  }% new, temporary defition of \item
}
\def\dofinaldots#1{%
  \ \newline \Pointilles{#1}\mbox{}% fill for final item in list
}

\makeatletter
\define@key{m45}{cols}{%
  \def\maybemulticols{\begin{multicols}{#1}}%
  \def\maybeendmulticols{\end{multicols}}%
}
\newif\iffirstItem\firstItemfalse

\define@key{m45}{dots}{%
  \def\maybedots{\doitemdots{#1}}
  \def\maybefinaldots{\dofinaldots{#1}}
}
\makeatother
%==========================================
\begin{document}

\Pointilles{2}
\hrule
\begin{answers}[cols=4]
    \item first
    \item second
    \item third
    \item fourth
\end{answers}
\hrule
\begin{answers}[dots=2]
    \item first
    \item second
    \item third
    \item fourth
\end{answers}
\hrule
\begin{answers}[cols=2, dots=2]
    \item first
    \item second
    \item third
    \item fourth
\end{answers}
\hrule
\begin{answers}[dots=2, cols=2]
    \item first
    \item second
    \item third
    \item fourth
\end{answers}
\hrule
\end{document}

在此处输入图片描述

相关内容