# How to inject commands at start and end of paragraphs and items automatically?

I need to modify the definitions of \item and of paragraphs in such a way that a command is inserted automatically at the begin and end thereof.

My use case comes from the necessity of injecting a bounding box annotation into PDF for some text areas. So far I am here with a minimal example:

\usepackage{zref-savepos}
\newcounter{meas}
\def\a{\zsavepos{Start\themeas}\expandafter\pdfannot width \dimexpr\zposx{End\themeas} sp -\zposx{Start\themeas} sp +0.1cm height \dimexpr -0.1cm + \zposy{End\themeas} sp -\zposy{Start\themeas} sp depth 0.0cm {
/Subtype /Square
/Contents (\themeas)
}}

\def\e{\hfill\zsavepos{End\themeas}\stepcounter{meas}}

\begin{document}

\begin{itemize}
\item\a First\e
\item\a Second\e
\item\a Third - a very long line which uses more than one line of the page for some interesting reason.\e
\end{itemize}

\a This \e

\end{document}


The remaining problem now is to get the \a and \e inserted automatically and not to have to do this manually.

I have not even a clue where to start, as the definition of \item` looks ...umm...strange to me. With paragraphs it is even worse...

• the class is missing in the example. If there is a linebreak the e position can be quite near to begin of the line, is that the wanted behaviour? And what do you mean by paragraphs? \paragraph or real text paragraphs? – Ulrike Fischer May 6 at 8:25
• The class where I actually need it for is beamer. The \hfill in the \e thus far prevented the problems with the linebreaks in my use cases, so no, this is not wanted behavior. With paragraphs I do not mean \paragraph but real text paragraphs. – Nobody-Knows-I-am-a-Dog May 6 at 11:12

With the newest LaTeX-dev (What is "latex-dev"?), which was uploaded yesterday, you can try to use the new paragraph hooks. Be aware that injecting code in this places can be tricky. Read the documentation that you get with texdoc ltpara.

And please don't use \def without checking if you redefine an existing command. \a is already defined.

The following example saves the positions and write red numbers:

\documentclass{article}
\usepackage{zref-savepos}
\ExplSyntaxOn
\bool_new:N \l_dog_para_bool
\int_new:N \g_dog_para_int
{
\bool_if:NT \l_dog_para_bool
{
\int_gincr:N \g_dog_para_int
\llap
{
\color_select:n{red}\tiny\int_use:N\g_dog_para_int%for debugging
\zsavepos{start\int_use:N\g_dog_para_int}
}
}
}
{
\bool_if:NT \l_dog_para_bool
{
\rlap
{
\zsavepos{end\int_use:N\g_dog_para_int}
\color_select:n{red}\tiny\int_use:N\g_dog_para_int
}
}
}
\NewDocumentCommand\dogmarksoff{}{\bool_set_false:N \l_dog_para_bool}
\NewDocumentCommand\dogmarkson{}{\bool_set_true:N \l_dog_para_bool}
\ExplSyntaxOff
\begin{document}

\dogmarkson
\begin{itemize}
\item First
\item Second
\item Third - a very long line which uses more than one line of the page for some interesting reason.

\end{itemize}

This

\end{document}

• Thanx. Problem is: This just was a minimal example and I need a more involved criterion when to add these things. I hope I can build this in TeX, but my knowledge on expl3 is zero. Moving to a dev-branch is risky as this is part of a production activity with already 4000 LoC of additional code I should not break. That's why I am still hoping for an \item redefinition (which actually covers 95% of my use cases). – Nobody-Knows-I-am-a-Dog May 6 at 11:42
• well in beamer you can set the start position more or less by using the font template. But getting the end point automatically is hard without the new hooks. So consider some manual mark up instead. Beside this: latex-dev contains the code of the next latex release, so the code that will be in less than a month in the normal latex. – Ulrike Fischer May 6 at 11:59