This is a follow up of my previous question Issues and potentiality of the tikzmark macro: dynamic box adaptation. I thought should have been better to ask two questions although they are related to the same argument because problems are different.
In a recent answer Horizontal alignment within an equation using Tikz, I derived a slightly different version of the macro to align text.
My solution is a bit fragile, in the sense that it strongly depends on the \textdim parameter, used to set the text width option.
Here are the examples. With this code:
\documentclass[11pt]{article}
\usepackage{xparse}
\usepackage{tikz}
\usepackage{amsmath,amssymb}
\newlength\textdim
\setlength{\textdim}{2cm}
%% code by Andrew Stacey
% https://tex.stackexchange.com/questions/51582/background-coloring-with-overlay-specification-in-algorithm2e-beamer-package#51582
\makeatletter
\tikzset{%
remember picture with id/.style={%
remember picture,
overlay,
save picture id=#1,
},
save picture id/.code={%
\edef\pgf@temp{#1}%
\immediate\write\pgfutil@auxout{%
\noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
},
if picture id/.code args={#1#2#3}{%
\@ifundefined{save@pt@#1}{%
\pgfkeysalso{#3}%
}{
\pgfkeysalso{#2}%
}
}
}
\def\savepointas#1#2{%
\expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}
\def\tmk@labeldef#1,#2\@nil{%
\def\tmk@label{#1}%
\def\tmk@def{#2}%
}
\tikzdeclarecoordinatesystem{pic}{%
\pgfutil@in@,{#1}%
\ifpgfutil@in@%
\tmk@labeldef#1\@nil
\else
\tmk@labeldef#1,\pgfpointorigin\@nil
\fi
\@ifundefined{save@pt@\tmk@label}{%
\tikz@scan@one@point\pgfutil@firstofone\tmk@def
}{%
\pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
\pgfsys@getposition{\pgfpictureid}\save@this@pic%
\pgf@process{\pgfpointorigin\save@this@pic}%
\pgf@xa=\pgf@x
\pgf@ya=\pgf@y
\pgf@process{\pgfpointorigin\save@orig@pic}%
\advance\pgf@x by -\pgf@xa
\advance\pgf@y by -\pgf@ya
}%
}
\makeatother
\NewDocumentCommand{\tikzmarkin}{m O{white} m}{%
\tikz[remember picture,overlay,baseline]
\draw[line width=1pt,rectangle,rounded corners,fill=#2,draw=none,outer sep=1pt,inner sep=1pt]
(pic cs:#1) ++(0.065,-0.32) rectangle (-0.05,0.6);
\tikz[baseline=(current bounding box.-7)] \node [align=center,text width=\textdim]at(pic cs:#1){\ensuremath{#3}}
;}
\newcommand\tikzmarkend[2][]{%
\tikz[remember picture with id=#2] #1;}
\begin{document}
\begin{alignat*}{3}
A &= \tikzmarkin{a}[red!20]{\dfrac{\partial u}{\partial x} + \lambda}\tikzmarkend{a}&&+ \dfrac{\partial f}{\partial y}\dfrac{\partial f}{\partial z}\\
B &= \tikzmarkin{b}[blue!20]{\dfrac{\partial u}{\partial z} + \dfrac{\partial^2 u}{\partial x}} \tikzmarkend{b}&&+ \gamma \\
CD &=\tikzmarkin{c}[green!20]{\dfrac{\partial f}{\partial x}}\tikzmarkend{c}&& + \dfrac{\partial f}{\partial z} \\
E &= \tikzmarkin{d}[orange!20]{\Gamma(x)}\tikzmarkend{d} &&+ \Xi(y)
\end{alignat*}
\end{document}
it is possible to get:

Let's now do a simple modification:
\setlength{\textdim}{4cm}
After having compiled twice, you get:

which is really unexpected. This behavior could be corrected by setting:
\tikz[baseline=(current bounding box.-3)]...
to achieve:

Is there any particular reasons for which an horizontal change affects the vertical positioning?


\pgfpointoriginby(0,0)in the coordinate system declaration in the\elsebranch of the\ifpgfutil@in@- my fault on that one. Secondly, this seems an odd use of the\tikzmark. You are putting the stuff that you are highlighting inside the node and the\tikzmarkwas developed for precisely when this wasn't a reasonable idea. – Andrew Stacey May 29 '12 at 18:33\tikzmarkis just a minor use, but this solution for the general case maybe is not acceptable. I will try to look for a different solution.. – Claudio Fiandrino May 29 '12 at 18:52