# Numbering paragraphs and theorems in the margin

I'd like to make a special paragraph environment that is numbered, and have the numbers put in the margin. Looking at this question, I came up with a way to do this using \llap. However, there is one issue: if the paragraph begins with a theorem environment then there is an unwanted line break at the start of the paragraph.

Here is what I have:

\documentclass{book}
\usepackage{lipsum}
\usepackage{amsthm}

\newtheorem*{theorem}{Theorem}
\newcounter{para}
\newenvironment{para}[1][]{%
\vskip\baselineskip\noindent%
\refstepcounter{para}%
\llap{\makebox[6em][r]{[\thepara]\hspace{.75cm}}}%
}{}

\begin{document}

The following is correct:

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

\vskip\baselineskip
The following is incorrect:

\begin{para}
\begin{theorem}
\lipsum[2]
\end{theorem}
\end{para}

\vskip\baselineskip
The above should look like this:

\begin{para}
\textbf{Theorem.} \itshape \lipsum[2]
\end{para}

\end{document}


One way to fix this is to define an alternate version of the theorem environment with no leading space, and use it at the start of these paragraphs. This is not ideal, since if you add/remove text before the theorem you'd have to remember to change the theorem environment to the right one. Is there a better way?

• Should the number in the margin be the theorem number? Oct 13, 2020 at 19:48
• The number in the margin should be the paragraph number. The theorems in this environment will not have their own numbers. Oct 13, 2020 at 19:59

All the theorem, list, and alignment (e.g., center) environments are derived from trivlist environment, and trivlist does some complex things to the "next" paragraph.

The following example makes use of the newly supported hook para/begin (since LaTeX2e 2021-06-01, see the doc texdoc ltpara), but I don't know why the \mbox{} is needed.

\documentclass{book}
\usepackage{lipsum}
\usepackage{amsthm}

\newtheorem*{theorem}{Theorem}
\newcounter{para}
\newenvironment{para}[1][]{%
\vskip\baselineskip
\refstepcounter{para}%
\llap{\normalfont[\thepara]\hspace{.75cm}}\mbox{}%
\OmitIndent % more or less equivalent to \noindent
}%
}{}

\begin{document}

The following is correct:

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

\bigskip
The following is incorrect:

\begin{para}
\begin{theorem}
\lipsum[2]
\end{theorem}
\end{para}

\bigskip
The above should look like this:

\begin{para}
\textbf{Theorem.} \itshape \lipsum[2]
\end{para}

\end{document}


• Thanks for the reply after all these months, this looks useful. Jul 12, 2021 at 12:59

A simple solution adds the relevant negative vertical spacing just before the theorem:

\documentclass{book}
\usepackage{lipsum}
\usepackage{amsthm}

\newtheorem*{theorem}{Theorem}
\newcounter{para}
\newenvironment{para}[1][]{%
\vskip\baselineskip\noindent%
\refstepcounter{para}%
\llap{\makebox[6em][r]{[\thepara]\hspace{.75cm}}}%
}{}

\begin{document}

The following is correct:

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

\vskip\baselineskip
The following is also correct:

\begin{para}\vspace*{-\dimexpr\baselineskip+\topsep}
\begin{theorem}
\lipsum[2]
\end{theorem}

\end{para}

\end{document}


• Thanks for the suggestion. This does work, but also suffers from the problem that you have to remember to add/remove the \vspace if you remove/add text before the theorem, so it's not totally ideal. Oct 14, 2020 at 11:03