Paragraph numbering (lineno for paragraphs)

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.

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – user11232 Mar 7 '15 at 15:33

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}


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}
\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}{\let\everypar\oldpars\par}
\begin{document}
\begin{enumpars}
\lipsum[1-5]
\end{enumpars}
\lipsum[6-8]
\begin{enumpars}
\lipsum[9-14]
\end{enumpars}
\end{document}