Boxed text between pages

I've made boxed text into a manuscript using tikz by following the example in texamples, it works like a charm as long as the box is short enough to fit into a page.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,snakes}
\usepackage{amsmath,amssymb}
\begin{document}

% Define box and box title style
\tikzstyle{mybox} = [draw=red, fill=blue!20, very thick,
rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{fancytitle} =[fill=red, text=white]

\begin{tikzpicture}
\node [mybox] (box){%
\begin{minipage}{0.50\textwidth}
To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
\end{minipage}
};
\node[fancytitle, right=10pt] at (box.north west) {A fancy title};
\node[fancytitle, rounded corners] at (box.east) {$\clubsuit$};
\end{tikzpicture}%
%
\end{document}

Questions

Is there a way to extend the code so that the box break (automatically) through pages?

I just found the mdframed package, How could I combine the two packages?

• Could you mock up a simple document showing the situation you have, and post it here, to give a starting point? May 22 '13 at 14:15
• And for boxes spanning multiple pages, where should the clover (\clubsuit) appear? May 22 '13 at 16:08
• Please see my updated answer for the inclusion of a counter. May 24 '13 at 16:26
• For information. Many thanks. Faouzi Bellalouna tex.stackexchange.com/questions/165088/… Mar 12 '14 at 10:04

1st version: general box with mdframed.

You don't need to combine both packages, just load mdframed with framemethod=TikZ and look at the example 5 in mdframed-example-tikz.pdf. I still don't completely understand how it works, but after some trial and error test I produced next code.

\documentclass{article}
\usepackage{tikz}
%\usetikzlibrary{shapes}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage[framemethod=TikZ]{mdframed}

\mdfsetup{skipabove=\topskip,skipbelow=\topskip}

\tikzstyle{titregris} =
[draw=red, thick, fill=red,%
text=white, rectangle,
right,minimum height=.7cm]

\newcounter{exercise}

\renewcommand*\theexercise{Exercise~n\arabic{exercise}}

\makeatletter
\def\mdf@@exercisepoints{}
\define@key{mdf}{exercisepoints}{
\def\mdf@exercisepoints{#1}
}
\def\mdf@@myboxedtitle{}
\define@key{mdf}{myboxedtitle}{
\def\mdf@myboxedtitle{#1}
}
\mdfdefinestyle{exercisestyle}{%
outerlinewidth=1em,%
outerlinecolor=white,%
leftmargin=-1em,%
rightmargin=-1em,%
middlelinewidth=1.2pt,%
roundcorner=5pt,%
linecolor=red,%
apptotikzsetting={\tikzset{mdfbackground/.append style={blue!30}}},
innertopmargin=1.2\baselineskip,
skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
skipbelow={-1em},
needspace=3\baselineskip,
frametitlefont=\sffamily\bfseries,
settings={\global\stepcounter{exercise}},
singleextra={%
\node[titregris,xshift=1cm] at (P-|O) %
{~\mdf@frametitlefont{\mdf@myboxedtitle}~};
},%
firstextra={%
\node[titregris,xshift=1cm] at (P-|O) %
{~\mdf@frametitlefont{\mdf@myboxedtitle}~};
},
}
\makeatother

\begin{document}

\lipsum[1-4]

\begin{mdframed}[style=exercisestyle,myboxedtitle={Hello}]
To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
\end{mdframed}

\end{document}

The result is: 2nd version: exercise box with mdframed.

As I've told, previous code was taken from an example at mdframed documentation. This example is supposed to be used for exercises and I didn't change original names. Now, if you want to type exercises with this format we can come back to the original code, clean it a little bit and obtain:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage[framemethod=TikZ]{mdframed}

\tikzstyle{titlered} =
[draw=red, thick, fill=red,%
text=white, rectangle,
right, minimum height=.7cm]

\newcounter{exercise}

\renewcommand*\theexercise{Exercise~\arabic{exercise}}

\makeatletter
\mdfdefinestyle{exercisestyle}{%
outerlinewidth=1em,%
outerlinecolor=white,%
leftmargin=-1em,%
rightmargin=-1em,%
middlelinewidth=1.2pt,%
roundcorner=5pt,%
linecolor=red,%
backgroundcolor=blue!20,
innertopmargin=1.2\baselineskip,
skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
skipbelow={-1em},
needspace=3\baselineskip,
frametitlefont=\sffamily\bfseries,
settings={\global\stepcounter{exercise}},
singleextra={%
\node[titlered,xshift=1cm] at (P-|O) %
{~\mdf@frametitlefont{\theexercise}~};},%
firstextra={%
\node[titlered,xshift=1cm] at (P-|O) %
{~\mdf@frametitlefont{\theexercise}~};},
}
\makeatother

\newcommand{\SomeText}{%
To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
}

\newenvironment{MyExercise}%
{\begin{mdframed}[style=exercisestyle]}{\end{mdframed}}

\begin{document}
\begin{MyExercise}
\SomeText

\SomeText
\end{MyExercise}

\begin{MyExercise}
\SomeText

\SomeText

\SomeText
\end{MyExercise}
\end{document} 3rd version: general and exercise box with tcolorbox.

Just for completion, I'd like to add a solution made with tcolorbox package. With recent versions it's possible to make breakable framed boxes with it.

This package provides several libraries to modify it's default behavior, we will need three of them:

1. breakable: to allow boxes to automatically break between pages.
2. skins: to add some overlays over regular boxes.
3. hooks: to be able of using some special hooks in our code.

Next code includes some comments to explain how to use it.

\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage[skins,breakable,hooks]{tcolorbox}

\newcounter{myexercise}

% Definition of some styles and parameters for
% tcolorboxes
\tcbset{%
% Title boxes will be added with overlays
fancytitle/.code={%
\ifstrempty{#1}{}{\pgfkeysalso{%
top=\baselineskip,
overlay unbroken and first app = {%
\node[rectangle, draw=red, fill=red,
anchor=west,font=\color{white}]
at ([xshift=5mm]frame.north west)%
{\strut#1};},
enlarge top by = \baselineskip,
enlarge top at break by= 0mm,
}}},
% This is our boxes style
fancybox/.style 2 args = {%
breakable, enhanced, colback=blue!20,
colframe=red, width=#1,
fancybox/.default = {.5\textwidth}{}%
}

% This command creates a new environment called
% FancyBox which uses previously defined fancybox
% style
\newtcolorbox{FancyBox}[.5\textwidth]{%
fancybox={#1}{#2}}

% This command creates a new environment called
% FancyExercise which uses previously defined
% fancybox style with an automatic enumerated
% title.
\newtcolorbox{FancyExercise}[.5\textwidth]{%
code={\stepcounter{myexercise}},
fancybox={#1}{Exercise~\arabic{myexercise}}}

\newcommand{\SomeText}{To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
}

\begin{document}

\lipsum[1-2]

% Example of a FancyExercise
\begin{FancyExercise}[.75\textwidth]
\SomeText
\end{FancyExercise}

% Example of a FancyExercise with default width
\begin{FancyExercise}
\SomeText
\end{FancyExercise}

% Example of a FancyBox with a mandatory title
\begin{FancyBox}[\textwidth]{This is a tcolorbox}
\SomeText
\end{FancyBox}

\end{document} • Thank you @ignasi, it woked like a charm! However, I have a question about the exercise counter. Could you include an example with the exercise counter working? Cheers!
– Dox
May 24 '13 at 14:12
• @gonzalo-medina Thank you for your help, your solutions have been great. However, I am having problems with boxed larger than two pages (using book class)... I'm getting one boxed page, a blank page, a boxed page, and so on. Do you know a solution for this behaviour?
– Dox
May 28 '13 at 17:53
• @Dox: I've tested with \documentclass{book} and \begin{MyExercise}\lipsum[1-20]\end{MyExercise}. It produces a box starting on page 1 and finishing on page 5 without any blank page. May 28 '13 at 21:11

Heres' one possible solution; the Fancybox environment has one optional argument, for the width of the box (default=0.5\textwidth), and a mandatory argument for the title of the box (provision is made in the case no title is given (see example below)):

\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{amsmath,amssymb}
\usetikzlibrary{calc}

\newenvironment{Fancybox}[.5\textwidth]
{%
\ifstrempty{#2}%
{}%
{\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[draw=red,anchor=east,rectangle,fill=red,font=\color{white}]
{\strut#2};}}%
}%
\mdfsetup{
skipabove=\topskip,
skipbelow=\topskip,
innertopmargin=10pt,
linecolor=red,%
innerlinewidth=0.8pt,
topline=true,
backgroundcolor=blue!20,
frametitleaboveskip=\dimexpr-\ht\strutbox\relax,
roundcorner=5pt,
singleextra={\path let \p1=(P), \p2=(O) in node[fill=red,font=\color{white}] at (P|-0,0.5*\y2+0.5*\y1) {$\clubsuit$};}
}
\begin{mdframed}[userdefinedwidth=#1]\relax%
}
{\end{mdframed}}

\begin{document}

\begin{Fancybox}{A fancy title}
To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
\end{Fancybox}

\begin{Fancybox}[\textwidth]{Another fancy title}
To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
\end{Fancybox}

\begin{Fancybox}[4cm]{}
To calculate the horizontal position the kinematic differential equations are needed:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
\end{Fancybox}

\end{document} Since no information was given in the question about the desired position of the \clubsuit decoration in case the environment spans more than one page, I only used the decoration in singleextra, so it will only appear if the frame doesn't contain a page break (in any case, this seemed a valid choice). If other bahaviour is desired, this can be easily achieved using the other ...extra family of keys (refer to the mdframed documentation for details).

A request has been made to include a counter and have a fixed "Exercise" title; this makes things even easier: a simple modification is enough; this new code also adds \allowdisplaybreaks (suggested by Marco Daniel to have better breaking points in case displayed math is involved):

\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{amsmath,amssymb}
\usetikzlibrary{calc}

\newcounter{exe}

\newenvironment{Fancybox}[.5\textwidth]
{%
\stepcounter{exe}%
\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[draw=red,anchor=east,rectangle,fill=red,font=\color{white}]
{\strut Exercise~\theexe};},%
skipabove=\topskip,
skipbelow=\topskip,
innertopmargin=10pt,
linecolor=red,%
innerlinewidth=0.8pt,
topline=true,
backgroundcolor=blue!20,
frametitleaboveskip=\dimexpr-\ht\strutbox\relax,
roundcorner=5pt,
singleextra={\path let \p1=(P), \p2=(O) in node[fill=red,font=\color{white}] at (P|-0,0.5*\y2+0.5*\y1) {$\clubsuit$};}
}
\begin{mdframed}[userdefinedwidth=#1]\allowdisplaybreaks\relax%
}
{\end{mdframed}}

\begin{document}

\begin{Fancybox}
To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
\end{Fancybox}

\begin{Fancybox}[\textwidth]
To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
\end{Fancybox}

\begin{Fancybox}[4cm]
To calculate the horizontal position the kinematic differential equations are needed:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
\end{Fancybox}

\end{document} • Thank you very much @gonzalo-medina, Inspired solution to the problem!
– Dox
May 28 '13 at 17:44
• @ignasi Thank you for your help, your solutions have been great. However, I am having problems with boxed larger than two pages (using book class)... I'm getting one boxed page, a blank page, a boxed page, and so on. Do you know a solution for this behaviour?
– Dox
May 28 '13 at 17:54
• @Dox I've done some tests, but I haven't been able to reproduce the problem mentioned. Open a new (follow-up) question providing a minimal working example illustrating the problem. May 28 '13 at 23:52