A (simple) LaTeX Example Environment

This week again, a little LaTeX. As you know, I’m putting the finishing touches to a manuscript I’ve been working on for a little bit more than a year now. Those “finishing touches” always start simple, then end up asking for quite a bit more LaTeX programming that I’d like.


This week, I worked quite a bit to repair my “example” environment that include example number, extra margin, and a different colored box around the text. This helps locate and distinguish examples from normal text. But my first version had little problems. Like equations centered not on the example text but still relative to the whole page. Bummer. Let’s see how I fixed all that.

In my first version of the environment, I used mdframed to create the shaded box and merely set \leftskip to indent the paragraphs. This works properly with text, but not with figures and equations that remain centered relative to the page. Reading more carefully the mdframed documentation, I discovered that I could use the inner margins to constrain not only the text but also the figures and equations.

You will need the following packages to implement my solution:

\usepackage[framemethod=tikz]{mdframed} % for colored backgrounds
\newcommand{\halmos}{$\square$} % makes a box at the end

And these definitions:

\setlength{\framedinnerleftmargin}{\widthof{Theoreme 10.10.10}+2em}
\setlength{\framedreversedinnerleftmargin}{\widthof{Theoreme 10.10.10}+1em}

% first argument: label in upper left corner,
% second argument: background color
}% begin code
{\hskip 0pt\\\hspace*{\fill}\halmos{}\end{mdframed}\vspace{1em}} % end code

\newenvironment{summary}[0]{\begin{center}\begin{minipage}[c]{\summarywidth}\begin{spacing}{0.9}\footnotesize} % begin code
{\end{spacing}\end{minipage}\end{center}} % end code

This solution assumes that the widest label is “theoreme 10.10.10”, and everything is measured according to this. So basically what this does is to create a box with an inner left margin wide enough to accommodate the longest label, and constrain the rest of the text in what’s left of the page.

That merely creates a box, with a given label and background color. That’s still not quite enough just yet. I wanted example numbers to increment, follow section numberings, etc. For this, you will need to create and use a counter:


% optional! if you want it to start at zero
% with every new chapter/section/etc.

{\refstepcounter{example}\vspace{1em plus 1em}\begin{boxedtext}{Exemple \theexample.}{blue!7}}%\setlength{\parskip}{0em}}
{\end{boxedtext}\vspace{-1em plus 1em}}

{\vspace{1em plus 1em}\begin{boxedtext}{Exemple.}{blue!7}}
{\end{boxedtext}\vspace{-1em plus 1em}}

The example* environment does what you expect: an non-numbered example.

* *

The nice thing about mdframed versus, say, minipage, is that it splits more or less gracefully across pages:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: