How can I add margin numbering for paragraphs? It should be alternating (left and right) for odd and even pages. So basically like lineo with the switch option but for paragraphs instead of lines.
Asked
Active
Viewed 1,080 times
11
-
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Mar 07 '15 at 15:33
2 Answers
6
Shorter and raw
\documentclass[twoside]{article}
\usepackage{lipsum}
\newcounter{mypara}
\setcounter{mypara}{0}
\begin{document}
\everypar{\refstepcounter{mypara}
\ifodd\value{page}
\marginpar{\themypara}
\else
\marginpar{\makebox[\marginparwidth][r]{\themypara}}
\fi}%
\lipsum[1-100]
\end{document}

4
This uses \everypar (exploiting David's answer at Why does \everypar not work?) to make "something" happen every paragraph. In this case, the "something" that happens is a \tabto to the margin, where the paragraph number is laid, followed by a return to the original position. I use an everypage hook to toggle the marginal location with each page.
I wrap it all in an enumpars environment, so that you can turn it on and off at will.
\documentclass{article}
\usepackage{lipsum,tabto,everypage}
\def\TabMargLeft{-.5in}
\def\TabMargRight{\dimexpr\textwidth+.5in\relax}
\def\TabMargPos{\TabMargRight}
\AddEverypageHook{\ifdim\TabMargPos=\TabMargLeft\relax%
\gdef\TabMargPos{\TabMargRight}\else%
\gdef\TabMargPos{\TabMargLeft}\fi}
\newcounter{parcount}
\let\oldep\everypar%
\newenvironment{enumpars}
{\newtoks\everypar%
\setcounter{parcount}{0}%
\oldep{\the\everypar\stepcounter{parcount}%
\tabto*{\TabMargPos}\makebox[0pt]{(\theparcount)}\tabto*{\TabPrevPos}}%
\par}{\global\let\everypar\oldep\par}
\begin{document}
\begin{enumpars}
\lipsum[1-5]
\end{enumpars}
\lipsum[6-8]
\begin{enumpars}
\lipsum[9-14]
\end{enumpars}
\end{document}


Steven B. Segletes
- 237,551
-
It says
\let\oldep\everyparin the beginnig but\let\everypar\oldparsat the end -- is this a typo? Isn't the idea to save whatever it is\everyparand reset it to this value after theenumpars-environment? Or am I just misunderstanding what you are doing here? – Florian Aug 16 '21 at 13:04 -
1@Florian Yes, thank you for detecting that typo. I will fix it. – Steven B. Segletes Aug 16 '21 at 13:39
-
Unfortunately I am still having an issue with this solution: When I add another evironment, say
\begin{enumerate}afterenumparshas been used I get "Something's wrong--perhaps a missing \item." Using environments withinenumparsworks fine and the paragraphs are even counted properly. – Florian Aug 18 '21 at 12:03 -
1@Florian Thanks again for noting that. I had to make the final
\letofenumparsbe global in order to reach outside of the environment. Thus, the closing part of the environment now reads,{\global\let\everypar\oldep\par}– Steven B. Segletes Aug 18 '21 at 12:08 -
This must be getting on your nerves... But you wouldn't have an idea why
\usepackage[cam, info]{crop}interferes with the counting, would you? – Florian Aug 19 '21 at 10:32 -
@Florian I am sorry, but I have no familiarity with those packages to even have a clue about the adverse interaction. – Steven B. Segletes Aug 19 '21 at 10:37