# Alignments by means of the tikzmark macro

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
% http://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}%
}%
}
\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?

-
Two minor comments: replace \pgfpointorigin by (0,0) in the coordinate system declaration in the \else branch 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 \tikzmark was developed for precisely when this wasn't a reasonable idea. –  Andrew Stacey May 29 '12 at 18:33
Thanks for the comments. :) This suggest to me I'm on the wrong way now because also in tex.stackexchange.com/a/57655/13304 I put inside the node the text to highlight. I think the text alignment by means of the \tikzmark is 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
If you're putting the stuff-to-be-highlighted inside a TikZ node then do the highlighting in the same tikzpicture - it's much, much easier. The problem is that you can't always put the stuff inside a node: it may be a line in a paragraph that breaks, or it may be a column of a matrix. Those are the situations where the tikzmark is useful, and in those situations it isn't always possible to automatically measure stuff. –  Andrew Stacey May 29 '12 at 19:00
Yes, indeed in my mind I see two main ways of using the macro: the first is the "standard" usage with delimiters (example tex.stackexchange.com/questions/57101/…) and the second one is when stuff to highlight - and even align - is on one line. From a user point of view, with basically three commands, it will be possible to do a lot of things. –  Claudio Fiandrino May 29 '12 at 19:09

The reason for the shift is that you are setting the baseline of the tikzpicture by using a point on the boundary of the bounding box that is specified by an angle. This is in the key baseline=(current bounding box.-7). This refers to the point on the boundary given by the angle -7 degrees. As the aspect ratio of the picture changes, the height of this changes. Here's an example:

Notice in particular how the boundary point at angle 20 shifts from the top to the side as the aspect ratio changes.

Given that you are putting text in a node inside the tikzpicture, probably the best coordinate to use to get the baseline correct is the base anchor of this node. Thus:

\tikz[baseline=(h.base)] \node (h) [align=center,text width=\textdim]at(pic cs:#1){\ensuremath{#3}};


(maybe choose a better node name, though it isn't all that important). This results in:

which is what I guess you want.

Here's the full code for that:

\documentclass[11pt]{article}
%\url{http://tex.stackexchange.com/q/57538/86}
\thispagestyle{empty}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{amsmath,amssymb}

\newlength\textdim
\setlength{\textdim}{2cm}
\setlength{\textdim}{4cm}

%% code by Andrew Stacey
% http://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,(0pt,0pt)\@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}%
}%
}
\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=(h.base)] \node (h) [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}


(Note also the correction to the coordinate system as per my comment to the question.)

-