I was wondering if there is any formal environment for specifying 'examples' through out the text. If not, which environments do you recommend?

  • 1
    What kind of examples?
    – N.N.
    Commented Jun 21, 2011 at 7:49
  • @N.N. A simple one which gives some indentation and enables a logical recognition of the example, through \label{example1} and \ref{example1}.
    – Md Oliya
    Commented Jun 21, 2011 at 8:00

3 Answers 3


You could use the amsthm package. As per their recommendations, which is what most people use, you should define the environment as


and than use is as

This is the example.

Note however, that the above definition will give the examples its own counter. If you already have theorems and definitions using the thm-counter, you might want to consider using


since it can be very confusing for the reader if you have Theorem 3, Lemma 3, Definition 3 and Example 3 on different pages.

  • Thank you @Carsten. but loading amsthm errs with "Command \proof already defined." I am using Springer llncs.cls. Any advice?
    – Md Oliya
    Commented Jun 21, 2011 at 8:34
  • 4
    @Md Oliya In that case you do not need the amsthm package. llncs provides this feature itself. According to section 8 of its documentation you should simply use \begin{example} <text> \end{example}. For using the same counter for Theorems, Definitions etc load the class with the option envcountsame. Commented Jun 21, 2011 at 8:44
  • Thanx @Carsten. the amsthm package works great. just a question: the counters are like section_number.counter (e.g III.1). how can i use a simple counter? i.e. no section numbers
    – Md Oliya
    Commented Jun 24, 2011 at 3:06
  • 1
    @Md Oliya use \newtheorem{exmp}{Example} without the [section] Commented Jun 24, 2011 at 7:37
  • ---------Great!
    – Md Oliya
    Commented Jun 25, 2011 at 6:40

Updated Answer 2016-03-04

Here is an alternative using tcolorbox. This solution is robust in that it can handle many situations: one page, partial page spans, and multiple page spans. I wrapped everything in a \NewDocumentEnvironment macro from xparse because it makes it easy to recognize what the input should be.

Caveat for those using XeLaTeX and listings 2016-07-05

Due to an unknown problem (see Why is text turning white (possible scope leak?) after page break and listings + tcolorbox + xelatex?), you should reapply the color "black" explicitly after each "testexample" environment to ensure that your text is visible as expected under all page break conditions. Fix is here: https://tex.stackexchange.com/a/308854/13552

\AfterEndEnvironment{testexample}{\color{black}} % Bugfix for unknown culprit. When tcolorbox wraps listings compiled with xelatex, subsequential text turns white https://tex.stackexchange.com/questions/308672/why-is-text-turning-white-possible-scope-leak-after-page-break-and-listings

Example Syntax

\begin{testexample}[<optional title>]
<content of example>


If desired, examples can be numbered by replacing the line

title={\exampletext: #1},% use \thetcbcounter to access the testexample counter text


title={\exampletext\ \thetcbcounter: #1},% use \thetcbcounter to access the testexample counter text


\def\exampletext{Example} % If English

\NewDocumentEnvironment{testexample}{ O{} }
\colorlet{colexam}{red!55!black} % Global example color
\newtcolorbox[use counter=testexample]{testexamplebox}{%
    % Example Frame Start
    empty,% Empty previously set parameters
    title={\exampletext: #1},% use \thetcbcounter to access the testexample counter text
    % Attaching a box requires an overlay
    attach boxed title to top left,
       % Ensures proper line breaking in longer titles
       minipage boxed title,
    % (boxed title style requires an overlay)
    boxed title style={empty,size=minimal,toprule=0pt,top=4pt,left=3mm,overlay={}},
    before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=3mm,right=0mm,top=2pt,breakable,pad at break=0mm,
       before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
    % Handles box when it exists on one page only
    overlay unbroken={\draw[colexam,line width=.5pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
    % Handles multipage box: first page
    overlay first={\draw[colexam,line width=.5pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
    % Handles multipage box: middle page
    overlay middle={\draw[colexam,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },
    % Handles multipage box: last page
    overlay last={\draw[colexam,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },%

\begin{testexample}[Latin Text]


enter image description here

Adding figures

You probably don't want a float figure inside of this example environment, because it is a box itself.

I would probably add the caption package and implement it like this:

\captionof{figure}{Wildspitze 3,770 m (12,370 ft)}


enter image description here

Old Answer

Here is an alternative using mdframed and xparse (xparse is not absolutely necessary, but I adopted it as my main syntax for defining commands and environments due to its flexibility)


\usepackage{mdframed} % Add easy frames to paragraphs
\usepackage{lipsum} % For dummy text
\usepackage{xparse} % Add support for \NewDocumentEnvironment
\definecolor{graylight}{cmyk}{.30,0,0,.67} % define color using xcolor syntax

\newmdenv[ % Define mdframe settings and store as leftrule

\NewDocumentEnvironment{example}{O{\textbf{Example:}}} % Define example environment




enter image description here

(fontspec used because I compile with XeLaTeX)

  • Any idea why the left line will not show up for me? I tried inserting leftline=true, to no avail. However, but setting rightline=true in the setting above, I can get the line on the right to show up. (The same is true for the other two lines.) Commented Feb 25, 2016 at 11:11
  • @Shredderroy I am not sure, but I can tell you that I have since switched to tcolorbox, because I found mdframed not able to properly handle multipage spanning. Also, I know TikZ, so tcolorbox was a logical alternative and not a difficult jump to make. I updated my answer. Maybe this alternative will work for you. Commented Mar 4, 2016 at 15:23
  • @macmadness86 this looks great. Is it possible to create a version where it is possible to use a float environment like figure in the example?
    – raedma
    Commented Jun 12, 2016 at 19:12
  • Nevermind, I got it working with dropping the figure environment and using \captionof{figure}{Title} instead.
    – raedma
    Commented Jun 12, 2016 at 19:18
  • @krtek I was just going to say that. See my update. Commented Jun 12, 2016 at 19:21

You can make a simple example environment as follows:

  \textbf{Example \arabic{examplecounter}}%
  A simple example environment


Some text: \lipsum[4]

  Another example

Example~\ref{ex:another} comes after example~\ref{ex:simple}

So you have an environment which is basically just a wrapper for a quote environment which has its own counter. You can replace quote with something else that adds indentation more to your liking, but the basic idea is there.

If you want it to include the section number as part of the example (e.g. example 1.2) then that's a little more work and maybe something like amsthm or ntheorem is a better bet...

note that \refstepcounter increments the environment's counter, and also makes the environment "visible" to the referencing mechanism.

You must log in to answer this question.

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