Example box with automatic numbering?

Working with LaTeX, I was just wondering how do you create a box around a set of related text such as an example in a textbook? I found \framebox or \makebox, but I can't figure out how to make it auto number such as Example 1.1 (for first example in chapter 1), or even manually add to it.

-

migrated from stackoverflow.comApr 4 '11 at 15:49

This question came from our site for professional and enthusiast programmers.

Look at the ntheorem, shadethm and thmtools packages, all of which let you define new environments in boxes with numbering.

-

Put it in an environment, like the listings package mentioned above, or you can define your own environment that will get its own counter:

 \newenvironment{nam}[args]{begdef}{enddef}

\renewenvironment{nam}[args]{begdef}{enddef}


These commands define or redefine an environment.

nam -- The name of the environment. For \newenvironment there must be no currently defined environment by that name, and the command \nam must be undefined. For \renewenvironment the environment must already be defined.

args -- An integer from 1 to 9 denoting the number of arguments of the newly-defined environment. The default is no arguments.

begdef -- The text substituted for every occurrence of \begin{nam}; a parameter of the form #n in cmd is replaced by the text of the nth argument when this substitution takes place.

enddef -- The text substituted for every occurrence of \end{nam}. It may not contain any argument parameters.

-

You could use the listings package. I don't know how much it is suited for other than programming languages, but I think it is adaptable. Are your examples code listings? Anyway, here is an example:

\usepackage{listings}
...

\renewcommand{\lstlistlistingname}{Examples} % Label of Table of Listings
\renewcommand{\lstlistingname}{Example} % Label for every Listing
\renewcommand{\thelstlisting}{\thesection.\arabic{lstlisting}}
\lstset{frame=single,captionpos=b,basicstyle={\rmfamily}}

\begin{lstlisting}[caption={[short D.]long Description},language=ruby]
array.each do |a|
a.times { puts "Examples" }
end
\end{lstlisting}


Edit:
For math there is an option mathescape=true, so formulas work inside single dollar signs.

-
My examples are mostly mathematics, its notes taking for math class. Thanks a lot! –  FurtiveFelon Sep 25 '09 at 18:13