Create an enumerate environment that can be turned off

This question led to a new package:
comment_io

I would like to create a document using enumerate environments that I then could switch on and off before compiling.

For example, let's say that I have a section that looks like this:

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


This would, under normal circumstances, compile as an enumerated list with three items, like so:

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


I would like a way for this specific enumerate environment to vanish so that the compiled output looked something like this:

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


In addition, I would like to be able to do this globally for different environments (that is, not having to edit each instance of the environment in question at the local level).

However, I would still want to be able to have other enumerate environments in my documents that stayed put (even if they were nested within the vanishing environment).

Context: I would like to write my texts using the Tractatus style of placing each section in a enumerated hierarchy. However, I would also like to be able to remove this enumeration and just show the text as a regular article. Finally, in this regular article, I would like to have the ability to show enumerated lists (that then wouldn't be a part of the Tractatus enumeration).

• What kind of interface are you looking for that would provide this? Different environment name would be the obvious choice. – Werner May 2 '15 at 15:59
• @Werner That would most certainly do it. I'm just not sure how to turn any specific enumerate environment off. – Speldosa May 2 '15 at 16:05
• Why can't you use \section for the sections and have a switch to toggle that? – cfr May 3 '15 at 19:15
• @cfr I'm using sections as well, independent of the enumeration environment. – Speldosa May 4 '15 at 9:54
• @Speldosa Well \subsection, then, or \subsubsection or whatever. – cfr May 4 '15 at 13:57

For allowing nested enumerate (or other lists) environments to still function, you need to capture \item if you're going to make it a no-op inside this new on-off enumerate. For this we store its definition in the preamble and resture it for use at the beginning of every enuemrate using etoolbox's \AtBeginEnvironment. If you plan on nesting other lists as well, you need to restore \item in a similar manner.

Below I've defined enumerate* which is turned off during compilation, while it still supports nested enumerates:

\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* redefined \item to do nothing (be \relax). As such, it'll completely ignore any optional argument, if you have any. That is, \item[<stuff>] will default to [<stuff>]. That can be adjusted to gobble the optional argument, if needed.

One can also adjust the vertical adjustment inserted by enumerate*.

• This seems to do the trick! However, what's the best way of turing it off if I actually want the items to be enumerated? – Speldosa May 2 '15 at 18:02
• @Speldosa: You can use \newenvironment{enumerate*}{\begin{enumerate}}{\end{enumerate}}. – Werner May 4 '15 at 12:22

Thank you for all your answers. However, in the end, after having pulled out all my hair from trying to get all the different methods to work, I simply resolved to writing my own script in Python which comments out (and uncomment) lines depending on certain commands that are included in the file.

So, for example, if my original file looks like this:

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


and I want the option to turn all these enumerate stuff off, I simply add a small command to the end of each line that I would like to be able to comment out, like so:

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


and let my script do its magic, which turns the file into something that looks like this:

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


This might seem cumbersome, but for me it's the absolut best solution since I know everything that's going on.

If anybody want to use or take a look at the program, which is written in Python, it can be found on CTAN.

Define a new env with enumitem acc. to your needs:

\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}

• This doesn't seem to work if I nest other enumerate environments inside of this one. – Speldosa May 2 '15 at 17:57
• @Speldosa - I'm not sure if I understand you correctly, but I edited my answer. Maybe this helps. – Arash Esbati May 2 '15 at 19:56
• Sorry for being unclear. What I mean is that with this solution, I can't have other enumerate enumerate environment nested within the myenum environment which don't disappear. – Speldosa May 2 '15 at 20:04
• @Speldosa Why not? Or does the last edit address that? There's no reason the lists inside shouldn't be enumerate or enumerate* or whatever you prefer. – cfr May 2 '15 at 20:35
• @Speldosa Or is the problem that you want to nest non-inline lists? – cfr May 2 '15 at 20:52

The original version of this answer was based on a combination of Werner's and Arash Esbati's answers. This version is derivatively based on them in virtue of being based on the original.

Here's an example:

\documentclass{article}
\usepackage[inline]{enumitem}% enable pre-defined inline list environments
\newif\ifoptenumerate% create a toggle
\optenumeratetrue
\optenumeratefalse% comment this line to have the list
\ifoptenumerate
\newlist{optenumerate}{enumerate}{3}% if true, we want an ordinary enumerated list
\setlist[optenumerate]{label=\arabic*}% set the label
\else
\newlist{optenumerate}{enumerate*}{3}% if false, we want an inline list
\setlist[optenumerate]{label=,mode=unboxed}% with an empty label, in unboxed mode (see comments below)
\fi
\begin{document}

\begin{optenumerate}
\item First
\item Second
\begin{enumerate}% a nested, non-inline, enumerated list
\item Initial
\item Continuing
\item Final
\end{enumerate}
\item Third
\item Fourth
\begin{enumerate}% a nested, non-inline, enumerated list
\item One
\item Two
\begin{enumerate*}% a nested, inline enumerated list at a lower level
\item Beginning
\item Middle
\item Ending
\end{enumerate*}
\item Three
\item Four
\end{enumerate}
\item Fifth
\item Sixth
\end{optenumerate}

\end{document}


As given, this produces the following:

Commenting the line

\optenumeratefalse% comment this line to have the list


produces:

You just use optenumerate for the lists you want to switch on and off and enumerate or enumerate* for the ones you don't want to switch. Then uncommenting or commenting the toggle line in the preamble will switch the optenumerate environments on or off. (More-or-less, anyway.)

Note that to accommodate lists within the optenumerate environment, it is necessary to use unboxed mode when the toggle is set to false. This is what makes the nesting possible. However, it has some disadvantages. In particular, you will not be warned about things like missing \items. I would therefore recommend to always check that the code compiles with the toggle set to true since that will do more sanity-checking and help to ensure the consistency of output.

See page 9 of the manual for further details concerning the difference between boxed and unboxed modes.

• I tried to run this. Uncommenting the fifth line while removing one of the enumerate* blocks brakes it :( – Speldosa May 3 '15 at 15:44
• @Speldosa Oops. I thought I had no clue when I realised the problem. However, I just happened to find an unfamiliar option in enumitem's documentation which I think should do the trick. Could you try to see if it works now? (At least if it compiles....) – cfr May 4 '15 at 0:03
• It compiles when I comment out the fifth line, but not otherwise. – Speldosa May 4 '15 at 9:59
• @Speldosa Then I have no idea since it compiles fine here. Moreover, the current version of enumitem is from 2011 so it seems very unlikely that you have a different version from me (v3.5.2). So if precisely the code I posted doesn't compile for you but does for me, something odd is going on. What error do you get? – cfr May 4 '15 at 13:52
• @crf Strange, it worked when I tried to compile it now. So thumbs up! (That it doesn't work when I apply it to my actual document is another matter. I really hate LaTeX sometimes :P) – Speldosa May 4 '15 at 20:55

This almost does the job in your use case. It may not be robust with fancier enumerate environments or options - it does not deal with nesting as you wish.

\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}