1

In my manuscript I make a comment that I would like to reference later. I would like the reference to look like "See my comment on paragraph 3 of page 4". I know how to automatically extract the page number of my comment using \label and \pageref

blah blah \label{comment1}

blah blah

blah blah see my comment on paragraph 3 of page \pageref{comment1}

but is there a way to also get the paragraph number automatically?

Note that I do not want the paragraphs to be numbered in the text. I expect the reader to count out to the third paragraph on the page.

2
  • If a paragraph is split across a page, is it paragraph 1 or 0? Note that \par is used inside figures, tables, and every \item in a list. Commented Jul 7, 2021 at 14:34
  • I have no personal preference for numbering paragraphs that are split across pages
    – Shep Bryan
    Commented Jul 10, 2021 at 23:22

1 Answer 1

1

Note that there is already a paragraph counter, which is incremented by \paragraph. I assume you don't want to use that one.

This uses \everypar to increment the counter locally and set \@currentlabel globally. That way paragraphs inside environments won't count. Note that \everypar is not preserved in general and will need to be reset frequently. (\@afterheading was the most frequent offender.)

\documentclass{article}
\usepackage{etoolbox}
\usepackage{lipsum}

\newcounter{Paragraph}[page]

\makeatletter
\newcommand{\Everypar}{\advance \c@Paragraph by 1
  \xdef\@currentlabel{\theParagraph}}
  
\patchcmd{\@afterheading}{\if@nobreak}{\Everypar\if@nobreak}{}{Failed}
\patchcmd{\@afterheading}{\everypar{}}{\everypar{\Everypar}}{}{Failed}
\makeatother
\everypar{\Everypar}

\begin{document}
\section{Test}

\ref{first} page \pageref{first} \label{first}\LipsumPar{1}

\begin{figure}[p]
  \caption{test}
\end{figure}

\ref{second} page \pageref{second} \label{second}\LipsumPar{2}

\ref{third} page \pageref{third} \label{third}\LipsumPar{3}

\ref{fourth} page \pageref{fourth} \label{fourth}\LipsumPar{4}

\ref{fifth} page \pageref{fifth} \label{fifth}\LipsumPar{5}% \label placement matters

\ref{sixth} page \pageref{sixth} \label{sixth}\LipsumPar{6}

\ref{seventh} page \pageref{seventh} \label{seventh}\LipsumPar{7}

\ref{eighth} page \pageref{eighth} \label{eighth}\LipsumPar{8}

\end{document}
2
  • I can see that this works when I try it, but do you mind explaining a little bit of what exactly is going on here? It seems that lines 5-14 change the paragraph counter to tick up after every new page, and it seems as if somehow you make \ref{...} reference the paragraph number rather than the section number. Is that correct?
    – Shep Bryan
    Commented Jul 10, 2021 at 23:23
  • 1
    \everypar executes at the start of every paragraph. \newcounter causes the Paragraph counter to reset to 0 when page counter is incremented. \label saves \@currentlabel (normally set by \refstepcounter) to the aux file as an argument to \newlabel. \section was a test for \@afterheading, just as the figure was a test for local/global counters. Commented Jul 11, 2021 at 15:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .