# tikzpicture in the background of text that is typeset in non-restricted horizontal mode?

I'd like to get a tikzpicture into the background of text that is typeset by LaTeX in non-restricted horizontal mode.

Currently I do such things by means of the atbegshi-package.

Something similar to this:

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{atbegshi}

\colorlet{MYcolor}{green}%

\begin{document}

\begin{minipage}{\textwidth}%
\noindent\lower-\ht\strutbox\hbox{%
\begin{tikzpicture}[overlay, remember picture]
\coordinate (begin the line) at (0,0) ;
\end{tikzpicture}%
}%
\lipsum[1]\hfill
\begin{tikzpicture}[overlay, remember picture]
\coordinate (end the line) at (0,0) ;
\end{tikzpicture}
\AtBeginShipoutNext{%
\hbox{%
\begin{tikzpicture}[overlay, remember picture]
\draw[solid, MYcolor,line width=1cm ,opacity=1] (begin the line) -- (end the line);
\end{tikzpicture}%
}%
}%
}%
\end{minipage}%

\end{document}


I said "something similar" because the code used in real-life is more complex as you need to take into account the temporal asynchrony of the output-routine due to which it is not ensured that the minipage in question will end up on the page which is shipped out as the next one.

My question is:

Are there less cumbersome ways of getting entire tikzpictures into the background of text that is typeset by LaTeX in non-restricted horizontal mode?

Can you do that without atbegshi and without measuring boxes and with leaving typesetting paragraphs to LaTeX's non-restricted horizontal mode?

Assuming that the question

Can you do that without atbegshi and without measuring boxes and with leaving typesetting paragraphs to LaTeX's non-restricted horizontal mode?

translates into

Can one do away with the freaking \hfill?

Yes, of course.

With tikzmark you can even add sanity checks via \iftikzmarkoncurrentpage.

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{atbegshi}
\usetikzlibrary{tikzmark}
\colorlet{MYcolor}{green}%

\begin{document}

\begin{minipage}{\textwidth}%
\tikzmark{start}
\lipsum[1]\tikzmark{end}
\AtBeginShipoutNext{%
\iftikzmarkoncurrentpage{after}%
\hbox{%
\begin{tikzpicture}[overlay, remember picture]
\path (pic cs:after) coordinate (aux1)
(pic cs:end) coordinate (aux2);
\draw[solid, MYcolor,line width=1cm ,opacity=1]
(pic cs:start) -- (aux1|-aux2);
\end{tikzpicture}%
}%
}%
\fi
}%
\end{minipage}\tikzmark{after}%

\end{document}


• Thanks for your efforts. But the freaking \hfill does not seem as freaking to me as the \AtBeginShipoutNext-thingie. Because with \AtBeginShipoutNext you need to trace whether the text with the background-image really goes on the page produced by the next \shipout... This is feasible, e.g., using zref's abspage-module for storing absolute page-numbers, and creating another \label-\ref-like-mechanism for referencing coordinates on the right pages, but it is, eh, freaking... ;-) Apr 12 '20 at 19:06
• @UlrichDiez This can be all taken care of with  \iftikzmarkoncurrentpage{after}. You can just add this to the an "unrestricted" ship out (I do not know the command because I usually use eso-pic, of course this builds on atbegshi but the macros are easier to remember) and the  \iftikzmarkoncurrentpage{after} will make sure it only does its work on the relevant page.
– user194703
Apr 12 '20 at 19:09
• It is still not clear to me how \AtBeginShipoutNext "gets to know" whether the minipage in question and the \tikzmark named after end up on the page produced by the \shipout in question. In case these things do not fit on that page it might happen that they do not end up on that page but on the next page (which will not be produced by that \shipout but by another \shipout where the directives of this \AtBeginShipoutNext do not apply any more...). Apr 12 '20 at 19:28
• @UlrichDiez This is all true, and can be solved with more elaborate tests, see e.g. tex.stackexchange.com/a/534205.
– user194703
Apr 12 '20 at 19:39
• @UlrichDiez Yes, that's what I meant. I just did not know the name of the macro in atbegshi. In the other answer I've done precisely that in the eso-pic syntax. Note also that you can have annotations that run over more than one pages that way.
– user194703
Apr 12 '20 at 20:00