arrows in equations with TikZ

I'm trying my hand at adding arrows in my documents, similar to how I would do this on my papers (i.e., handwritten notes). I'd love to indicate the answer to some lengthy arithmetic by placing an arrow from the equation number (tagged, or otherwise) back to the final answer. Please see the MWE below.

\documentclass{article}

\usepackage{amsmath,tikz}

\begin{tikzpicture}%
\hspace{0.25cm} \draw [<-] (0,0) -- (5,0);
\end{tikzpicture}\tag{{\LARGE{$\star$}}}%
}

\begin{document}

I'd love to change the solution of
\begin{align}
A &= x + y - z \nonumber \\
&= 5 + (-2) - (-0.3) \nonumber \\
&= 3.3
\end{align}

to this,
\begin{align}
A &= x + y - z \nonumber \\
&= 5 + (-2) - (-0.3) \nonumber \\
\end{align}

... but with the equation kept centered and fill only the space between the answer'' and the tagged equation with an arrow.

\end{document}


Any/All help is much appreciated.

For questions like this tikzmark is your friend: you can create a mark at the end of the equation and then draw your arrow from inside a \tag command. For example, one way to automate this is with:

\newcommand\Tag[1]{%
\tikzmark{mytag}%
\tag{\tikz[remember picture]{%
\draw[overlay, ->](-0.5,0)--(pic cs:mytag)}\Large$#1$}%
}


This command is used as \Tag{\star}.

In fact, this command is not quite enough as the tag name is not dynamic. My initial thought was to use the equation counter but this does not work because \tag does not increment the equation. Instead, let's define a new counter, say Tag, and then use this for the labels inside \tikzmark.

This is enough to define the basic command but let's go a little further and add an optional argument for styling the TikZ arrow. It would also be nice to be able to draw these arrows from an equation number, which should be the default. As we already have an optional argument for styling the arrow we can use the \NewDocumentCommand command from xparse to require a second optional argument that is enclosed by parentheses to replace the equation number with a symbol, such as \star. That is, \Tag will draw an arrow from the equation number, \Tag(\star) will draw from a \star and then \Tag[red] and \Tag[red](\star), respectively, will colour these arrows red.

With this in place the MWE below produces:

Here is the full code.

\documentclass{article}

\usepackage{amsmath,tikz,xparse}
\usetikzlibrary{tikzmark, arrows.meta}
\usetikzlibrary{decorations.pathmorphing} % for the coil

\newcounter{Tag}
\tikzset{
Tag/.style = {% default styling for the \Tag arrow
arrows=-{LaTeX},
blue
}
}
\NewDocumentCommand\Tag{ O{} d() }{%
\refstepcounter{Tag}         % increment Tag counter for unique tags
\tikzmark{tagging \theTag}   % create the tikzmark
\tag{\tikz[remember picture,overlay]{% tag equation and point to mark
\draw[Tag, #1](-0.2,0.1)--([shift={(0.2,0.1)}]pic cs:tagging \theTag);}{%
\IfNoValueTF{#2}{\refstepcounter{equation}\theequation}{$#2$}
}%
}%
}

\begin{document}

\begin{align}
A &= x + y - z        \Tag \\
&= 5 + (-2) - (-0.3)\Tag[red](\star) \\
&= 3.3              \Tag[orange,decorate,decoration={coil,aspect=0}](\ast)
\end{align}

\end{document}


Note that I have created a Tag style using \tikzset to make styling of the arrow easier. By default, LaTeX arrow tips are used and the arrow is blue. Also, as is always the case when you use tikzmark, you need to compile the document twice before the marks start working.

• This is exactly what I was looking for... and then some. Love the flexibility of keeping the equations numbered (or not). I also thank you for the added benefit of including the star/asterisk. Thank you very much. Sep 12 '20 at 13:54