Consider the following minimal example where I want to draw a rectangle behind a sequence of numbers (to highlight them):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc}
\NewDocumentCommand{\tikzmark}{ m }{\tikz[overlay, remember picture] \node (#1) {};}
\newcounter{seq}
\NewDocumentCommand{\seqstart}{}{%
\stepcounter{seq}% Start a new sequence
\tikzmark{seq-start-\theseq}}% Set start marker
\NewDocumentCommand{\seqend}{}{%
\tikzmark{seq-end-\theseq}% Set end marker
\tikz[remember picture, overlay]
\scoped [on background layer]
\fill [fill=blue!10!white] ($(seq-start-\theseq.south west) + (-0.5pt,1pt)$) rectangle ($(seq-end-\theseq.south east) + (0.5pt,1.1\normalbaselineskip)$);%
}
\begin{document}
\[
12, 15, 8, 11, 4, 7, 0, 3, 16, 19, 12, 15, 8, 11, 4, 7, 0, 3, \ldots
\]
\[
\seqstart{}12, 15, 8, 11, 4, 7, 0, 3, 16, 19\seqend{}, 12, 15, 8, 11, 4, 7, 0, 3, \ldots
\]
\end{document}
The idea is that \seqstart would set a (counter-driven) \tikzmark representing the start of the sequence to be marked. Then \seqend would set a \tikzmark at the end and highlight it using the background layer. The counter allows for multiple such sequence marking within a larger document.
However, the output shows that the rectangle highlighting a part of the sequence is not being drawn on background layer as requested.
Why is that? How can I ensure the rectangle is drawn on the background layer behind document/text elements?
The backgrounds library documentation (in the tikz documentation, section 45) mentions that on background layer can only be used inside a {scope} or \scoped, which is done above.





tikzmarkI've revisited highlighting text using the tikzmark mechanism. Take a look at the documentation. I think it's in the ctan version.on background layerwithblend mode=multiply