# LaTeX example environment

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.

-
Welcome to TeX.sx! –  Martin Scharrer Sep 24 '11 at 10:01
Andro, I merged now your original unregistered account with you new registered account. Most posts and comments here where under still under the first. Both look very identical, so it isn't easy to see the difference right away. –  Martin Scharrer Sep 25 '11 at 8:26
It isn't possible. The definition of the start is easy but you can not look in the future :-) –  Marco Daniel Sep 26 '11 at 11:01

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}
\lipsum[1]
\end{example}
\begin{solution}
\lipsum[2]
\end{solution}
\blockline
\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}
\lipsum[1]
\end{example}
\begin{solution}
\lipsum[2]
\end{solution}
\blockline
\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}
\lipsum[1]
\end{example}
\begin{solution}
\lipsum[2]
\end{solution}
%\blockline
\lipsum[1]
\end{example}
\begin{solution}
\lipsum[2]
\end{solution}
\end{block}
\end{document}

-
Thanks. Nicely done:-) only thing is the text inside the example\solution is set in italics instead of regular text, is there a way to change the theorem definition? –  Andro Sep 24 '11 at 10:59
@Andro: Use \theoremseparator{:} \theorembodyfont{} –  Marco Daniel Sep 24 '11 at 11:11
@Andro -- if using \theorembodyfont{} it must be stated before the \newtheorems to which it applies -- which probably isn't all of them. –  barbara beeton Sep 24 '11 at 13:19
@Marco: Ok. I was wondering if it is possible to enhance this example further, and make the block lines appear automatically with the examples. My idea is for the "example" environment to check if the previous environment is also an example; if it is, a thin line will replace the previous thick line. Otherwise, a thick line will be drawn. at the end of a solution a thick line will always be drawn. I this possible? This way has the advantage that the lines are part of the example, which saves typing effort and also further separates the content from the design. –  Andro Sep 24 '11 at 13:49
@Andro: Have you seen my second edit? Please delete you answer ;-) –  Marco Daniel Sep 24 '11 at 13:57