TikZ-PGF – Better Solution to Display the Distributive Property

tikz-pgf

Trying to apply the solution provided for the question How to draw arrows between parts of an equation to show the Math Distributive Property (Multiplication)? I have found a major problem as is shown in my example below:

\documentclass{article}
\usepackage{amsmath}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[2]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[->,shorten >=5pt,shorten <=5pt,out=70,in=130,distance=0.5cm,#1] (MarkA.north) to (MarkC.north);
    \draw[->,shorten >=5pt,shorten <=5pt,out=50,in=140,distance=0.3cm,#2] (MarkA.north) to (MarkB.north);
  \end{tikzpicture}
}
\begin{document}
\[\tikzmark{MarkA}a(b\tikzmark{MarkB}+c\tikzmark{MarkC})=ab+ac \DrawBox{OrangeRed}{Cerulean}\]

\begin{align*}
-(2x+5)&=(-\tikzmark{MarkA}1)(2x\tikzmark{MarkB}+5\tikzmark{MarkC})\DrawBox{OrangeRed}{Cerulean}\\
       &=(-1)(2x)+(-1)(5)\\
       &=-2x+(-5)\\
       &=-2x-5
\end{align*}

\end{document}

This gives:

enter image description here

To start with I don't like how the arrows leave and enter the node. I would like to able to control the height of the arc. Secondly, the arrow is not pointing correctly above the right node. I understand the command is not set to take the character and let it act as a node like in Jakes answer. This is a downfall.

Next, even when I tried Jakes method, there can only be one source but several targets. How can one define several sources. I like Peter Grill's answer since one can define several tikzmark's. This is very useful but in terms of appearance it is not the best. The color scheme I have chosen I wish to maintain standard but the other parameters I mentioned above I would like to be able to customize.

Hence, the arcs must be a suitable height (maybe let TikZ do the calculations) for this and that there can be several sources and targets without defining new \tikzmarks. Its somewhat a combination of both answers.

EDIT

I think that I am understanding the \tikzmark command but here is my major problem that can be avoided if the mathmode characters are selected as nodes as Jakes answer does.

This is the example where I see the most problem (probably its just my lack of understanding of TikZ and nodes.)

\documentclass[letterpaper]{article}
\usepackage{amsmath,amssymb}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[2]{%
  \begin{tikzpicture}[overlay,remember picture]
    \node[xshift=0.85cm,yshift=0.65cm] (MarkExp) {\footnotesize Exponent};
    \node[xshift=-0.85cm,yshift=-0.35cm] (MarkBase) {\footnotesize Base};
    \draw[->,shorten >=6pt,out=180,in=90,#1] (MarkExp.west) to ([xshift=-2.5pt]MarktoExp.north);
    \draw[->,out=0,in=270,#2] (MarkBase.east) to ([xshift=2.5pt,yshift=2pt]MarktoBase.south);
  \end{tikzpicture}
}
\begin{document}
$\tikzmark{MarktoBase}2^3\tikzmark{MarktoExp}$\DrawBox{OrangeRed}{Cerulean}
\end{document}

enter image description here

I feel that in my solution above, I have forced the arrows to fit what I want even though it does the job. I sincerely believe there is a better method to achieve the same output as above.

Best Answer

This already has the structure built in for you to provide other options to control the lines that are drawn. You can adjust the

  • out= to control the angle at the start of the arrow,
  • in= to control the angle at the end of the arrow,
  • distance= to control the height of the arrow,
  • and the two shorten to adjust how close the start and end are to the particular node.

There are probably other options, that should be in the PGF manual related to the syntax of using to instead of -- for line drawing.

enter image description here

Notes:

  • I also moved the position of the in: 2\tikzmark{MarkB}x so that the arrow points to the center of the 2x term.

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[2]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[->,shorten >=5pt,shorten <=5pt,out=70,in=130,distance=0.5cm,#1] (MarkA.north) to (MarkC.north);
    \draw[->,shorten >=5pt,shorten <=5pt,out=50,in=140,distance=0.3cm,#2] (MarkA.north) to (MarkB.north);
  \end{tikzpicture}
}
\begin{document}
\[\tikzmark{MarkA}a(b\tikzmark{MarkB}+c\tikzmark{MarkC})=ab+ac \DrawBox{OrangeRed,distance=0.75cm,in=110,shorten >=3pt}{Cerulean,out=60,in=110,distance=0.5cm}\]

\begin{align*}
-(2x+5)&=(-\tikzmark{MarkA}1)(2\tikzmark{MarkB}x+5\tikzmark{MarkC})\DrawBox{OrangeRed,distance=0.75cm}{Cerulean,out=60,in=110,distance=0.5cm}\\
       &=(-1)(2x)+(-1)(5)\\
       &=-2x+(-5)\\
       &=-2x-5
\end{align*}
\end{document}