# How can I indent all text after the label in my example environment

I have built my own example environment for a paper I am working on. I have created a counter to label the resulting blocks of text. Now I want to indent the Text after the label, like so:

(1)   Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nullam hendrerit tempor purus in interdum. Ut nec augue
ipsum.

(2)   Sed volutpat aliquet mauris non porta. Vivamus sagittis
ante dapibus tellus vehicula ullamcorper. Quisque tris-
tique commodo nisi et semper.

...

(10)  Sed volutpat aliquet mauris non porta. Vivamus sagittis
ante dapibus tellus vehicula ullamcorper. Quisque tris-
tique commodo nisi et semper.


This is the code I have written so far:

\newcounter{examplectr}

\newenvironment{example}[1][]{
\ignorespaces
\refstepcounter{examplectr}
(\theexamplectr)
\hfill
}{}


But unfortunately, the resulting output looks more like this:

(1)   Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nullam hendrerit tempor purus in interdum. Ut nec augue ipsum.

(2)   Sed volutpat aliquet mauris non porta. Vivamus sagittis
ante dapibus tellus vehicula ullamcorper. Quisque tristique
commodo nisi et semper.


Does anyone know how to achieve that?

-

The following answer builds closely on the OP's original code, only adding some low-level TeX code in the preamble to set the width of the hanging indents. Basically, it defines a length variable called \alength and then sets its value to the width of a "box" filled by "(\theexamplectr)" (plus a minute fudge factor, determined empirically). The environment example is instructed to hang-indent all but the first line of its paragraph by the amount of \alength. Note that the amount of the indent is adjusted automatically as the counter variable switches from 1 to 2, 3, etc digits.

This code is, by design, not as "elegant" as simply setting some of the options of the enumitem package. In contrast, it has the advantage (IMHO) of not depending on this package and, instead, demonstrating how one may go about achieving the OP's objective by setting the \hangafter and \hangindent parameters. :-)

Addendum: I edited the code, following the OP's comment, to allow the possibility that the maximum number of examples is known in advance, so that the bodies of the examples may all be set to the same (hanging) indent. This is implemented by defining a counter variable named maxexamplectr; if this variable is greater than zero, its value is taken to be the largest number of any example environment. Conversely, if its value is zero the value of the hang-indent is calculated separately for each label.

\documentclass{article}
\usepackage{lipsum,ifthen}

\newcounter{maxexamplectr}  %% largest possible number of examples
\setcounter{maxexamplectr}{1002}

\newcounter{examplectr} %% counter of current example environment
\setcounter{examplectr}{0}  %% initialize this counter

\newlength\alength

\makeatletter
% if maxexamplectr>0, calculate \alength only once
\ifthenelse{\themaxexamplectr>0}{
\sbox\@tempboxa{(\themaxexamplectr)\ \ }
\setlength{\alength}{\the\wd\@tempboxa}}{}

\newenvironment{example}{%
\refstepcounter{examplectr}%
\ifthenelse{\themaxexamplectr=0}{% calculate \alength if maxexamplectr=0
\sbox\@tempboxa{(\theexamplectr)\ \ }
\setlength{\alength}{\the\wd\@tempboxa}}{}
% start a "generic list" with a single, numbered item
\begin{list}{{}(\theexamplectr)}%
{\leftmargin\alength
\labelwidth\alength
\topsep0pt}
\ignorespaces\item }
{\unskip \end{list}}
\makeatother

\begin{document}
\noindent
The quick brown fox \ldots

\begin{example}
\lipsum[2]
\end{example}

\setcounter{examplectr}{20}
\begin{example}
\lipsum[2]
\end{example}

\setcounter{examplectr}{1001}
\begin{example}
\lipsum[2]
\end{example}

\noindent
The quick brown fox \ldots
\end{document}

-
really not bad, i like this approach! i'm really learning a lot this way. but one of my big problems remains: the paragraphs that are numbered with a two-digit-number have a deeper indentation than the paragraphs that are numbered wit only one digit. so one would have to measure the width of the last label and then indent all example environments this deep. is that possible? – Patrick Oscity Sep 6 '11 at 11:33
To answer your question: If one knows in advance what the widest numeral label is going to be, one could account for that in two ways. First, one could redefine the example env to accept as an optional input the widest possible label. But this becomes tedious if you have, say, 105 examples and must provide the width of the widest label to the first 99 examples. Second, you could define a "special variable" in the preamble; if it is defined, it is the largest numeral that may be encountered. The example code tests for the presence of this variable and uses it to calculate the hangindent width. – Mico Sep 6 '11 at 15:20
I've adjusted the MWE above to incorporate the second possibility described in the preceding comment. – Mico Sep 6 '11 at 15:21
i must admit that i like this solution better than Gonzalo's so i accepted yours instead. thank you very much! – Patrick Oscity Sep 7 '11 at 11:05
Glad you liked my solution! :-) By the way, in case you haven't already figured this one out on your own, you can add a labeling instruction, such as \label{ex:some}, after any of the \begin{example} instructions and then create a cross-reference to that example environment, somewhere else in the document, with the command \ref{ex:some}. Happy TeXing! – Mico Sep 7 '11 at 14:46

Using the enumitem package you can easily create a list-like environment to achieve the desired result:

\documentclass{article}
\usepackage{enumitem}
\usepackage{lipsum}

\newlist{example}{enumerate}{4}
\setlist[example,1]{label=(\arabic*),leftmargin=*,resume,ref=\arabic*}

\begin{document}

\begin{example}
\item \lipsum[1]
\end{example}
\lipsum[1]
\begin{example}
\item \lipsum[1]
\item \lipsum[1]
\end{example}

\end{document}


EDIT: you can wrap the list-like environment inside another one, using the series=, resume= options:

\documentclass{article}
\usepackage{enumitem}
\usepackage{lipsum}

\newlist{example}{enumerate}{4}
\setlist[example,1]{label=(\arabic*),leftmargin=*,ref=\arabic*}
\newenvironment{myexample}
{\begin{example}[series=lafter,resume=lafter]\item}
{\end{example}}

\begin{document}

\begin{myexample}
\lipsum[1]
\end{myexample}
\lipsum[1]
\begin{myexample}
\lipsum[1]
\end{myexample}

\end{document}

-
You should add the options resume to \setlist[example,1] to achieve the same functionality as the environment of the question. – N.N. Sep 5 '11 at 17:35
sorry, i think i didn't make clear enough, that there will be paragraphs between the examples! so this won't work i guess... – Patrick Oscity Sep 5 '11 at 17:38
@padde: why not? Using the resume option you can end the environment, write your paragraphs, and then continue with the environment. See my updated answer – Gonzalo Medina Sep 5 '11 at 17:42
@padde: you're welcome! Don't forget to up-vote and accept the answer that you consider to be the one which best answers your question. – Gonzalo Medina Sep 5 '11 at 18:27
@padde: you need a recent version (3.0) of the enumitem package. Update your version of the package. – Gonzalo Medina Sep 5 '11 at 18:44

Below is a solution using ntheorem and the changepage package.

\documentclass{report}
\usepackage{ntheorem}
\usepackage{lipsum}
\usepackage[showframe]{geometry}
\usepackage{changepage}

\makeatletter
\newtheoremstyle{marginjustnumber}%
\makeatother

\newlength{\examplelabel}
\theoremstyle{marginjustnumber}
\theorembodyfont{}
\theoremseparator{}
\theoremprework{%
\newtheorem{myexample}{}

\begin{document}

\begin{myexample}
\lipsum[1]
\end{myexample}
\begin{myexample}
\lipsum[2]
\end{myexample}
\begin{myexample}
\lipsum[3]
\end{myexample}

\end{document}

-
unfortunately, the paragraphs do not align any more, when the number of examples is 10 or higher. is it possible to achieve that with this package? the rest is exactly as i want it! – Patrick Oscity Sep 5 '11 at 18:14
You could try changing the line \begin{adjustwidth}{\examplelabel}{}} to \begin{adjustwidth}{1cm}{}} (or some similar length) – cmhughes Sep 5 '11 at 18:25