# How to draw tikz node behind text

I am trying to annotate an equation using tikz. I'd like to highlight a part of that equation using a tikz node, but the left part of the equation gets covered:

\documentclass[varwidth, border={0pt 4pt 0pt 4pt}]{standalone}
\usepackage{tikz}
\begin{document}
$y(x) = g(x)f(x)h(x)$

\bigskip
$y(x) = g(x) \tikz[remember picture, overlay]{ \node[fill=gray!20, inner sep=4pt, anchor=text, rectangle, rounded corners=1mm]{$f(x)$}; } \phantom{f(x)} h(x)$
\end{document}


The first part of the equation, g(x), gets covered by the node, while the right hand side, h(x), doesn't, because it appears after the node in the LaTeX code:

Instead, I would like to have the node in the background, behind the rest of the equation, while preserving the original spacing within the equation:

• The easiest is to write the whole RHS in TikZ and paint, send to background etc. as you wish – percusse Oct 2 '17 at 13:39

TikZ may be an overkill for such simple tasks, you can try tcolorbox, mdframed, fancybox, etc. If you still like TikZ, your example can be easily modified.

\documentclass[12pt,a4paper]{book}
\usepackage{tikz,amsmath}
\usepackage{graphicx}
\begin{document}

\begin{equation*}
y(x) = g(x)\,
\tikz[remember picture, overlay]{
\node[fill=gray!20, inner sep=1pt, anchor=text, rectangle, rounded corners=1mm]{$f(x)$};
}
\phantom{f(x)}\,
h(x)
\end{equation*}

\end{document}


• Thanks @AboAmmar. Unfortunately this doesn't produce the result I'm looking for, so I tried to rephrase my question so that my specific needs are clearer. – pmk Oct 2 '17 at 13:10

Here is another answer with the exactly correct spacing. In contrast to my other answer, this one doesn't need layers.

\documentclass[border={0pt 2pt 0pt 2pt}]{standalone}

\usepackage{tikz}

\begin{document}
$y(x) = %% remember position of g(x), but don't draw it yet: \tikz[remember picture,baseline=(gx.base)]{ \node [inner sep=0,anchor=text] (gx) {\phantom{$g(x)$}}; } %% draw f(x) and g(x) in the correct order: \tikz[remember picture,baseline=(fx.base)]{ %% remember position of g(x), but don't draw it yet: \node [inner sep=0,anchor=text] (fx) {\phantom{$f(x)$}}; } %% Now draw everything in the correct order: \tikz[remember picture,overlay]{ %% overlay, because the space is already taken by the \phantoms %% draw the gray box: \node at (fx.center) [anchor=center] %% exact position as f(x) [fill=gray!20, inner sep=2pt, rectangle, rounded corners=1mm] {$f(x)$} %% exact same size as f(x) ; %% draw g(x): \node at (gx.center) [anchor=center] %% exact position of g(x) {$g(x)$}; } h(x)$
\end{document}

• I had thought about that, but it's a different approach (except one common idea), so I decided to separate them. (See also, e.g., meta.stackexchange.com/a/60451) – Scz Oct 2 '17 at 13:46
• Many thanks @Scz, this is exactly what I was looking for!! I have other (more complicated) cases where this might be difficult to implement, but this is a good basis. I'll post asgain if I fail to make it work on those cases. Thanks! – pmk Oct 2 '17 at 13:54

This answer is quite complicated, but it achieves the desired effect of keeping the exact spacing as before but with the gray box in the background:

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}

\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}

\begin{document}
\begin{equation*}
y(x) =
%% remember position of g(x), but don't draw it yet:
\tikz[remember picture,baseline=(gx.base)]{
\node [inner sep=0,anchor=text] (gx) {\phantom{$g(x)$}};
}
%% draw f(x) and g(x) in the correct order:
\tikz[remember picture,baseline=(fx.base)]{
\node [inner sep=0,anchor=text] (fx) {$f(x)$}; %% draw f(x)
%% do not take up any space for the box or g(x), as we already have that:
\begin{pgfinterruptboundingbox}
\begin{pgfonlayer}{bg} %% draw on the background layer
%% draw the gray box:
\node at (fx.center) [anchor=center] %% exact position as f(x)
[fill=gray!20, inner sep=2pt,  rectangle, rounded corners=1mm]
{\phantom{$f(x)$}} %% exact same size as f(x)
;
%% draw g(x):
\node at (gx.center) [anchor=center] %% exact position of g(x)
{$g(x)$};
\end{pgfonlayer}
\end{pgfinterruptboundingbox}
}
h(x)
\end{equation*}
\end{document}


