# Highlight \paragraph command arg if labeled

For editing purposes I need to spot labeled \paragraphs in my documents and I'd like to do it directly in the .pdf file. I'm wondering if is there a way to change the layout of the \paragraph command (for exaple changing the text color of the arg) only if it has been labeled.

This is a MWE:

\documentclass[11pt]{article}
\pagestyle{empty}
\usepackage{blindtext}
\begin{document}

\section{Section}

\subsection{Subsection}

\paragraph{Paragraph 1.}
\blindtext

\paragraph{Paragraph 2.}
\label{paragraph2}
\blindtext

\end{document}


I'd like something like:

My idea is to redefine the \paragraph command to make it looking for a regexp in the .aux file and make it behave accordingly (but I have not idea how to do it).

• Why don't you use e.g. \usepackage{showkeys}? – Ulrike Fischer Jun 29 '20 at 7:19
• @UlrikeFischer I don't like the showkeys output. I find it too messy for my taste. Anyway if there was a way to print only the \paragraph's labels I could give it a try. – Gabriele Nicolardi Jun 29 '20 at 8:05
• Note that if you label a paragraph as you did in this MWE the label will refer to the subsection (which is numbered) and not to the paragraph itself (which is not numbered). – Marijn Jun 29 '20 at 8:07
• \paragraph is a level 4 heading so should really only be used after \subsubsection not after \subsection – David Carlisle Jun 29 '20 at 8:23
• It is better (less likely to have bad spacing) if you put \label inside the argument not after it, and if used in that way it would be trivial to make it colour – David Carlisle Jun 29 '20 at 8:24

When LaTeX finds a command that increases a counter (such as \section) then the internal macro \@currentlabel is set with the new value of the counter. The next time a \label command is encountered the current label value is retrieved from \@currentlabel and stored in the .aux file, in order for \ref commands to find it (see How does \@currentlabel work? How does it connect to sectioning commands / headings? for more information on \@currentlabel).

The following code stores the current value of the label when a \paragraph command is encountered. Then, in the \label command, there is a check if the current label value is still the same as when the \paragraph command was used. If yes, then you are still in that paragraph (and not in the next section for example). In that case you can flag the paragraph as being labeled, for example with a \marginnote.

Adding the relevant code to \paragraph and \label can be done using the xpatch package, that provides a command \xpretocmd to add some code at the start of the original definition of a command. Note that for paragraphs this works because a paragraph does not set any counter (it is not a numbered section). If you would use a similar approach to check for, e.g., labeled subsections, then you need to store the current label value after the counter is increased, i.e., at the end of the command - which might be problematic because in LaTeX a command may consist of a chain of macro's that postpone argument handling, creating extra complexity for patching.

MWE:

\documentclass[11pt]{article}
\pagestyle{empty}
\usepackage{xcolor}
\usepackage{xpatch}
\makeatletter
\xpretocmd{\paragraph}{\edef\mylabel{\@currentlabel}}{\typeout{paragraph patch ok}}{\typeout{paragraph patch failed}}
\xpretocmd{\label}{\ifx\mylabel\@currentlabel\marginpar{\textcolor{red}{labeled paragraph}}\fi}{\typeout{label patch ok}}{\typeout{label patch failed}}
\makeatother
\def\shortblindtext{Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin.
Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales com-
modo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede.}
\begin{document}

\section{Section}

\subsection{Subsection}

\paragraph{Paragraph 1a.}
\shortblindtext

\paragraph{Paragraph 2a.}
\label{paragraph2}
\shortblindtext

\section{Section}
\label{sec2}
\subsection{Subsection}
\label{subsec2}
\shortblindtext
\paragraph{Paragraph 1b.}
\label{par21}
\shortblindtext

\paragraph{Paragraph 2b.}
\shortblindtext

\end{document}


Result:

• In my real cases (not the MWE) this code works only if I put it after \begin{document}. It doesn't work if I put it in the preamble or in \AtBeginDocument{...}. – Gabriele Nicolardi Jun 29 '20 at 15:49
• There is also \AfterEndPreamble from etoolbox, that hooks to the end of \begin{document}, maybe that would work here. – Marijn Jun 29 '20 at 15:53
• I just tried it. it doesn't work. – Gabriele Nicolardi Jun 29 '20 at 15:57
• Ok, ok... my mistake. I have to put \makeatletter/\makeatother outside of \AtBeginDocument. Now it works! Thank you. – Gabriele Nicolardi Jun 29 '20 at 16:04