# Anchor alignment in tikzmarknode

The following code results in a strange behavior:

\begin{align*}
\sum\limits_{i=1}^{3}\sum\limits_{j=1}^{4} a_{ij}&=
\tikzmarknode{red1}{\highlight[red!20]{\sum\limits_{j=1}^{3}a_{1j}}} +
\tikzmarknode{green1}{\highlight[green!20]{\sum\limits_{j=1}^{3}a_{2j}}} +
\tikzmarknode{blue1}{\highlight[blue!20]{\sum\limits_{j=1}^{3}a_{3j}}}\\
&\\&=
\tikzmarknode{red2}{\highlight[red!20]{a_{11} + a_{12} + a_{13} + a_{14}}} +
\tikzmarknode{green2}{\highlight[green!20]{a_{21} + a_{22} + a_{23} + a_{24}}} +
\tikzmarknode{blue2}{\highlight[blue!20]{a_{31} + a_{32} + a_{33} + a_{34}}}\\
\end{align*}
\begin{tikzpicture}[overlay, remember picture]
\draw[->] (pic cs:red1.south) to (pic cs:red2.north);
\draw[->] (pic cs:green1) to (pic cs:green2);
\draw[->] (pic cs:blue1) to (pic cs:blue2);
\end{tikzpicture}


When I'm not using anchors (i.e. in green1, green2, blue1 and blue2) it behaves as expected*, but if I try to anchor the arrows to the south or north of the tikzmarknode (i.e. red1 and red2) the appears on the bottom-left side of the picture. If I change red2.north to just red2, then that anchor is ok, but red1.south still insists on being in the bottom left.

Any suggestions at how to solve this?

*not what I want to have eventually, but at least it works properly.

• Can you please provide a complete code that starts with \documentclass and loads all the packages and/or provides all commands. In particular, there are several versions of \highlight and it is not clear which you are using.
– user121799
Jun 9 '19 at 1:03

The technical reason for your problems is that pic cs: is not appropriate here. That is, strictly speaking I do not know what is appropriate in your code as you do not show the preamble. Assuming you are loading the tikzmark library, it is not. In more detail, the later versions of tikzmark have a command \tikzmarknode, which you may or may not be using (hard to tell without preamble). If you are using it, you do not really use its features. You can forget about all this \highlight stuff and just use the corresponding directives in the options of \tikzmarknode. This also solves the problem mentioned in your question.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begingroup\tikzset{every tikzmarknode/.append style={inner sep=3pt,rounded
corners}}
\begin{align*}
\sum\limits_{i=1}^{3}\sum\limits_{j=1}^{4} a_{ij}&=
\tikzmarknode[fill=red!20]{red1}{\sum\limits_{j=1}^{4}a_{1j}} +
\tikzmarknode[fill=green!20]{green1}{\sum\limits_{j=1}^{4}a_{2j}} +
\tikzmarknode[fill=blue!20]{blue1}{\sum\limits_{j=1}^{4}a_{3j}}\\
\\
&=
\tikzmarknode[fill=red!20]{red2}{a_{11} + a_{12} + a_{13} + a_{14}} +
\tikzmarknode[fill=green!20]{green2}{a_{21} + a_{22} + a_{23} + a_{24}} +
\tikzmarknode[fill=blue!20]{blue2}{a_{31} + a_{32} + a_{33} + a_{34}}
\end{align*}\endgroup
\begin{tikzpicture}[overlay, remember picture,>=stealth,very thick]
\draw[->,red!20] (red1.south) to[out=-90,in=120,looseness=0.3] (red2.north);
\draw[->,green!20] (green1) to[out=-90,in=120,looseness=0.3] (green2);
\draw[->,blue!20] (blue1) to[out=-90,in=135,looseness=0.3] (blue2);
\end{tikzpicture}
\end{document}


The current version of tikzmark that is on CTAN has some bug that is fixed in it the GitHub version. So you could either download it from there, or temporarily work with:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\newcommand{\tikznode}[3][]{%based on https://tex.stackexchange.com/a/402466/121799
\ifmmode%
\tikz[remember picture,baseline=(#2.base),inner sep=0pt] \node[#1] (#2) {$#3$};%
\else
\tikz[remember picture,baseline=(#2.base),inner sep=0pt] \node[#1] (#2) {#3};%
\fi}
\begin{document}
\begingroup\tikzset{every node/.append style={inner sep=3pt,rounded
corners}}
\begin{align*}
\sum\limits_{i=1}^{3}\sum\limits_{j=1}^{4} a_{ij}&=
\tikznode[fill=red!20]{red1}{\sum\limits_{j=1}^{4}a_{1j}} +
\tikznode[fill=green!20]{green1}{\sum\limits_{j=1}^{4}a_{2j}} +
\tikznode[fill=blue!20]{blue1}{\sum\limits_{j=1}^{4}a_{3j}}\\
\\
&=
\tikznode[fill=red!20]{red2}{a_{11} + a_{12} + a_{13} + a_{14}} +
\tikznode[fill=green!20]{green2}{a_{21} + a_{22} + a_{23} + a_{24}} +
\tikznode[fill=blue!20]{blue2}{a_{31} + a_{32} + a_{33} + a_{34}}
\end{align*}\endgroup
\begin{tikzpicture}[overlay, remember picture,>=stealth,very thick]
\draw[->,red!20] (red1.south) to[out=-90,in=120,looseness=0.3] (red2.north);
\draw[->,green!20] (green1) to[out=-90,in=120,looseness=0.3] (green2);
\draw[->,blue!20] (blue1) to[out=-90,in=135,looseness=0.3] (blue2);
\end{tikzpicture}
\end{document}


which yields the same output (in this example).

• Wow, thanks! The second option you provided worked exactly as it should. The first option, though, creates issues: it complains that it doesn't know red1 (and the rest of the nodes). I'm loading tikzmark exactly as you did, with \usepackage{tikz} and \usetikzlibrary{tikzmark}. Not sure why this is happening. Jun 9 '19 at 9:05
• @pelegs Yes, you are right. I added a link to the GitHub version of tikzmark which doesn't have the problem.
– user121799
Jun 9 '19 at 21:05