# mdframed causes “too many floats” error

I defined a new environemnt examples for a book I am writing. I would like examples to be with a framed grey box, and use the following construct

%----------------------------------------------------------------------------
% EXAMPLES
%----------------------------------------------------------------------------
\newlistof[chapter]{examples}{exp}{\listexamples}
\newcommand{\bex}[1]{%
\begin{mdframed}[linewidth=2, leftmargin=0, rightmargin=0, backgroundcolor=gray, linecolor=black, splittopskip=\topskip, skipbelow=\baselineskip, skipabove=\baselineskip]
\refstepcounter{examples}
\par\noindent\sqrblt\underline{\textbf{Example \theexamples. #1}}
\addcontentsline{exp}{examples}
{\protect\numberline{\theexamples}#1}\par}
\cftsetindents{examples}{0em}{2em} % for example numbers which are greater than 9
% Solution
\newcommand{\sol}{\begin{center}\underline{{\bf Solution:}}\end{center}}
% End of Example
\newcommand{\eex}{
\noindent \hbox{~~}\hfill \hbox{~~} \sqrblt
\end{mdframed}
}


If I activate the mdframed, I get "not in outer par mode". If I comment the mdframed part, all is well

-
Welcome to TeX.sx! Please post a minimal working example of your code illustrating the problem. –  Gonzalo Medina Oct 1 '11 at 21:00
If I try your commands, I get what seems to be expected. What's the real error you get? The title says "too many floats", the questions refers to "not in outer par mode" –  egreg Oct 1 '11 at 22:23

## 1 Answer

Here is a solution which works.

Your feedback is welcome.

\setcounter{errorcontextlines}{999}
\documentclass[openany]{book}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage[framemethod=default]{mdframed}
\usepackage{tocloft}
%----------------------------------------------------------------------------
% EXAMPLES
%----------------------------------------------------------------------------
\newlistof[chapter]{examples}{exp}{\listexamples}
\cftsetindents{examples}{0em}{2em}
\newcommand*\listexamples{List of Examples}

\newcommand{\sqrblt}{\rule{0.1in}{0.1in}\hspace{.05in}}

\mdfdefinestyle{example}{%
linewidth=2,
leftmargin=0,
rightmargin=0,
backgroundcolor=gray,
linecolor=black,
splittopskip=\topskip,
skipbelow=\baselineskip,
skipabove=\baselineskip,
frametitlefont=\bfseries,}

\newenvironment{examplesenv}[1]%
{\refstepcounter{examples}%
\addcontentsline{exp}{examples}{\protect\numberline{\theexamples}#1}
\begin{mdframed}[style=example,frametitle={\sqrblt Example \theexamples.~#1}]
}{\mbox{}\hspace{0.2in}\mbox{}\hfill\llap{\sqrblt}\end{mdframed}}

\let\bex\examplesenv
\let\eex\endexamplesenv

\usepackage{showframe,lipsum}
\begin{document}
\listofexamples
\chapter{foo}
\lipsum[1]

\begin{examplesenv}{Example one}
\lipsum[1]
\end{examplesenv}
\clearpage
\bex{Example two}
\lipsum[1]
\eex
\end{document}


EDIT

In a comment zampano wrote he insert a floating environment. This can't work. The explanation if given in the UK List of TeX Frequently Asked Questions

This is of course a contradiction: figure and table are designed to float, and will always have the potential to appear away from where you asked for them. Therefore you have to find a means of getting the caption and other effects without allowing the figure or table to float.

To use captions in this contents I prefer the method captionof (provided by the KOMA-script or by the packages caption or capt-of)

\begin{center}
\includegraphics{picture}
\captionof{figure}{foo}
\end{center}


-
Sure, it works, but the moment you insert inside the example a figure (\begin{figure} \includegraphics etc...) it fails with not in outer mode immediately –  zampano Oct 3 '11 at 12:45
@zampano: This is an information which you forgot to present. You can't insert float-objects. If you want to use the caption you can use the center-environment in combination with captionof. –  Marco Daniel Oct 3 '11 at 13:05