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'd like to know if it's possible to have something added automatically to any paragraph in a given environment.

I use LaTeX mostly for doing fill-in-the-blank worksheets at the school where I work. I have an environment called FillInTheBlanks(*). It outputs the page twice---once with certain words that I've indicated with \blank{} blanked out with an underline in their place, and another page(s) with the answers filled in.

I also find it useful to have the paragraphs numbered, to make it easier to talk with the students about the content (e.g. "At the end of paragraph 3, you'll notice..."). In my Worksheets.sty file, I added:

\reversemarginpar
\newcommand\marginlabel[1]{\mbox{}\marginpar
  {\hspace{2em}#1}}

so I can add a paragraph number in the margin to each paragraph. The only thing is, using this I have to add \marginlabel{"a number"} at the beginning of each paragraph manually. I'd like it to be done automatically. If I made a \newcommand to wrap around each paragraph, it could write the command so that it would save me the effort of manually inputting the paragraph numbers, but given that there are usually half a dozen paragraphs per page or less, it doesn't actually save much labour.

Is there some way I could have the \marginlabel{} command inserted automatically whenever a new paragraph starts?

(*) I know the exam class does something like this, but it doesn't do it the way I wanted to, so I rolled my own.

share|improve this question
2  
See "Automatically assign a number to every paragraph" for a very similar question. –  Mike Renfro Nov 7 '11 at 2:43
add comment

1 Answer

up vote 9 down vote accepted

\everypar can help, but be careful: some LaTeX macros redefine it.

However, this works:

\documentclass{article}
\usepackage{lipsum}
\newcounter{par}
\newenvironment{numberedpars}{\setcounter{par}{0}%
  \everypar={\refstepcounter{par}%
    \makebox[0pt]{\makebox[15ex][l]{\arabic{par}}}}}{}
\begin{document}

\begin{numberedpars}
  \lipsum[1-4]
\end{numberedpars}

\end{document}

enter image description here

Since this uses \refstepcounter, you can use \label and \ref mechanism to reference your paragraphs.

share|improve this answer
    
This did exactly what I wanted. Thanks so much! –  angus77 Nov 7 '11 at 3:20
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.