明确设置枚举级别

明确设置枚举级别

我需要在最外层有一个自定义列表环境,定义如下:

\newboolean{odd}
\setboolean{odd}{true}
\usepackage{environ}
\newcounter{exnum}[section]% Exercise counter (within section)
\NewEnviron{solution}{% Solution
   \stepcounter{exnum}%
   \ifthenelse{\isodd{\theexnum}}% Condition on oddness
      {\item \BODY}% Print odd answer
      {\ifthenelse{\boolean{odd}}{}{\item \BODY}}% Print odd
}
\newenvironment{outerlist}%
    {\begin{list}%
        {{\bfseries\arabic{exnum}.}}{}}%
    {\end{list}}%

所有这些的目的在于能够设置odd布尔值并改变是否输出所有项目或仅输出奇数项目。

这一切都很好,除了这个列表环境outerlist不会增加枚举级别。因此,内部枚举从级别 1 开始。这与视觉外观不一致(outerlist在输出中显然看起来像外部枚举),并且让维护者感到困惑(我自己也困惑了好几次 :)

我可以告诉 LaTeX 从枚举级别 2 开始吗?或者有没有更好的方法来编写上述内容以获得我想要的行为?

编辑:根据要求,这里是 MWE:

\documentclass{article}
\usepackage{environ}

\newcounter{itemnum}
\NewEnviron{outeritem}{% Solution
   \stepcounter{itemnum}%
   {\item \BODY}%
}

\newenvironment{outerlist}%
    {\begin{list}%
        {{\bfseries\arabic{itemnum}.}}{}}%
    {\end{list}}%

\renewcommand{\labelenumi}{{\bfseries(\alph{enumi})}}
\renewcommand{\labelenumii}{{\bfseries(\roman{enumii})}}

\begin{document}

\begin{outerlist}
\begin{outeritem}
  This level is numbered with arabic numerals, but is not really an enumerate list.
\end{outeritem}
\begin{outeritem}
  This is outer list item 2.
  \begin{enumerate}
    \item This looks visually to be at level 2, but LaTeX thinks of it as a level 1 enum, 
        and it is governed by $\backslash$labelenumi. I would like it to be governed by
        $\backslash$labelenumii, so that the TeX file can use level 2 definitions for 
        this list level format.
  \end{enumerate}
\end{outeritem}
\end{outerlist}
\end{document}

答案1

可以调整列表深度/级别。让我们看看它的enumerate作用是什么(摘自latex.ltx):

