[Tex/LaTex] How to make itemize/enumerate/description environment robust to missing \item elements

lists

Is there any way to make LaTeX tolerate empty itemize/enumerate (i.e. no \item)?

Specifically, I would welcome any package/elegant hack that would gracefully "cancel" a list if no elements are found (instead of firing up a Latex Error).

Before jumping to the obvious answer (which I expect would be something along the lines of "don't use itemize if you don't have items to put in" ;)) please consider my use-case:

  • I want to format remotely stored elements (say, bibliographic items), which I can only access as an XML file.

    <elems>
      <bibelem><title>Cool result 1</title><author>joe</author></bibelem>
      <bibelem><title>Cool result 2</title><author>jim</author></bibelem>
    </elems>
    
  • I have a python script that downloads/parses the XML and outputs its content in a file bib.tex as a list of Latex macros-decorated data

    \BibElem{Cool result 1}{joe}
    \BibElem{Cool result 2}{jim}
    
  • Now, I want to grant the end-user complete control over the styling of her/his produced document, so the intended usage within Latex is something like

    \documentclass{article}
    \begin{document}
      \newcommand{\BibElem}[2]{\item {\em #1} by {#2}}
      \begin{itemize}
        \input{bib.tex}
      \end{itemize}
    \end{document}
    

The rationale behind this design choice is that the user should be restricted as little as possible in her/his styling choices. For instance, the user may either want to display the bibelems within a list as shown in the example, or may prefer a fancy-formatted tabular. On the other hand, the user should design the latex document once, and should be able to run it regardless of the amount of data produced by the source.

Therefore, the python script cannot make any assumption regarding the context in which its latex product will be included, and it should be up to the user to deal with empty bibliographies, i.e. with empty lists. How could one possibly do that?

Update: Werner proposed a nice solution that redefines the list environment to make Latex oblivious of the fact he is currently in a list. However, this only seems to work if the empty list is the last list in the document, as revealed by the following mwa:

\documentclass{article}
\makeatletter
\newenvironment{myitemize}
  {\itemize}
  {\@newlistfalse\enditemize}
\makeatother
\begin{document}
  % Empty Itemize
  \begin{myitemize}
  \end{myitemize}
  % Next non-nested itemize environment (can be itemize/enumerate/description) 
  \begin{myitemize}
    \item Arghh!
  \end{myitemize}
\end{document}

Best Answer

You should create your own myitemize environment that allows you to do this:

enter image description here

\documentclass{article}
\makeatletter
\newenvironment{myitemize}
  {\itemize\@newlistfalse}% \begin{myitemize}
  {\enditemize}% \end{myitemize}
\makeatother
\begin{document}
\noindent Here is some text.
\begin{myitemize}% A list with items
  \item An item
  \item Another item
\end{myitemize}
Here is some more text.
\begin{myitemize}% An empty list
\end{myitemize}
Here is a final piece of text.
\end{document}

The myitemize environment is exactly the same as itemize, except that it (re)sets the boolean condition \@newlistfalse. This is set to true (\@newlisttrue) at the start of a regular list (itemize, enumerate, ...), which causes the error when no \items are used.

This may cause problems with nested lists, although I don't think this fits your use case.