[Tex/LaTex] Boxed text between pages

boxesmdframedpage-breakingtikz-pgf

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?

Best Answer

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]

\pgfdeclarehorizontalshading{exercisebackground}{100bp}{color(0bp)=(green!40);color(100bp)=(black!5)}

\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:

enter image description here

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}

enter image description here

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,
    fancytitle={#2},pad at break=2mm},
  fancybox/.default = {.5\textwidth}{}%
}

% This command creates a new environment called
% FancyBox which uses previously defined fancybox 
% style
\newtcolorbox{FancyBox}[2][.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}[1][.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}

enter image description here