重新定义 \item 命令以不排版某些项目

重新定义 \item 命令以不排版某些项目

假设我想要一个枚举环境,但我不想在其中排版某些项目。我可以在 LateX 中使用以下命令获得它(让我们忽略数字错误的问题,这很容易修复)

\documentclass{article}
\newcounter{excluded}
\newcommand{\exclude}[1]{\setcounter{excluded}{#1}}
\let\Item\item
\renewcommand{\item}[1]{\ifnum\value{enumi}=\value{excluded}%
   \stepcounter{enumi}\else\Item #1\fi}
\begin{document}

\begin{enumerate}
\exclude{2}
\item{This is the first item}
\item{This should not be shown}
\item{This should be shown}
% I would like to be able to use
%\item This is an item without \{ and \}. Would make the course look nicer.
\end{enumerate}
\end{document}

此设置的问题是,项目文本现在需要作为 的参数\item,即我需要在文本周围使用花括号。如果我仍然可以使用

\item This is the first item

但我无法让它工作。

有什么优雅的方法可以解决这个问题吗?

答案1

使用一些想法enumerate自动对环境中的物品进行排序,以下 MWE 提供了一个myenumerate环境,您可以在其中将可选参数作为数字传递,然后从列表中排除该特定项目。

在此处输入图片描述

\documentclass{article}
\usepackage{environ}% http://ctan.org/pkg/environ
\makeatletter
\newcounter{listcount}
\let\olditem\item% Store regular \item macro
\NewEnviron{myenumerate}[1][]{%
  \if\relax\detokenize{#1}\relax% A normal list (https://tex.stackexchange.com/q/53068/5764)
  \else% A reordered list
    \g@addto@macro{\BODY}{\item\relax\item}% Used to delimit the items; last item identified by \item\relax\item
    \setcounter{listcount}{0}% Restart at the beginning of myenumerate
    \def\item##1\item{% Redefine \item to capture contents
      \def\optarg{##1}%
      \expandafter\ifx\optarg\relax\else% Last item not reached
        \stepcounter{listcount}% Next item being processed
        \ifnum\value{listcount}=#1\stepcounter{enumi}\else% Don't print this item, just step enumi
          \olditem ##1% Print regular item
        \fi
        \expandafter\item% Recursively continue processing items
      \fi
    }%
  \fi
  \enumerate\BODY\endenumerate% Process environment
}
\makeatother
\begin{document}
\begin{myenumerate}
  \item This is the first item.
  \item This is the second item.
  \item This is the third item.
\end{myenumerate}

\begin{myenumerate}[2]
  \item This is the first item.
  \item This is the second item.
  \item This is the third item.
\end{myenumerate}
\end{document}

它可以推广到处理要丢弃的物品列表。稍加修改并引入etoolbox的列表处理,以下处理每个数字序列\item并设置或不设置该项目。

在此处输入图片描述

\documentclass{article}
\usepackage{environ,etoolbox}% http://ctan.org/pkg/{environ,etoolbox}
\makeatletter
\newif\ifprintitem% Condition to print item or not
\newcounter{listcount}
\let\olditem\item% Store regular \item macro
\NewEnviron{myenumerate}[1][]{%
  \if\relax\detokenize{#1}\relax% A normal list
  \else% A reordered list
    \g@addto@macro{\BODY}{\item\relax\item}% Used to delimit the items; last item identified by \item\relax\item
    \setcounter{listcount}{0}% Restart at the beginning of myenumerate
    \def\item##1\item{% Redefine \item to capture contents
      \def\optarg{##1}%\show\optarg%
      \expandafter\ifx\optarg\relax\else% Last item not reached
        \stepcounter{listcount}% Next item being processed
        \printitemtrue% Always print item
        \renewcommand*{\do}[1]{\ifnum\value{listcount}=####1 \printitemfalse\fi}%
        \expandafter\docsvlist\expandafter{#1}% Process list of possible exclusions
        \ifprintitem
          \olditem ##1% Print regular item
        \else\stepcounter{enumi}% Don't print this item, just step enumi
        \fi
        \expandafter\item% Recursively continue processing items
      \fi
    }%
  \fi
  \enumerate\BODY\endenumerate% Process environment
}
\makeatother
\begin{document}
\begin{myenumerate}
  \item This is the first item.
  \item This is the second item.
  \item This is the third item.
  \item This is the fourth item.
  \item This is the fifth item.
  \item This is the sixth item.
  \item This is the seventh item.
  \item This is the eighth item.
\end{myenumerate}

\begin{myenumerate}[2,3,8]
  \item This is the first item.
  \item This is the second item.
  \item This is the third item.
  \item This is the fourth item.
  \item This is the fifth item.
  \item This is the sixth item.
  \item This is the seventh item.
  \item This is the eighth item.
\end{myenumerate}
\end{document}

相关内容