[Tex/LaTex] Adding something automatically to every paragraph in an environment

environmentsmacrosmarginparmarginsparagraphs

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.

Best Answer

\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.

Related Question