How to draw tikz node behind text
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}
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}
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}