# tikzpicture behind text

Is there a way to push a tikzpicture behind text?

MWE:

\documentclass{article}

% -- packages
\usepackage{tikz}
\usepackage{pgf}
\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

% -- indeces highlight
\newcommand{\xhl}[1][x]{
\rcolor{red!50}{#1}
}
\newcommand{\yhl}[1][y]{
\rcolor{blue!50}{#1}
}

% -- document
\begin{document}
\begin{equation*}
\begin{pmatrix}
\tikznode{start}{a_{\xhl[1]\yhl[1]}} & a_{\xhl[1]\yhl[2]} & \cdots & a_{\xhl[1]\yhl[n]}\\
a_{\xhl[2]\yhl[1]} & a_{\xhl[2]\yhl[2]} & \cdots & a_{\xhl[n]\yhl[n]}\\
\vdots & \vdots & \ddots & \vdots\\
a_{\xhl[n]\yhl[1]} & a_{\xhl[n]\yhl[2]} & \cdots & \tikznode{end}{a_{\xhl[n]\yhl[n]}}\\
\end{pmatrix}
\end{equation*}

% -- TikZ part
\begin{tikzpicture}[overlay, remember picture]
\begin{pgfonlayer}{background layer}
\draw[-, line width=5mm, cap=round, green, opacity=0.35] (start.north west) -- (end.south east);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}


What I get is

But what I would like to have is the green line being behind the matrix elements.

Any help would be appreciated.

• Yes. But your code is not all compilable. If it was, you could use atbegshi or eso-pic to add this on the background. Nowadays one can use the nicematrix package to obtain such results. – user194703 Apr 15 '20 at 18:37

One way to make the shading appear to be on the background is to use blend mode = multiply. This is the method suggested by the nicematrix manual, which allows one to produce nice(r) matrices.

\documentclass{beamer}
\usepackage{tikz}
\usepackage{nicematrix}
% -- indeces highlight
\newcommand{\xhl}[1][x]{%
\textcolor{red!50}{#1}
}
\newcommand{\yhl}[1][y]{%
\textcolor{blue!50}{#1}
}

\begin{document}
\begin{frame}[t]
\frametitle{A matrix}

$\begin{pNiceMatrix} a_{\xhl[1]\yhl[1]} & a_{\xhl[1]\yhl[2]} & \Cdots & a_{\xhl[1]\yhl[n]}\\ a_{\xhl[2]\yhl[1]} & a_{\xhl[2]\yhl[2]} & \Cdots & a_{\xhl[n]\yhl[n]}\\ \Vdots & \Vdots & \Ddots & \Vdots\\ a_{\xhl[n]\yhl[1]} & a_{\xhl[n]\yhl[2]} & \Cdots & a_{\xhl[n]\yhl[n]} \CodeAfter \tikz [blend mode=multiply] \draw[-, line width=6mm, cap=round, green, opacity=0.35] ([xshift=1ex]1-1.north west) -- ([xshift=-1ex]4-4.south east); \end{pNiceMatrix}$
\end{frame}
\end{document}


This method works great for black texts but isn't spotless for colored texts as in this question. So her is a compilable version of your code with the annotation in the background.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{amsmath}
\usepackage{eso-pic}
% -- indeces highlight
\newcommand{\xhl}[1][x]{
\textcolor{red!50}{#1}
}
\newcommand{\yhl}[1][y]{
\textcolor{blue!50}{#1}
}

% -- document
\begin{document}
\begin{equation*}
\begin{pmatrix}
\tikzmarknode{start}{a_{\xhl[1]\yhl[1]}} & a_{\xhl[1]\yhl[2]} & \cdots & a_{\xhl[1]\yhl[n]}\\
a_{\xhl[2]\yhl[1]} & a_{\xhl[2]\yhl[2]} & \cdots & a_{\xhl[n]\yhl[n]}\\
\vdots & \vdots & \ddots & \vdots\\
a_{\xhl[n]\yhl[1]} & a_{\xhl[n]\yhl[2]} & \cdots & \tikzmarknode{end}{a_{\xhl[n]\yhl[n]}}\\
\end{pmatrix}
\end{equation*}

\begin{tikzpicture}[overlay, remember picture]
\draw[-, line width=5mm, cap=round, green, opacity=0.35]
([xshift=0.5ex]start.north west) -- ([xshift=-0.5ex]end.south east);
\end{tikzpicture}}
\end{document}


For beamer you do not need eso-pic.

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{amsmath}
% -- indeces highlight
\newcommand{\xhl}[1][x]{%
\textcolor{red!50}{#1}
}
\newcommand{\yhl}[1][y]{%
\textcolor{blue!50}{#1}
}

% -- document
\begin{document}
\begin{frame}[t]
\frametitle{Test}

\end{frame}

\setbeamertemplate{background}{\begin{tikzpicture}[overlay, remember picture]
\draw[-, line width=5mm, cap=round, green, opacity=0.35]
([xshift=0.5ex]start.north west) -- ([xshift=-0.5ex]end.south east);
\end{tikzpicture}}

\begin{frame}[t]
\frametitle{A matrix}

\begin{equation*}
\begin{pmatrix}
\tikzmarknode{start}{a_{\xhl[1]\yhl[1]}} & a_{\xhl[1]\yhl[2]} & \cdots & a_{\xhl[1]\yhl[n]}\\
a_{\xhl[2]\yhl[1]} & a_{\xhl[2]\yhl[2]} & \cdots & a_{\xhl[n]\yhl[n]}\\
\vdots & \vdots & \ddots & \vdots\\
a_{\xhl[n]\yhl[1]} & a_{\xhl[n]\yhl[2]} & \cdots & \tikzmarknode{end}{a_{\xhl[n]\yhl[n]}}\\
\end{pmatrix}
\end{equation*}
\end{frame}

\setbeamertemplate{background}{}

\begin{frame}[t]
\frametitle{Another test}

\end{frame}
\end{document}


• Thanks, but it doesn't work for me: the green highlight disappears. Perhaps it's because the actual code (from which I made the MWE) is in a Beamer frame? – pelegs Apr 15 '20 at 18:51
• Please accept my sincere apologies - the actual code is a part of a big project with many files that define a lot of things. I tried to clear the code as much as possible, but I assumed (incorrectly) that the MWE should work without testing it. Many thanks for your help. – pelegs Apr 15 '20 at 19:19

You can use a savebox and put the entire matrix inside the tikzpicture.

\documentclass{standalone}
\usepackage{amsmath}
% -- packages
\usepackage{tikz}
\usetikzlibrary{backgrounds}

% -- indeces highlight
\newcommand{\xhl}[1][x]{
\textcolor{red!50}{#1}
}
\newcommand{\yhl}[1][y]{
\textcolor{blue!50}{#1}
}
\newsavebox{\tempbox}
\newcommand{\tikznode}[2]{\tikz[remember picture,baseline=(#1.base)]{\node[inner sep=0pt](#1){$#2$};}}

% -- document
\begin{document}
\savebox{\tempbox}{$\begin{pmatrix} \tikznode{start}{a_{\xhl[1]\yhl[1]}} & a_{\xhl[1]\yhl[2]} & \cdots & a_{\xhl[1]\yhl[n]}\\ a_{\xhl[2]\yhl[1]} & a_{\xhl[2]\yhl[2]} & \cdots & a_{\xhl[n]\yhl[n]}\\ \vdots & \vdots & \ddots & \vdots\\ a_{\xhl[n]\yhl[1]} & a_{\xhl[n]\yhl[2]} & \cdots & \tikznode{end}{a_{\xhl[n]\yhl[n]}}\\ \end{pmatrix}$}

% -- TikZ part
\begin{tikzpicture}[remember picture]
\node[inner sep=0pt] (P) {\usebox\tempbox};
\begin{scope}[on background layer]
\draw[-, line width=5mm, cap=round, green, opacity=0.35] (start.north west) -- (end.south east);
%\draw[-, line width=5mm, cap=round, green, opacity=0.35] (P.north west) -- (P.south east);
\end{scope}
\end{tikzpicture}
\end{document}