Example environment?
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.
Updated Answer 2016-03-04
Here is an alternative using tcolorbox
. This solution is robust in that it can handle many situations: one page, partial page spans, and multiple page spans. I wrapped everything in a \NewDocumentEnvironment
macro from xparse
because it makes it easy to recognize what the input should be.
Caveat for those using XeLaTeX and listings
2016-07-05
Due to an unknown problem (see Why is text turning white (possible scope leak?) after page break and listings + tcolorbox + xelatex?), you should reapply the color "black" explicitly after each "testexample" environment to ensure that your text is visible as expected under all page break conditions. Fix is here: https://tex.stackexchange.com/a/308854/13552
\AfterEndEnvironment{testexample}{\color{black}} % Bugfix for unknown culprit. When tcolorbox wraps listings compiled with xelatex, subsequential text turns white https://tex.stackexchange.com/questions/308672/why-is-text-turning-white-possible-scope-leak-after-page-break-and-listings
Example Syntax
\begin{testexample}[<optional title>]
<content of example>
\end{testexample}
Code
If desired, examples can be numbered by replacing the line
title={\exampletext: #1},% use \thetcbcounter to access the testexample counter text
with
title={\exampletext\ \thetcbcounter: #1},% use \thetcbcounter to access the testexample counter text
\documentclass{article}
\usepackage{fontspec}
\usepackage[most]{tcolorbox}
\newcounter{testexample}
\usepackage{xparse}
\usepackage{lipsum}
\def\exampletext{Example} % If English
\NewDocumentEnvironment{testexample}{ O{} }
{
\colorlet{colexam}{red!55!black} % Global example color
\newtcolorbox[use counter=testexample]{testexamplebox}{%
% Example Frame Start
empty,% Empty previously set parameters
title={\exampletext: #1},% use \thetcbcounter to access the testexample counter text
% Attaching a box requires an overlay
attach boxed title to top left,
% Ensures proper line breaking in longer titles
minipage boxed title,
% (boxed title style requires an overlay)
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,left=3mm,overlay={}},
coltitle=colexam,fonttitle=\bfseries,
before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=3mm,right=0mm,top=2pt,breakable,pad at break=0mm,
before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
% Handles box when it exists on one page only
overlay unbroken={\draw[colexam,line width=.5pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
% Handles multipage box: first page
overlay first={\draw[colexam,line width=.5pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
% Handles multipage box: middle page
overlay middle={\draw[colexam,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },
% Handles multipage box: last page
overlay last={\draw[colexam,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },%
}
\begin{testexamplebox}}
{\end{testexamplebox}\endlist}
\begin{document}
\lipsum[1]
\begin{testexample}[Latin Text]
\lipsum[2]
\end{testexample}
\lipsum[3]
\begin{testexample}
\lipsum[2]
\end{testexample}
\end{document}
Output
Adding figures
You probably don't want a float figure inside of this example environment, because it is a box itself.
I would probably add the caption
package and implement it like this:
\begin{testexample}
\centering
\includegraphics[width=!,height=2in]{mountain}
\captionof{figure}{Wildspitze 3,770 m (12,370 ft)}
\end{testexample}
Output
Old Answer
Here is an alternative using mdframed
and xparse
(xparse
is not absolutely necessary, but I adopted it as my main syntax for defining commands and environments due to its flexibility)
Input
\documentclass{article}
\usepackage{fontspec}
\usepackage{mdframed} % Add easy frames to paragraphs
\usepackage{lipsum} % For dummy text
\usepackage{xcolor}
\usepackage{xparse} % Add support for \NewDocumentEnvironment
\definecolor{graylight}{cmyk}{.30,0,0,.67} % define color using xcolor syntax
\newmdenv[ % Define mdframe settings and store as leftrule
linecolor=graylight,
topline=false,
bottomline=false,
rightline=false,
skipabove=\topsep,
skipbelow=\topsep
]{leftrule}
\NewDocumentEnvironment{example}{O{\textbf{Example:}}} % Define example environment
{\begin{leftrule}\noindent\textcolor{graylight}{#1}\par}
{\end{leftrule}}
\begin{document}
\begin{example}
\lipsum[1]
\end{example}
\begin{example}[Example]
\lipsum[1]
\end{example}
\end{document}
Output
(fontspec
used because I compile with XeLaTeX)
You can make a simple example environment as follows:
\documentclass{article}
\usepackage{lipsum}
\newcounter{examplecounter}
\newenvironment{example}{\begin{quote}%
\refstepcounter{examplecounter}%
\textbf{Example \arabic{examplecounter}}%
\quad
}{%
\end{quote}%
}
\begin{document}
\begin{example}\label{ex:simple}
A simple example environment
\lipsum[1]
\end{example}
Some text: \lipsum[4]
\begin{example}\label{ex:another}
Another example
\end{example}
Example~\ref{ex:another} comes after example~\ref{ex:simple}
\end{document}
So you have an environment which is basically just a wrapper for a quote
environment which has its own counter. You can replace quote with something else that adds indentation more to your liking, but the basic idea is there.
If you want it to include the section number as part of the example (e.g. example 1.2) then that's a little more work and maybe something like amsthm
or ntheorem
is a better bet...
note that \refstepcounter
increments the environment's counter, and also makes the environment "visible" to the referencing mechanism.