4

If one has a itemize environment, with no items inside :

\begin{itemize}
\end{itemize}

the compilation will complain "perhaps a missing \item". I would like to modify the environment itemize (or enumerate) such that in case there is no \item, the compilation doest NOT fail, and simply display nothing.

EDIT I think that by trying to extract a MWE from my actual problem, i missed the point. So, i reformulated the question here.

Loic Rosnay
  • 8,167

3 Answers3

5

You can deactivate it as follows, though I'm not really sure why anyone would want to do this!

\documentclass{article}
\makeatletter
\let\@noitemerr\relax
\makeatother
\begin{document}
Some text.
\begin{itemize}
\end{itemize}
More text.
\end{document}

If you need to reactivate it later, you need to save the original definition.

\documentclass{article}
\makeatletter
\let\@oldnoitemerr\@noitemerr %Save the command definition                      
\newcommand\noitemerroroff{\let\@noitemerr\relax}
\newcommand\noitemerroron{\let\@noitemerr\@oldnoitemerr}
\makeatother
\begin{document}
Some text.
\noitemerroroff
\begin{itemize}
\end{itemize}
More text.
%                                                                               
\noitemerroron
\begin{itemize}
\end{itemize}
\end{document}
Ian Thompson
  • 43,767
1

These environments are part of the LaTeX kernel, and thus not easily changeable or tweakable without delving into the implementation. (See Ian's answer.) One solution would be to define your own command and use the ifthen package:

\usepackage{ifthen}
\newcommand{\safeitemizecmd}[1]{%
  \ifthenelse{\equal{#1}{}}{}{\begin{itemize}#1\end{itemize}}}%
}

Note that this solution expands the parameter #1 twice, just for checking if it's empty. You can avoid this by using \expandonce from the etoolbox package:

\usepackage{ifthen}
\usepackage{etoolbox}
\newcommand{\safeitemizecmd}[1]{%
  \ifthenelse{\equal{\expandonce{#1}}{}}{}{\begin{itemize}#1\end{itemize}}}%
}

Then, \safeitemizecmd{\item ...} would typeset an itemized environment, and \safeitemizecmd{} would typeset nothing.

If you want to use an environment, use the command created above:

\newenvironment{safeitemize}{\safeitemizecmd\bgroup}{\egroup}

The \bgroup and \egroup commands are essentially equivalent to braces.

krlmlr
  • 12,530
1

what do you actually want to happen?

Given

xxx

aaa\begin{itemize}\end{itemize}bbb

yyy

then do you want to get a middle paragraph of aaabbb as if the empty environment wasn't there? Do you want (just) a paragraph break, do you want a paragraph break and the display spacing that an itemize environment normally gets above and below?, or do you want (the distinctly odd) output that you actually get from LaTeX, but without the error message?

All those things are (probably) possible but some are easier than others.

David Carlisle
  • 757,742
  • I'm realizing, that extracting a MWE from my original code, i missed the essential point of my problem. So i have to reformulate the question. But yes, i want to get aaabbb. And for this, the solution of user946850 could work... – Loic Rosnay Feb 07 '12 at 21:24