# Example environment?

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

-
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

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.

-
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
@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
@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}}%
}{%
\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.

-