# Horizontal alignment within an equation using Tikz

I am trying to center horizontally the first terms of a system of equations inside the alignat environment. I don't know if Tikz is the right way to go, but as I might have to add some other "decorations" afterwards, it might be the easiest way.

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\begin{alignat*}{3}
A &= \dfrac{\partial u}{\partial x} + \lambda &&+ \dfrac{\partial f}{\partial y}\dfrac{\partial f}{\partial z} \\
B &= \dfrac{\partial u}{\partial z} + \dfrac{\partial^2 u}{\partial x}&&+ \gamma  \\
CD &= \dfrac{\partial f}{\partial x} &&+ \dfrac{\partial f}{\partial z} \\
E &= \Gamma(x) &&+ \Xi(y)
\end{alignat*}
\end{document}


So here goes: basically I want to create nodes in which I will use text align=centered (or something similar, you get the idea) that are the exact same width as the largest node (T), and that will adjust the height of the line (i.e. if the text height of the node is smaller than the actual height of the line, it should stay the same, or expand vertically if the line cannot fit the node).
I have different issues with the following code:

\begin{alignat*}{3}
A &= \dfrac{\partial u}{\partial x} + \lambda &&+ \dfrac{\partial f}{\partial y}\dfrac{\partial f}{\partial z} \\
B &= \tikz[remember picture, baseline]{\node[fill=blue!20,anchor=base,inner sep=0pt,outer sep=0pt] (T) {$\dfrac{\partial u}{\partial z} + \dfrac{\partial^2 u}{\partial x}$};} &&+ \gamma  \\
CD &=
\tikz[baseline={([yshift=-2.7pt]current bounding box.center)}]{\node[outer sep=0pt,inner sep=0pt,fit={(T)},fill=green!20,label=center:$\dfrac{\partial f}{\partial x}$] {};}
%\dfrac{\partial f}{\partial x}
&& + \dfrac{\partial f}{\partial z} \\
E &= \Gamma(x) &&+ \Xi(y)
\end{alignat*}

1. If you comment line 5 and uncomment line 6, you can clearly see that the new node expand the line vertically, which is not needed.
2. Is it possible to add the text of the label within the curly brackets, instead of using the "label=center:" (obviously it is not working right now).
3. How can I get the correct vertical alignment, without having to specify, by hand [yshift=-2.7pt] for the baseline? If one removes that, one can clearly see that it is not right.
4. I would like to use the same technic to align the first terms of the first line A. However, at that point, node (T) is not known. Is there some way to make it possible, using "remember picture" or something like that, and compiling twice the .tex ?

PS: I'm in no way sure that I'm going in the right direction with this, especially the "fit", so please feel free to provide other technics instead of "fixing" my problems in this given piece of code.

I was being stupid, @percusse answer did (almost) the trick:

\def\biggest{$\dfrac{\partial u}{\partial z} + \dfrac{\partial^2 u}{\partial x}+3$}
\newdimen{\blabla}
\settowidth{\blabla}{\biggest}
\tikzset{mynode/.style={anchor=base,inner sep=0pt,outer sep=0pt,text width=\blabla,align=center}} % If you wish add: minimum height=2em
\newcommand{\putintonode}[2]{\tikz[baseline]{\node[#1]{#2};}}
\begin{alignat*}{3}
A  &= \putintonode{mynode}{$\dfrac{\partial u}{\partial x} + \lambda$} &&+ \dfrac{\partial f}{\partial y}\dfrac{\partial f}{\partial z} \\
B  &= \putintonode{fill=blue!20,mynode}{\biggest} &&+ \gamma  \\
CD &= \putintonode{fill=green!20,mynode}{$\dfrac{\partial f}{\partial x}$} && + \dfrac{\partial f}{\partial z} \\
E  &= \putintonode{mynode}{$\Gamma(x)$} &&+ \Xi(y)
\end{alignat*}

-

I have to say that I didn't get the real issue probably but I think instead of over-automating, you can simplify a little bit by selecting a text width (and height if you like) and adjust it afterwards.

\documentclass[10pt]{article}
\usepackage{tikz,amsmath,amssymb}
\tikzset{mynode/.style={anchor=base,inner sep=0pt,outer sep=0pt,text width=1.5cm,align=center}} % If you wish add: minimum height=2em
\newcommand{\putintonode}[2]{\tikz[baseline]{\node[#1]{#2};}}
\begin{document}
\begin{alignat*}{3}
A  &= \putintonode{mynode}{$\dfrac{\partial u}{\partial x} + \lambda$} &&+ \dfrac{\partial f}{\partial y}\dfrac{\partial f}{\partial z} \\
B  &= \putintonode{fill=blue!20,mynode}{$\dfrac{\partial u}{\partial z} + \dfrac{\partial^2 u}{\partial x}$} &&+ \gamma  \\
CD &= \putintonode{fill=green!20,mynode}{$\dfrac{\partial f}{\partial x}$} && + \dfrac{\partial f}{\partial z} \\
E  &= \putintonode{mynode}{$\Gamma(x)$} &&+ \Xi(y)
\end{alignat*}
\end{document}


-

To accomplish this task always using TikZ, I would like to propose a solution based on a derived tikzmark macro.

Here is the code:

\documentclass[11pt]{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc}
\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,(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=(current bounding box.-8)] \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}{\Gamma(x)}\tikzmarkend{d} &&+ \Xi(y)
\end{alignat*}

\end{document}


How coordinates are computed, saved and used is still based on Andrew's code in Background coloring with overlay specification in algorithm2e + beamer package. What changes is the definition of the \tikzmarkin: it has, of course, a parameter to identify the marker, an optional argument for the color (less relevant) and a parameter to insert the text that will be in some sense manipulated.

\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.-8)] \node [align=center,text width=\textdim]at(pic cs:#1){\ensuremath{#3}}
;}


By setting the optional parameter to white as default it is possible to not color the background while still aligning the text.

This allows, without effort, to get a result like:

with a small change (the position and the color of the end marker):

E &= \tikzmarkin{d}[orange!20]{\Gamma(x)+ \Xi(y)}\tikzmarkend{d}


EDIT

To see a potential problem of this method and its solution, refer to Alignments by means of the tikzmark macro.

-