I want to define a custom environment example. This is not a problem at all, the only thing I humble upon is the caption (short title below the element) and its listing in the table of contents or rather "List of examples". I want to get a caption like this:

Example <normal-counter>: My caption

And in the appendix a "list of examples". In other words, copy the entire stuff for figures to my example environment. I'm sure that there is a solution and that there is also an answer within tex SE, but I'm not able to find it :-( Thank you!

PS: I always prefer avoiding additional packages :-)

  • With caption you mean something like a header, not a lengthy description what is contained in the example, being something like a section title? – user31729 Jun 10 '14 at 7:48
  • I modified my question. Yes, I mean a header. The same thing as the caption of a figure, similar to "Figure x: UML diagram depicting packages" – strauberry Jun 10 '14 at 7:56

Here's a solution that does the following:

  • it starts a new file, \jobname.exs, that stores the titles of your examples; this uses the command \@starttoc
  • it creates a new environment, example, that can be used either as

    \begin{example}{goes into the list of examples}

    in which case the argument is written to the \jobname.exs file; alternatively, you can use

    \begin{example}[optional caption]{goes into the list of examples}

    in which case the optional caption will be written to \jobname.exs

You requested that no packages be used, so I have used a simple list environment to help with the definition of the look of the example environment (see Definitive guide to trivlists) and you'll notice that there is a check to see if #1 is empty, using \ifx\\#1\\ as detailed in Check for empty macro argument and the links within.

The example environment could easily be tweaked if you change your mind later and want to load a package to help with it, e.g ntheorem, amsthm, mdframed, tcolorbox, etc, but for the moment, here's how it looks:


Here's the complete code - compile it twice with pdflatex, or otherwise just run it once with arara which does the double compilation for you.

% arara: pdflatex
% arara: pdflatex
\usepackage{lipsum}% just to generate text

% this sets up \jobname.exs which will store the 
% contentslines added on each example or examples
\newcommand\listexamplename{List of Examples}

            \ifx\\#1\\ % if #1 is empty
            \begin{list}{}{% options
        \setlength{\leftmargin}{0mm}%     leftmargin
        \parsep\parskip%                  space between paragraphs within an item
        \setlength{\itemsep}{-\parsep}%   space between items
   \item {\bfseries Example \theexample: ~#2}


\begin{example}{goes into the list of examples}

\begin{example}[optional caption]{goes into the list of examples}



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.