[Tex/LaTex] LaTeX example environment

environmentsmacros

I was inspired by the following LaTeX document:
http://www.people.fas.harvard.edu/~djmorin/chap11.pdf
(written by Prof. David Morin from Harvard university)

I would like to produce a similar example environment, as seen in page 11 for a single example (a thick grey line before and after the example text, and also the "Example (…)" and "Solution" titles in each example.)
Also, as in page 15 a thin grey line is separating two consecutive examples.

Any idea how to create an environment with these macros? I might change it a bit but I would like to be able to produce it.

EDIT:

Is it possible to include the \begin{block} in the example and the \end{block} in the solution, so that I don't need to explicitly write it, but only

\begin{example}[Rear clock ahead]
\lipsum[1]
\end{example}

\begin{solution}
\lipsum[2]
\end{solution}

\begin{example}[Example 2]
\lipsum[1]
\end{example}

\begin{solution}
\lipsum[2]
\end{solution}

And finally, I would rather have a blank line between the "example" and "solution", and also have \blockline have an equal vertical distance from its top and bottom texts. I am just not sure where to put these commands in your definitions.

Thanks.

Framed

Best Answer

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}

enter image description here

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}
Related Question