建立一個可以關閉的枚舉環境

建立一個可以關閉的枚舉環境

這個問題導致了一個新的包:
comment_io

我想使用枚舉環境建立一個文檔,然後可以在編譯之前打開和關閉該環境。

例如,假設我有一個如下所示的部分:

\begin{enumerate}
    \item This is a test.
    \item This is another test.
    \item This is yet another test.
\end{enumerate}

在正常情況下,這將編譯為包含三個項目的枚舉列表,如下所示:

1. This is a test.
2. This is another test.
3. This is yet another test.

我希望有一種方法可以讓這個特定的枚舉環境消失,以便編譯後的輸出看起來像這樣:

This is a test. This is another test. This is yet another test.

此外,我希望能夠針對不同的環境在全域範圍內執行此操作(也就是說,不必在本機層級編輯相關環境的每個實例)。

但是,我仍然希望能夠在我的文件中保留其他枚舉環境(即使它們嵌套在消失的環境中)。

上下文:我想使用 Tractatus 風格來編寫我的文本,將每個部分放在列舉的層次結構中。但是,我還希望能夠刪除此枚舉並僅將文字顯示為常規文章。最後,在這篇常規文章中,我希望能夠顯示枚舉清單(這樣就不會成為 Tractatus 枚舉的一部分)。

答案1

為了允許嵌套enumerate(或其他清單)環境仍然起作用,您需要捕獲\item是否要在這個新的 on-off 中使其成為無操作enumerate。為此,我們將其定義儲存在序言中,並在每次enuemrate使用開始時重新使用它etoolbox\AtBeginEnvironment.如果您也計劃嵌套其他列表,則需要\item以類似的方式恢復。

下面我定義了enumerate*在編譯期間關閉的功能,同時它仍然支援巢狀enumerate

在此輸入影像描述

\documentclass{article}
\usepackage{etoolbox}
\AtBeginEnvironment{enumerate}{\let\item\olditem}
\let\olditem\item
\newenvironment{enumerate*}
  {\par\let\item\relax}
  {\par}
\begin{document}

\begin{enumerate*}
  \item Something
    \begin{enumerate}
      \item Something else
      \item Another thing
        \begin{enumerate*}
          \item This is it
          \item This is another it
        \end{enumerate*}
    \end{enumerate}
  \item Last thing
\end{enumerate*}

\end{document}

enumerate*重新定義\item為不執行任何操作 (be \relax)。因此,它會完全忽略任何可選參數(如果有)。也就是說,\item[<stuff>]將預設為[<stuff>].如果需要,可以對其進行調整以吞噬可選參數。

也可以透過 插入垂直調整enumerate*

答案2

感謝您的所有回答。然而,最後,在費盡心思嘗試讓所有不同的方法發揮作用之後,我只是決定用 Python 編寫自己的腳本,根據包含在其中的某些命令註釋掉(和取消註釋)行。

因此,例如,如果我的原始文件如下所示:

\begin{document}
    \begin{enumerate}
        \item 
        This is a test.
        \item 
        This is another test.
        \item 
        This is yet another test.
    \end{enumerate}
\end{document} 

我想要關閉所有這些枚舉內容的選項,我只需在每行末尾添加一個小命令,我希望能夠註解掉它,如下所示:

\begin{document}
    \begin{enumerate} %@
        \item %@
        This is a test.
        \item %@
        This is another test.
        \item %@
        This is yet another test.
    \end{enumerate} %@
\end{document}

讓我的腳本發揮其魔力,將文件變成如下所示:

\begin{document}
%@    \begin{enumerate}
%@      \item
        This is a test.
%@      \item
        This is another test.
%@      \item
        This is yet another test.
%@  \end{enumerate}
\end{document}

這可能看起來很麻煩,但對我來說,這絕對是最好的解決方案,因為我知道發生的一切。

如果有人想使用或查看這個用 Python 寫的程序,可以在CTAN

答案3

使用 acc 定義一個新環境enumitem。根據您的需求:

\documentclass{article}
\usepackage{enumitem}

\newlist{myenum}{enumerate*}{3}
\setlist[myenum]{leftmargin=!,label=\arabic*.}

\begin{document}
\begin{myenum}
\item This is a test.
\item This is another test.
\item This is yet another test.
  \begin{myenum}[label=]
  \item Second level This is a test.
  \item This is another test.
  \item This is yet another test.
    \begin{myenum}[label=\roman*.]
    \item Third level This is a test.
    \item This is another test.
    \item This is yet another test.
    \end{myenum}
  \end{myenum}
\end{myenum}
\end{document}

答案4

這幾乎可以完成您的用例中的工作。對於更高級的枚舉環境或選項,它可能不穩健 - 它不會按照您的意願處理嵌套。

在此輸入影像描述

\documentclass{article}

\usepackage{etoolbox}
\newtoggle{killenumerate}

\toggletrue{killenumerate}

\newenvironment{myenumerate}
{\iftoggle{killenumerate}
  {\renewcommand{\item}{}}
  {\begin{enumerate}}
}
{\iftoggle{killenumerate}
  {}
  {\end{enumerate}}
}

\begin{document}

Enumeration switched off selectively in preamble.

\begin{myenumerate}
\item One item in myenumerate environment
\item Two
\item Three
\end{myenumerate}

\begin{enumerate}
\item One item in ordinary enumerate
\item Two
\item Three
\end{enumerate}

Enumeration switched on for the rest of the document.

\togglefalse{killenumerate}

\begin{myenumerate}
\item One item in myenumerate environment
\item 
\item Two
\item Three
\end{myenumerate}

\end{document}

相關內容