Horizontal alignment within an equation using Tikz
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
% 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,(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}%
\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.-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}
which leads to:
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.