10

I had a case where I wanted do start a text with a square bracket inside an environment. It was interpreted as an optional argument. My workaround was to insert a {} to separate that. Is that the way to go or is there something better?

  • It would help if you showed us some code. Can you provide a minimal working example, please? It sounds reasonable to add the curly brackets. But saying that currently involves committing to a very general claim and perhaps it is not always the best option. – cfr Nov 26 '15 at 14:17
  • 2
    If it was interpreted as optional argument the environment has been defined to look for such an optional argument, otherwise it would be typeset as [...]. Use {} between \begin{foo}{}[your bracket stuff] – user31729 Nov 26 '15 at 14:22
  • Depends on how the environment is defined: simetimes it suffices to place the [...] on the next line or with a space before. What should work in any case is \begin{env}\relax – clemens Nov 26 '15 at 14:34
  • 2
    is your environment math mode or text, in text {} is Ok, but don't use that in math mode, it will affect the spacing. – David Carlisle Nov 26 '15 at 22:01
10

A delimiter is needed to convince the environment macro (or any other macro) that the argument list is finished and all other stuff is not to be processed by the macro itself.

This delimiter is a {}, empty space is ignored. See the examples where foobar uses [...] as normal text, whereas foobaropt expects an optional argument and interpretes it that way unless {} is inserted between {foobaropt} and [...].

\documentclass{article}


\newenvironment{foobar}{%
Not interpreted as opt argument
}{}


\newenvironment{foobaropt}[1][]{%
Catched as opt argument ``#1"
}{}


\begin{document}

\begin{foobar}[Some text]
\end{foobar}

\begin{foobaropt}[Some text]
\end{foobaropt}


\begin{foobaropt}{}[Some text]
\end{foobaropt}


\end{document}
  • 1
    Or \relax, instead of {} – egreg Nov 26 '15 at 19:05
  • 1
    @egreg: Forgot about \relax as another possibility. Thanks – user31729 Nov 26 '15 at 21:00
7

This is very similar to what Leslie Lamport recommends in his LaTeX: A Document Preparation System, except he advises that you enclose the entire [ ... ] in braces. It really comes to very much the same thing, by having {[ ... ]}, you are telling LaTeX that [ ... ] is to be taken separately. I suppose with {} you are almost providing insulation, if you see what I mean.

Quoting from the relevant section:

The following commands taken an optional last argument:

\\ \item \linebreak \pagebreak \nolinebreak \nopagebreak \newcounter \newtheorem \twocolumn \suppressfloats

If that argument is missing and the next nonspace character in the text is a [, then LaTeX will mistake this [ for the beginning of an optional argument. Enclosing the [ in braces prevents this mistake.

He then provides the following example:

... \begin{itemize}
  \item {[This is an aside.]} This is ...

A ] within the optional argument of an \item command must be enclosed in braces to prevent its being mistaken for the ] that marks the end of the argument.

...

Some commands, including \\, have a *-form that is obtained by typing a * right after the command name. If a * is the first nonspace character following a command like \\, then it should be enclosed in braces; otherwise, LaTeX will mistake the \\ and * for a \\* command.

The same, of course, goes for environments which can take optional arguments. So you might have:

\begin{foo}
  {[He]} said some unkind things.
\end{foo}

If the foo environment could take an optional argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.