You could use the amsthm
package. As per their recommendations, which is what most people use, you should define the environment as
\theoremstyle{definition}
\newtheorem{exmp}{Example}[section]
and than use is as
\begin{exmp}
This is the example.
\end{exmp}
Note however, that the above definition will give the examples its own counter. If you already have theorems and definitions using the thm
-counter, you might want to consider using
\newtheorem[thm]{exmp}
since it can be very confusing for the reader if you have Theorem 3, Lemma 3, Definition 3 and Example 3 on different pages.
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}
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}
Best Answer
This is just a partial answer (for a complete answer, go to the last exampe). I used the
thmtools
package as a front-end foramsthm
to define a theorem-like structure.The use of
in
postheadhook
prevents pagebreaks as requested.The only thing that remains unsolved is the positioning of the end-mark when the last line in the environment belongs to a displayed math expression. Quite frankly I wouldn't know how to conditionally change the end-mark placement.
Here's another possibility using this time the
mdframed
package; the end-mark is provided using the\xqed
macro defined in the document linked in the page Theorems in AMS-LaTeX of the AMS-FAQ.:Here's now a possible complete solution; instead of conditionally redefining the behaviour of the end-marks (which could be really difficult), the user can manually select the
lexample
environment which behaves likeexample
, but placing the end-mark to the left: