How do create a colourful example environment like the one here?
[Tex/LaTex] Fancy example environment
environments
Related Solutions
Here a simple solution with rule:
\documentclass{article}
\usepackage{xcolor}
\usepackage{ntheorem,lipsum}
\theoremseparator{:}
\newtheorem*{example}{Example}
\newtheorem*{solution}{Solution}
\newlength\outerlinewidth
\newlength\interline
\newlength\hangleft
\newenvironment{block}{%
\trivlist\item\relax\par%
\noindent\rlap{\hspace*{-\hangleft}\color{gray}%
\rule{\dimexpr \hangleft+\linewidth\relax}{\outerlinewidth}%
}%
}{\par%
\noindent\rlap{\hspace*{-\hangleft}\color{gray}%
\rule{\dimexpr \hangleft+\linewidth\relax}{\outerlinewidth}
}%
\endtrivlist}
\newcommand*\blockline{\par\noindent%
\begingroup\color{gray}%
\rule{\linewidth\relax}{\interline}\endgroup\par%
}
%Set parameter:
\setlength{\outerlinewidth}{6pt} %height of the outer line
\setlength{\interline}{2pt} %height of the inter line
\setlength{\hangleft}{1cm} % left indention
\begin{document}
\begin{block}
\begin{example}[Rear clock ahead]
\lipsum[1]
\end{example}
\begin{solution}
\lipsum[2]
\end{solution}
\blockline
\begin{example}[Rear clock ahead]
\lipsum[1]
\end{example}
\begin{solution}
\lipsum[2]
\end{solution}
\end{block}
\end{document}
EDIT
Here a solution with mdframed
\documentclass{article}
\usepackage{microtype}
\usepackage{xcolor}
\usepackage{ntheorem,lipsum}
\usepackage[framemethod=default]{mdframed}
\usepackage{showframe}
\mdfdefinestyle{block}{%
rightmargin=0pt,
innerrightmargin=0pt,
skipabove=\topskip,
skipbelow=\topskip,
leftline=false,
rightline=false,
leftmargin=-1cm,
innerleftmargin=1cm,
linecolor=gray,
linewidth=6pt
}
\newmdenv[style=block,ntheorem]{block}
\theoremseparator{:}
\newtheorem*{example}{Example}
\newtheorem*{solution}{Solution}
\newlength\outerlinewidth
\newlength\interline
\newcommand*\blockline{\par\noindent%
\begingroup\color{gray}%
\rule{\linewidth\relax}{\interline}\endgroup\par%
}
%Set parameter:
\setlength{\interline}{2pt} %height of the inter line
\begin{document}
\begin{block}
\begin{example}[Rear clock ahead]
\lipsum[1]
\end{example}
\begin{solution}
\lipsum[2]
\end{solution}
\blockline
\begin{example}[Rear clock ahead]
\lipsum[1]
\end{example}
\begin{solution}
\lipsum[2]
\end{solution}
\end{block}
\end{document}
EDIT 2
Here a solution with mdframed
in combination with etoolbox
. Of course you can use the solution without using mdframed
.
\documentclass{article}
\usepackage{microtype}
\usepackage{xcolor}
\usepackage{ntheorem,lipsum}
\usepackage[framemethod=default]{mdframed}
\usepackage{showframe}
\mdfdefinestyle{block}{%
rightmargin=0pt,
innerrightmargin=0pt,
skipabove=\topskip,
skipbelow=\topskip,
leftline=false,
rightline=false,
leftmargin=-1cm,
innerleftmargin=1cm,
linecolor=gray,
linewidth=6pt
}
\newmdenv[style=block,ntheorem]{block}
\theoremseparator{:}
\theorembodyfont{}
\newtheorem*{example}{Example}
\newtheorem*{solution}{Solution}
\newtoggle{env:example}
\newtoggle{env:solution}
\newtoggle{env:block}
\settoggle{env:solution}{false}
\settoggle{env:example}{false}
\settoggle{env:block}{false}
\BeforeBeginEnvironment{block}%
{\settoggle{env:block}{true}
\settoggle{env:solution}{false}
\settoggle{env:example}{false}}
\AfterEndEnvironment{block}%
{\settoggle{env:solution}{false}
\settoggle{env:block}{false}
\settoggle{env:example}{false}}
\BeforeBeginEnvironment{example}%
{\ifboolexpr{ test {\iftoggle{env:block}}
and
test {\iftoggle{env:example}}}%
{\settoggle{env:example}{true}}{}
\iftoggle{env:example}{\blockline}{}}
\AfterEndEnvironment{example}%
{\settoggle{env:example}{true}}
\newlength\outerlinewidth
\newlength\interline
\newcommand*\blockline{\par\noindent%
\begingroup\color{gray}%
\rule{\linewidth\relax}{\interline}\endgroup\par%
}
%Set parameter:
\setlength{\interline}{2pt} %height of the inter line
\begin{document}
\begin{block}
\begin{example}[Rear clock ahead]
\lipsum[1]
\end{example}
\begin{solution}
\lipsum[2]
\end{solution}
%\blockline
\begin{example}[Rear clock ahead]
\lipsum[1]
\end{example}
\begin{solution}
\lipsum[2]
\end{solution}
\end{block}
\end{document}
This is just a partial answer (for a complete answer, go to the last exampe). I used the thmtools
package as a front-end for amsthm
to define a theorem-like structure.
The use of
\interlinepenalty 10000
in postheadhook
prevents pagebreaks as requested.
\documentclass{book}
\usepackage{xcolor}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{thmtools}
\usepackage{lipsum}
\colorlet{myred}{red!80!black}
\declaretheoremstyle[
spaceabove=\topsep, spacebelow=\topsep,
headfont=\normalfont\color{myred},
notefont=\mdseries\color{myred}, notebraces={(}{)},
bodyfont=\normalfont,
postheadspace=\newline,
headpunct=,
numberwithin=chapter,
postheadhook=\leavevmode%
\interlinepenalty 10000%
\vskip-1.3\baselineskip%
\noindent{\color{myred}\rule{\textwidth}{1pt}}%
\interlinepenalty 10000%
\vskip0.3\baselineskip\noindent,
qed=\textcolor{myred}{$\blacksquare$}
]{mystyle}
\declaretheorem[style=mystyle]{example}
\renewcommand\theexample{\thechapter-\arabic{example}}
\begin{document}
\chapter{Test Chapter}
%\lipsum[1-3]
\begin{example}
\lipsum*[2]
\end{example}
\end{document}
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.:
\documentclass{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{amssymb}
\usepackage{lipsum}
\usepackage{etoolbox}
\newcommand{\xqed}[1]{%
\leavevmode\unskip\penalty9999 \hbox{}\nobreak\hfill
\quad\hbox{\ensuremath{#1}}}
\AtBeginEnvironment{example}{\stepcounter{example}}
\AtEndEnvironment{example}{\xqed{\textcolor{myred}{\blacksquare}}}
\colorlet{myred}{red!80!black}
\newmdenv[
hidealllines=true,
frametitle={Example~\theexample},
frametitlerule=true,
frametitlerulecolor=myred,
frametitlerulewidth=2pt,
frametitlefont=\bfseries\color{myred},
innerleftmargin=0pt,
innerrightmargin=0pt
]{example}
\newcounter{example}[chapter]
\renewcommand\theexample{\thechapter-\arabic{example}}
\begin{document}
\chapter{Test Chapter}
\begin{example}
\lipsum*[2]
\end{example}
\end{document}
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 like example
, but placing the end-mark to the left:
\documentclass{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{etoolbox}
\newlength\mylen
\newcommand{\xxqed}[1]{%
\settowidth\mylen{$#1$}%
\vskip-2\baselineskip\hspace*{-\mylen}\llap{\hbox{\ensuremath{#1}}}%
}
\newcommand{\xqed}[1]{%
\leavevmode\unskip\penalty9999 \hbox{}\nobreak\hfill
\quad\hbox{\ensuremath{#1}}}
\AtBeginEnvironment{example}{\stepcounter{example}}
\AtEndEnvironment{example}{\xqed{\textcolor{myred}{\blacksquare}}}
\AtBeginEnvironment{lexample}{\stepcounter{example}}
\AtEndEnvironment{lexample}{\xxqed{\textcolor{myred}{\blacksquare}}}
\colorlet{myred}{red!80!black}
\newmdenv[
hidealllines=true,
frametitle={Example~\theexample},
frametitlerule=true,
frametitlerulecolor=myred,
frametitlerulewidth=2pt,
frametitlefont=\bfseries\color{myred},
innerleftmargin=0pt,
innerrightmargin=0pt,
skipabove=.6\topskip,
skipbelow=.6\topskip
]{lexample}
\newmdenv[
hidealllines=true,
frametitle={Example~\theexample},
frametitlerule=true,
frametitlerulecolor=myred,
frametitlerulewidth=2pt,
frametitlefont=\bfseries\color{myred},
innerleftmargin=0pt,
innerrightmargin=0pt,
skipabove=.6\topskip,
skipbelow=.6\topskip
]{example}
\newcounter{example}[chapter]
\renewcommand\theexample{\thechapter-\arabic{example}}
\newcommand\Text{% just to generate text for the example
Quisque ullamcorper placerat ipsum. Cras nibh. Morbi vel justo vitae lacus
tincidunt ultrices. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In
hac habitasse platea dictumst. Integer tempus convallis augue. Etiam facilisis.
Nunc elementum fermentum wisi. Aenean placerat. Ut imperdiet, enim sed
gravida sollicitudin, felis odio placerat quam, ac pulvinar elit purus eget enim.
Nunc vitae tortor. Proin tempus nibh sit amet nisl.
}
\begin{document}
\chapter{Test Chapter}
\begin{lexample}
\Text
\begin{align}
a &= b \\
&= c
\end{align}
\end{lexample}
\Text
\begin{example}
\Text
\end{example}
\end{document}
Best Answer
Below I present two options, both of them admitting page breaks.
A simple example using
tcolorbox
(admits page breaks); adjust the settings according to your needs:The code:
A simple example using
mdframed
(admits page breaks); adjust the settings according to your needs:The code: