2

I am auto-generating a LaTeX document. The input data can possibly generate empty data, so that I am left with something like this in the generated TeX source:

\begin{itemize}

\end{itemize}

This leads to lualatex complaining when compiling (of course):

Misplaced \item

Is there a way to make it ignore any empty list (no \item) silently?

For clarification: I do have a lot of itemize lists in the document and some can be empty depending on my data. I know I could filter that before generation, but that would be more cumbersome than a potential LaTeX solution.

jœ.
  • 185
  • Do you also have active itemize in your document? – Niranjan Oct 05 '19 at 12:24
  • I use custom envronments defined through the enumitem package, so the full error reads ! Package enumitem Error: Misplaced \item. in my case, but it is a general TeX problem, right? – jœ. Oct 05 '19 at 12:26

3 Answers3

1

I am writing yet another answer because I feel the strategy used for both of my answers is different fundamentally.

First of all, this one is a hack! You will have to replace all of your itemizes with newitemizes. As you have mentioned earlier, if you are automating the input, you can pre-define your program to generate newitemize and not itemize. If you do this then your problem is almost solved with something like following.

\documentclass{article}
\newenvironment{newitemize}{\begin{itemize} \item}{\end{itemize}}

\begin{document}
    \begin{newitemize}

    \end{newitemize}
\end{document}

Similarly you can form new environments for all of your list structures.

Niranjan
  • 3,435
0

If you don't have any other active itemize (or other lists) -

\documentclass{article}
\renewenvironment{empty}{}{}

\begin{document}
abcd
    \begin{empty} % This list structure was renamed to empty.

    \end{empty}

    \begin{itemize} % This list structure is still active.
        \item
    \end{itemize}
\end{document}
Niranjan
  • 3,435
  • That is a neat idea but too extreme in my case with several itemize environments around. – jœ. Oct 05 '19 at 17:53
  • Well if you have some itemize which you want to be active and some to be inactive, you are probably going to need some manual interference... – Niranjan Oct 05 '19 at 18:03
  • In that case I would have to build some logic. That is okay, I just wondered if I could make LaTex ignore it directly. Thanks for your effort though, Niranjan! – jœ. Oct 05 '19 at 21:12
  • You can rename all of your empty list structures with something like empty and keep active structures intact. This is very simple with find and replace provided by some editors. Please see the edit. – Niranjan Oct 06 '19 at 04:34
0

How about you take advantage of item[] which creates a new item without a label.

Output

\documentclass{article}
\begin{document}
  \noindent Our first list:
  \begin{itemize}
    \item First item
    \item Second item
    \item[]
    \item Fourth item
  \end{itemize}
  Our second list:
  \begin{itemize}
    \item[]
  \end{itemize}
  Our third list:   
  \begin{itemize}
    \item First of all
    \item[] Second of all
    \item Thirdly
  \end{itemize}
\end{document}
M. Al Jumaily
  • 4,035
  • 2
  • 11
  • 26
  • Thanks for your suggestion. Unfortunately that does not work in my case with added logic through the enumitem package (, between items and . after the last one). – jœ. Oct 06 '19 at 20:54
  • It would have been better for the both of us if you would have posted a minimal and complete example of what you have. Your original post used only itemize and had no mentioning of enumitem. – M. Al Jumaily Oct 06 '19 at 22:05