\def\enumerate{%
  \ifnum \@enumdepth >\thr@@\@toodeep\else
    \advance\@enumdepth\@ne
    \edef\@enumctr{enum\romannumeral\the\@enumdepth}%
      \expandafter
      \list
        \csname label\@enumctr\endcsname
        {\usecounter\@enumctr\def\makelabel##1{\hss\llap{##1}}}%
  \fi}

在定义中\advance\@enumdepth\@ne增加枚举的深度/级别。我们可以定义

\newcommand{\advanceenumeratelevel}{\advance\@enumdepth\@ne}

并在需要的地方使用它:

在此处输入图片描述

\documentclass{article}
\usepackage{environ}% http://ctan.org/pkg/environ

\newcounter{itemnum}
\NewEnviron{outeritem}{% Solution
   \stepcounter{itemnum}%
   {\item \BODY}%
}
\makeatletter
\newcommand{\advanceenumeratelevel}{\advance\@enumdepth\@ne}
\makeatother

\newenvironment{outerlist}%
  {\begin{list}%
    {{\bfseries\arabic{itemnum}.}}{}}%
  {\end{list}}%

\renewcommand{\labelenumi}{{\bfseries(\alph{enumi})}}
\renewcommand{\labelenumii}{{\bfseries(\textit{\roman{enumii}})}}

\begin{document}

\begin{outerlist}
\begin{outeritem}
  This level is numbered with arabic numerals, but is not really an enumerate list.
\end{outeritem}
\begin{outeritem}
  This is outer list item 2.
  \advanceenumeratelevel
  \begin{enumerate}
    \item This looks visually to be at level 2, but LaTeX thinks of it as a level 1 enum, 
        and it is governed by $\backslash$labelenumi. I would like it to be governed by
        $\backslash$labelenumii, so that the TeX file can use level 2 definitions for 
        this list level format.
  \end{enumerate}
\end{outeritem}
\end{outerlist}
\end{document}

由于我不知道列表中的全部使用范围enumerate,您可能需要考虑在末尾降低级别outeritem。例如,以下内容可能就足够了:

\makeatletter
\newcommand{\advanceenumeratelevel}{\advance\@enumdepth\@ne}
\newcommand{\retractenumeratelevel}{\advance\@enumdepth\m@ne}
\makeatother
\newenvironment{outerlist}%
  {\advanceenumeratelevel%
   \begin{list}%
    {{\bfseries\arabic{itemnum}.}}{}}%
  {\end{list}%
   \retractenumeratelevel}%

答案2

我会选择enumitem

\documentclass{article}
\usepackage{environ}
\usepackage{enumitem}

\NewEnviron{outeritem}{% Solution
  \item \BODY
}

\newlist{outerlist}{enumerate}{1}
\setlist[outerlist,1]{label=\bfseries\arabic*.,resume,before=\advancedepth}
\setlist[enumerate,2]{label=\bfseries(\roman*)}
\makeatletter
% hook into LaTeX's enumerate depth
\newcommand{\advancedepth}{\advance\@enumdepth\@ne}
\makeatother

\begin{document}

\begin{outerlist}
\begin{outeritem}
  This level is numbered with arabic numerals
\end{outeritem}
\begin{outeritem}
  This is outer list item 2.
  \begin{enumerate}
    \item This is at level 2.
  \end{enumerate}
\end{outeritem}
\end{outerlist}
\end{document}

在此处输入图片描述

仅打印偶数项目很容易添加:

\documentclass{article}
\usepackage{environ}
\usepackage{enumitem,etoolbox}

\newtoggle{printodd}
\NewEnviron{outeritem}{% Solution
  \iftoggle{printodd}
    {%
     \item \BODY
    }
    {%
     \ifnumodd{\value{outerlisti}}
       {\item\BODY}% the next item is even, print it
       {\stepcounter{outerlisti}}% the next item is odd, skip it
    }%
}

\newlist{outerlist}{enumerate}{1}
\setlist[outerlist,1]{label=\bfseries\arabic*.,resume,before=\advancedepth}
\setlist[enumerate,2]{label=\bfseries(\roman*)}
\makeatletter
\newcommand{\advancedepth}{\advance\@enumdepth\@ne}
\makeatother

\begin{document}

The toggle is initially false, only even numbered items are printed

\begin{outerlist}
\begin{outeritem}
  This level is numbered with arabic numerals
\end{outeritem}
\begin{outeritem}
  This is outer list item 2.
  \begin{enumerate}
    \item This is at level 2.
  \end{enumerate}
\end{outeritem}
\end{outerlist}

Now we set the toggle to true, all items are printed
\toggletrue{printodd}

\begin{outerlist}
\begin{outeritem}
  This level is numbered with arabic numerals
\end{outeritem}
\begin{outeritem}
  This is outer list item 2.
  \begin{enumerate}
    \item This is at level 2.
  \end{enumerate}
\end{outeritem}
\end{outerlist}

\end{document}

在此处输入图片描述

答案3

我指的是你真正想要的:如果相关布尔值是这样设置的,则只输出奇数/偶数项。

\documentclass{article}
\usepackage{ifthen}
\newboolean{oodd}
\setboolean{oodd}{true}
\usepackage{environ}
\makeatletter
\NewEnviron{Item}{%
\ifthenelse{\boolean{oodd}}
 {
  \ifthenelse{\isodd{\value{enum\romannumeral\@enumdepth}}}{\item \BODY}{\refstepcounter{enum\romannumeral\@enumdepth}}
 }
 {\item\BODY}
}
\makeatother
\begin{document}
\begin{enumerate}
\begin{Item}
Eins
\end{Item}
\begin{Item}
Zwei
\begin{enumerate}
 \begin{Item}
 Hallo
 \end{Item}
 \begin{Item}
 Du
 \end{Item}
 \begin{Item}
 Da
 \end{Item}
 \begin{Item}
 !
 \end{Item}
\end{enumerate}
\end{Item}
\begin{Item}
Drei
\end{Item}
\begin{Item}
Vier
\end{Item}
\end{enumerate}
\end{document}

相关内容