[Tex/LaTex] A special example environment

environmentsexamplesframedpage-breaking

How can I define an environment named "example" so that:

  1. can be broken over pages
  2. has a horizontal line above "Example \chap.\no" (e.g. Example 4.10),
    and vertical line that is spread to the whole content of the example
    and ends to a square symbol close to the text or formula
  3. tables, figures, and other floats can be inserted in the environment
  4. the last line of this environment can be text or formula; If it is a
    formula, the square symbol (QED) should be inserted close to it (the
    same but not the next line)
  5. has a bold horizontal line above "Example 4.8". The line has to be as long as "Example 4.8" length; so it should not have a fixed length.
  6. the bottom horizontal line should come to near of the rightmost word at the last line. For example, in the image below, the rightmost word at the last line, is "5685.", so we want to draw the bottom horizontal line (and consequently the QED symbol), just near to this word.

Just like the following image

EDIT: conditions 5 and 6 added
enter image description here

Best Answer

One possible solution, at least for requests 1, 2 3, and 5, using mdframed:

\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{booktabs}
\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{calc}
\usepackage{lipsum}

\newcounter{exa}[section]
\renewcommand\theexa{\thesection.\arabic{exa}}

\newlength\mylen% to hols the width of the title

\newmdenv[
  settings={\global\refstepcounter{exa}
            \settowidth\mylen{\bfseries\color{cyan}Example~\theexa}},
  leftline=false,
  linecolor=cyan,
  middlelinewidth=1pt,
  innertopmargin=30pt,
  innerbottommargin=10pt,
  innerleftmargin=0pt,
  skipbelow=1.3\topsep,
  singleextra={
    \path node[font=\bfseries\color{cyan},anchor=west,yshift=-12pt,inner sep=0pt] at (O|-P) {Example~\theexa};
    \draw[ultra thick,cyan] ([yshift=.5\pgflinewidth]O|-P) -- ++(\mylen,0pt);
    \draw[line width=2pt,white] (O) -- ($(P|-O)+(-2cm,0pt)$) node (A){};
    \node[anchor=south west,fill=cyan,inner sep=3pt,outer sep=0pt] at (A) {};
  },
  firstextra={
    \path node[font=\bfseries\color{cyan},anchor=west,yshift=-12pt,inner sep=0pt] at (O|-P) {Example~\theexa};
    \draw[ultra thick,cyan] ([yshift=.5\pgflinewidth]O|-P) -- ++(\mylen,0pt);
  },
  secondextra={
    \draw[line width=2pt,white] (O) -- ($(P|-O)+(-2cm,0pt)$) node (A){};
    \node[anchor=south west,fill=cyan,inner sep=3pt,outer sep=0pt] at (A) {};
  }
]{example}

\begin{document}

\section{Test Section}
\begin{example}
\lipsum[4]
\begin{center}
\captionof{table}{This is a table inside a \texttt{mdframed} environment}
\begin{tabular}{ccc}
\toprule
column1a & column2a & column3a \\
column1a & column2a & column3a \\
column1a & column2a & column3a \\
\bottomrule
\end{tabular}
\end{center}
\end{example}
\lipsum*[2-3]
\setcounter{exa}{23}% just for the example
\begin{example}
\lipsum*[1-4]
\begin{center}
\includegraphics[width=6cm]{ctanlion}
\captionof{figure}{This is a figure inside a \texttt{mdframed} environment}
\end{center}
\lipsum*[5-6]
\end{example}
\lipsum*[2]

\end{document}

enter image description here

Floats won't be allowed inside the mdframed environment; as has been mentioned in comments, this is not a restriction; in fact, it wouldn't make much sense. However, as the example demonstrates, one can still have tabular material and images with captions, using the \captionof from the caption or capt-of packages.

CTAN lion drawing by Duane Bibby.


SMH: Due to a reference issue, I edited the code.


Another approach is the suggest package tcolorbox. With the newest version of the package you can use the following code. It has the same limitation like mdframed.

\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage{mwe}%load graphix,lipsum,...
\usepackage{xcolor}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\tcbuselibrary{breakable}

\newcounter{example}[section]
\renewcommand\theexample{\thesection.\arabic{example}}

\tcbset{examplebox/.style={%
 title={Example~\refstepcounter{example}\theexample},
 breakable,
 leftrule=0pt,
 arc=0pt,
 colback=white,
 colframe=cyan,
 enhanced,
 colbacktitle=white,
 coltitle=cyan,
 titlerule=0pt,
 enlarge left by=-4mm,
 width=\linewidth+4mm,
 enlarge top by=2pt,
 overlay unbroken={\draw[cyan,line width=2pt] (frame.north west)++(0,0.25mm) --++(4cm,0pt);
                      \draw[white,line width=10mm] (frame.south west) --++(8cm,0pt) node (P) {};
                      \fill[cyan] (P) rectangle ++(6pt,6pt) ;},%
 overlay first={\draw[cyan,line width=2pt] (frame.north west)++(0,1pt) --++(4cm,0pt);},%
 overlay last={\draw[white,line width=10mm] (frame.south west) --++(8cm,0pt) node (P) {};
                       \fill[cyan] (P) rectangle ++(6pt,6pt) ;},%
 }%
}

\newenvironment{example}{%
  \tcolorbox[examplebox]}%
 {\endtcolorbox}

\begin{document}

\section{Test Section}

\begin{example}
\lipsum[4]
\begin{center}
\captionof{table}{This is a table inside a \texttt{tcolorbox} environment}
\begin{tabular}{ccc}
\toprule
column1a & column2a & column3a \\
column1a & column2a & column3a \\
column1a & column2a & column3a \\
\bottomrule
\end{tabular}
\end{center}
\end{example}
\lipsum*[2-3]
\begin{example}
\lipsum*[1-4]
\begin{center}
\includegraphics[width=6cm]{example-image-a}
\captionof{figure}{This is a figure inside a \texttt{tcolorbox} environment}
\end{center}
\lipsum*[5-6]
\end{example}
\lipsum*[2]

\end{document}