I have this document:


   \noindent \textbf{Example~\theexample. #1} \rmfamily}{\medskip}




The output looks like this:

However, if I remove [] (appearing after [1]) in the definition of the enviroment, I'll get

Can anyone explain to me why this is happening and what those braces [] after [1] actually do, please?

  • 4
    The \newenvironment{example}[1][]{}{} means optional argument, enclosed in [ ], i.e. \begin{example}[foo]...\end{foo} , if you remove [] from the definition, [1] means that the environment expects an argument, it grabs the first token it gets, i.e. the T from Test: \begin{example} Test\end{example}. You should read some introduction about macro and/or environments and their arguments with {} and [] ;-) – user31729 Jul 20 '17 at 20:45
  • Just a comment aside: why don't you use a theorem-like structure? – Bernard Jul 20 '17 at 20:45
  • @ChristianHupfer I have read this. And it's still confusing. For example, their "boxed" enviroment doesn't have [] after [1] and it seems it's working well. – Patrik Bak Jul 20 '17 at 21:06
  • @PatrikBak: The linked example does have multiple versions of boxed environments. The first boxed does not have a [1] at all -- it does not expect an argument, the second example is defined with [1] and uses Title of the Box as argument, which is centered outside the box frame. Your code usage above should read like this: \begin{example}[My nice example] Test \end{example}, if you persist on the [1][] usage and it should read \begin{example}{My nice example} Test\end{example} in the [1] case. I doubt that you want Test to be the title of your example environment – user31729 Jul 20 '17 at 21:09

There's nothing strange about the environment definition really -- the strangeness is the consequence of disregarding LaTeX macro/environment call syntax.

Using standard LaTeX macros like \newenvironment there are basically four ways of defining the example environment:

  • Use no argument at all, i.e. \newenvironment{foo}{start code}{end code}
  • Use a mandatory argument, that must be enclosed with a {} pair:

    \newenvironment{foobar}[1]{start code}{end code}

  • Use at least one optional argument in definition and decide on environment usage whether the argument is given or not:

    \newenvironment{foobarother}[1][]{start code}{end code}

    The empty [] means empty optional argument value, i.e. if \begin{foobarother} is used without [] the #1 is empty, otherwise specify it as \begin{foobarother}[Foobar].

If a default title should be used, say 'My nice default title', then use \newenvironment{foobarother}[1][My nice default title]{start code}{end code} which applies My nice default title if there's no optional argument specified.

  • Use optional and mandatory arguments, i.e. \newenvironment{yetanotherfoobar}[2][]{start code}{end code} with \begin{yetanotherfoobar}[Some other title]{Yet another foobar}, where [...] is optional

More sophisticated ways are possible with \NewEnviron from environ package and \NewDocumentEnvironment from xparse (for example). Please note that #1 etc. can't be used in the end code of an environment defined with \newenvironment, but \NewDocumentEnvironment and \NewEnviron allow usage of the parameters in the end code section of the environment.


  \noindent \textbf{Example~\theexample.\notblank{#1}{~#1}{}} \rmfamily}{\medskip}

   \noindent \textbf{Example~\theexample. #1} \rmfamily}{\medskip}

   \noindent \textbf{Example~\theexample} \rmfamily}{\medskip}


\begin{examplewithoptarg}% Empty optional argument

% Non-empty optional argument, title is My nice title
\begin{examplewithoptarg}[My nice title] 

% Correct syntax for example with arg: Use `{Foo}` as argument and `Foo` as title, `Test` is the environment body content

% 'Wrong' usage -- grabs F, regards it as title and then displays oo Test
Foo Test

% No argument -- this has the standard title `Example \theexample` by definition


enter image description here


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.