I'd suggest to use hf-tikz (see https://tex.stackexchange.com/a/57655/1952), a package for highlighting formulas compatible with beamer.

If you load it with beamer option, \tikzmarkin command accepts overlay syntax: <1->. When deafult margins for marking are not correct (like in this case) you must adjust box margins.

\documentclass{beamer}
\usepackage[beamer]{hf-tikz}
\begin{document}
\begin{frame}
$y(x)=g(x)f(x)h(x)$

$y(x)=g(x)\tikzmarkin<2->[right offset={0.01}, left offset={0.01}]{f1}f(x)\tikzmarkend{f1}h(x)$
\end{frame}
\end{document}


• Thank you @Ignasi. However this still covers g(x) if the box is larger. – pmk Oct 3 '17 at 10:13
• @pmk Then you have to correct margins again. I didn't provide an universal solution but a way to work. – Ignasi Oct 3 '17 at 10:25

I managed to achieve this using the tikzmark library:

\documentclass[varwidth, border={0pt 4pt 0pt 4pt}]{standalone}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
% Create the node with right location and right dimensions
% This needs to placed BEFORE the actual equation
\begin{tikzpicture}[remember picture, overlay, baseline]%
\node[fill=gray!20, inner sep=4pt, anchor=text, rounded corners=1mm] at ({pic cs:mymark}) {
\phantom{$f(x)$}
};
\end{tikzpicture}
% Now write the actual equation
% and put a tikzmark at the desired position (before f(x))
$y(x) = g(x) \tikzmark{mymark} f(x) h(x)$
\end{document}


which indeed produces:

Using tcolorbox package or tikz

\documentclass{article}
\usepackage{amsmath}
\usepackage{tcolorbox}
\tcbuselibrary{skins, theorems}
\usepackage{tikz}
\usetikzlibrary{backgrounds, positioning}

\begin{document}
\begin{equation*}
y(x) = g(x)
\tcboxmath[boxsep=0mm, colframe=white, right=1mm, left=1mm]{f(x)}
h(x)
\end{equation*}

\begin{equation*}
\begin{tikzpicture}
\node[name=A, inner sep=2pt, anchor=text, rectangle, rounded corners=1mm]{$y(x) = g(x)$};
\node[name=B, right=0mm of A, fill=gray!20, inner sep=2pt, baseline, rectangle, rounded corners=1mm]{$f(x)$};
\node[name=C, right=0mm of B, inner sep=2pt, baseline, rectangle, rounded corners=1mm]{$y(x)$};
\end{tikzpicture}
\end{equation*}
\end{document}


• Thank you @flav for your answer. However, I would like to know if this is feasible using a tikz node. In addition, I would like to keep my inner sep spacing of 2pt, but I also want to keep the same spacing in the equation as if there were no box (like in my example except the box would be behind the equation). The reason for this is that I am using beamer and I want the node to appear only at a certain point. Thanks! – pmk Oct 2 '17 at 12:47
• see the edit for a tikz solution but I don't think that is the good solution – flav Oct 2 '17 at 13:02
• Thanks @flav. Unfortunately this doesn't produce the result I'm looking for, so I tried to rephrase my question so that my specific needs are clearer. – pmk Oct 2 '17 at 13:09
• @pmk Give us an image of the result you want – flav Oct 2 '17 at 14:05
• Sorry my question wasn't clear enough. I tried to rephrase again. Anyway, I found a "simple" solution using tikzmark (tex.stackexchange.com/a/394400/41558). Thanks for your help! – pmk Oct 3 '17 at 15:53