16

The following does not compile because of the [ after the \item. I presume it is because LaTeX expects a ] as per the \item[option] syntax. However, I just want to insert the [ character into the text as the first character of the enumerated item.

\documentclass[oneside,12pt]{article}
\begin{document}
\begin{enumerate}
\item before
\item [ random text
\item after
\end{enumerate}
\end{document}

I would like LaTeX to generate a document:

before

[ random text

after

One way I've come up with is to put a \ in front of the [, however I'd appreciate thoughts on the least invasive way to do this.

1
  • What you've done is initializing the displaymath environment.
    – Crowley
    May 24, 2010 at 10:33

2 Answers 2

24

Just like whenever you want to put a special character (such as a space, or something else) after a command, you can use the {} to suppress the next character.

\item{}[your random text
2
  • 11
    Another option is to insert some other command, such as \relax (which does nothing at all) between \item and the bracket. Feb 5, 2009 at 7:55
  • 2
    @JouniK.Seppänen I don't like the {} tweak, because it looks like that it is an argument of \item, which is not true. So I agree with Jouni that \relax is a good option.
    – yo'
    Feb 5, 2012 at 21:50
1

\item looks ahead for the [ character in its definition using \@ifnextchar [:

\def\item{%
  \@inmatherr\item
  \@ifnextchar [\@item{\@noitemargtrue \@item[\@itemlabel]}}

This is done to enable an optional argument for \item. In that sense, using anything other than [ before using [ would suffice.

If you never use the optional argument for \item, you could remove it from its definition and use something different like \Item. That way you can use [ without worrying about it:

enter image description here

\documentclass{article}
\makeatletter
% Remove optional argument from \item
\newcommand{\Item}{\@inmatherr\item\@noitemargtrue\@item[\@itemlabel]}
\makeatother
\begin{document}
\begin{enumerate}
  \Item before
  \Item [ random text
  \Item after
\end{enumerate}
\end{document}

The \item renewal removes the conditional "peeking" for a [ in the input stream, setting the default optional argument to be the regular label. As mentioned, if you never use it in any of your lists, this should be no problem.

3
  • 8
    Beware, for this to be safe it's not enough that you never use \item[]. You must also not use any commands or environments that use this internally. latex.ltx uses this for theorems and bibliography items for example, and probably several packages do likewise. Feb 5, 2012 at 11:18
  • 2
    An alternative would be to create an \Item command that does not have optional arguments, and use that. Feb 5, 2012 at 13:18
  • 2
    Sorry, but this is bad advice when a simple {} is enough. Feb 5, 2012 at 14:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .