Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers 3

up vote 1 down vote accepted

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}
share|improve this answer
    
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? –  padde 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! –  padde 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
add comment

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}

enter image description here

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}
share|improve this answer
2  
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... –  padde 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
1  
@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
1  
@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
show 12 more comments

Below is a solution using ntheorem and the changepage package.

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

\makeatletter
\newtheoremstyle{marginjustnumber}%
{\item[\theorem@headerfont \llap{(##2) }]}%
{\item[\theorem@headerfont \llap{(##2)} (##3)]}%
\makeatother

\newlength{\examplelabel}
\theoremstyle{marginjustnumber}
\theorembodyfont{}
\theoremseparator{}
\theoremprework{%
\settowidth{\examplelabel}{(\themyexample)\quad}%
\begin{adjustwidth}{\examplelabel}{}}
\theorempostwork{\end{adjustwidth}}
\newtheorem{myexample}{}

\begin{document}

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

\end{document}
share|improve this answer
    
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! –  padde 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
add comment

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.