TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering if there is any formal environment for specifying 'examples' through out the text. If not, which environments do you recommend?

share|improve this question
1  
What kind of examples? – N.N. Jun 21 '11 at 7:49
    
@N.N. A simple one which gives some indentation and enables a logical recognition of the example, through \label{example1} and \ref{example1}. – Md Oliya Jun 21 '11 at 8:00
up vote 38 down vote accepted

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.

share|improve this answer
    
Thank you @Carsten. but loading amsthm errs with "Command \proof already defined." I am using Springer llncs.cls. Any advice? – Md Oliya Jun 21 '11 at 8:34
1  
@Md Oliya In that case you do not need the amsthm package. llncs provides this feature itself. According to section 8 of its documentation you should simply use \begin{example} <text> \end{example}. For using the same counter for Theorems, Definitions etc load the class with the option envcountsame. – Carsten Thiel Jun 21 '11 at 8:44
    
Thanx @Carsten. the amsthm package works great. just a question: the counters are like section_number.counter (e.g III.1). how can i use a simple counter? i.e. no section numbers – Md Oliya Jun 24 '11 at 3:06
1  
@Md Oliya use \newtheorem{exmp}{Example} without the [section] – Carsten Thiel Jun 24 '11 at 7:37
    
---------Great! – Md Oliya Jun 25 '11 at 6:40

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.

share|improve this answer

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.

\AfterEndEnvironment{testexample}{\color{black}} % Bugfix for unknown culprit. When tcolorbox wraps listings compiled with xelatex, subsequential text turns white http://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

enter image description here


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

enter image description here


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

enter image description here

(fontspec used because I compile with XeLaTeX)

share|improve this answer
    
Any idea why the left line will not show up for me? I tried inserting leftline=true, to no avail. However, but setting rightline=true in the setting above, I can get the line on the right to show up. (The same is true for the other two lines.) – Shredderroy Feb 25 at 11:11
    
@Shredderroy I am not sure, but I can tell you that I have since switched to tcolorbox, because I found mdframed not able to properly handle multipage spanning. Also, I know TikZ, so tcolorbox was a logical alternative and not a difficult jump to make. I updated my answer. Maybe this alternative will work for you. – macmadness86 Mar 4 at 15:23
    
@macmadness86 this looks great. Is it possible to create a version where it is possible to use a float environment like figure in the example? – krtek Jun 12 at 19:12
    
Nevermind, I got it working with dropping the figure environment and using \captionof{figure}{Title} instead. – krtek Jun 12 at 19:18
    
@krtek I was just going to say that. See my update. – macmadness86 Jun 12 at 19:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.