I naively thought that this would be a simple fix for highlighting text -- yes, there are ulem.sty
and soul.sty
but they impose some restrictions with which I would prefer not to deal.
I assumed it would be useful to use something like tikzmarks
. And so it is, except that the shading rectangles, though to all appearances on a background layer, actually overprint the text they should be highlighting.
Can this work? What have I missed? (Using TexLive2022, updated yesterday with Aquamacs/Skim). I should point out that this will always be used on a single page.
\documentclass{article}
\usepackage[papersize={5.5in,8.5in},margin=0.6in]{geometry}
\usepackage{tikzpagenodes}
\usetikzlibrary{backgrounds}
%% |=====8><-----| %%
\newcounter{shadenum}
\NewDocumentCommand{\btshadepar}{}{%
\stepcounter{shadenum}%
\leavevmode
\begin{tikzpicture}[remember picture,overlay]
\node[inner sep=0pt](bps-\theshadenum){\strut};
\end{tikzpicture}%
}%
\NewDocumentCommand{\etshadepar}{}{%
\begin{tikzpicture}[remember picture,overlay]
\node[inner sep=0pt,outer sep=-3pt](eps-\theshadenum){\strut};
\begin{scope}{on background layer}
\fill[yellow!20] ([yshift=3pt]bps-\theshadenum.north) rectangle
(bps-\theshadenum.south -| current page text area.east);% first line
\fill[yellow!20] ([yshift=3pt]eps-\theshadenum.north) rectangle
(eps-\theshadenum.south -| current page text area.west);% last line
\fill[yellow!20] ([yshift=3pt]bps-\theshadenum.south -| current page text area.west) rectangle
(eps-\theshadenum.north -| current page text area.east);% body of paragraph
\end{scope}
\end{tikzpicture}%
}%
\begin{document}
\thispagestyle{empty}
This is a gigantic paragraph that means \emph{absolutely} nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a \tikz{\draw[fill=red] (0,0) rectangle (1ex,1ex);} gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. \[x^2+y^2=z^2\] This is a \textbf{gigantic} paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all.
\btshadepar This is a gigantic paragraph that means \emph{absolutely} nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a \tikz{\draw[fill=red] (0,0) rectangle (1ex,1ex);} gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. \[x^2+y^2=z^2\] This is a \textbf{gigantic} paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all.\etshadepar
\end{document}
Update
I've altered the text to indicate the general sort of issues with which the code must cope (display math, a small tikz
object). All of the replies thus far (@samcarter_is_at_topanswers.xyz and @Qrrbrbirlbel) cope perfectly with this.