How to draw arrows between parts of an equation to show the Math Distributive Property (Multiplication)?
Adapted Solution:
Here is a version of the Basic Solution below adapted to your specific case:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[4]{%
\begin{tikzpicture}[overlay,remember picture,-latex,shorten >=5pt,shorten <=5pt,out=70,in=130]
\draw[distance=0.45cm,#1] (a.north) to (b.north);
\draw[distance=0.65cm,#2] (a.north) to (c.north);
\draw[distance=0.9cm, #3] (a.north) to (d.north);
\draw[distance=1.1cm, #4] (a.north) to (e.north);
\end{tikzpicture}
}
\begin{document}
\begin{gather*}
(\tikzmark{a}l_{1}) \vee \big( (p \vee\tikzmark{b} q) \wedge (\neg p \vee\tikzmark{c} q) \wedge (p \vee\tikzmark{d} \neg q) \wedge (\neg p \vee\tikzmark{e} \neg q)\big) \DrawBox{red}{blue}{green}{orange}\\
(l_{1} \vee p \vee q) \wedge (l_{1} \vee \neg p \vee q) \wedge (l_{1} \vee p \vee \neg q) \wedge (l_{1} \vee \neg p \vee \neg q)
\end{gather*}
\end{document}
Notes:
As with most
\tikzmark
s, this does require two runs. First one to determine the locations, and the second to do the drawing.The
\tikzmark
is from Adding a large brace next to a body of text.
Basic Solution:
You can use tikz
and the \tikzmark
as defined in Arrow between parts of equation in LaTeX). Each endpoint of an arc is identified by \tikzmark
, and the \DrawBox
macro draws the arc between each of the nodes. The arc angle going out are adjusted by out=
, and the incoming angle of the endpoint is specified by in=
.
\documentclass{article}
\usepackage{amsmath}
\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}
\begin{equation}
\tikzmark{MarkA}5 (x\tikzmark{MarkB} + 6\tikzmark{MarkC})\DrawBox{red}{blue}
\end{equation}
\end{document}
Just to add something to the great answers already here: it's sometimes useful to have squared arrows instead of curved arrows. Here's an example of how you can do that. The same style could be applied to any of the other solutions here (with some minor tweaking of the anchors.)
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\tikzset{square arrow/.style={to path={-- ++(0,-.25) -| (\tikztotarget)}}}
\begin{document}
\begin{equation}
a\tikzmark{a}x^2 + bx + c = 5\tikzmark{b}x^2 + bx + c.
\tikz[overlay,remember picture]
{\draw[->,square arrow] (a.south) to (b.south);}
\end{equation}
\end{document}
Response to comments
If you want to add a label text to the line you can do the following. A big thanks to Kpym for improving this code.
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\tikzset{square arrow/.style={
to path={-- ++(0,-.25) -| (\tikztotarget) \tikztonodes},below,pos=.25}}
\begin{document}
\begin{equation}
a\tikzmark{a}x^2 + bx + c = 5\tikzmark{b}x^2 + bx + c.
\tikz[overlay,remember picture]
{\path[draw,->,square arrow] (a.south) to node{x} (b.south) ;
}
\end{equation}
\end{document}
Here's something that might be slightly more comfortable to use if you need this regularly.
I've defined three new commands, \source
, \target
and \drawarrows
. In your equation, replace the source term with \source{<source term>}
, and each of the <target terms>
with \target{<target term>}
. After your equation, issue \drawarrows
, and the arrows will be drawn. The solution uses TikZ overlay
, which means you have to compile the document twice to get the placement right.
\documentclass{article}
\usepackage{tikz}
\newcommand\source[1]{%
\tikz[remember picture,baseline,inner sep=0pt] {%
\node [name=source,anchor=base]{$#1$};
}%
\setcounter{target}{0}
}
\newcounter{target}
\newcommand\target[1]{%
\tikz[remember picture,baseline,inner sep=0pt] {%
\node [name=target-\thetarget,anchor=base]{$#1$};
}%
\stepcounter{target}%
}
\newcommand\drawarrows{
\tikz[remember picture, overlay, bend left=20, -latex] {
\foreach \i [evaluate=\i as \n using int(\i-1)] in {1,...,\thetarget} {
\draw (source.north) to (target-\n.north);
}
}
}
\begin{document}
$\source{(l_{1})}\vee \big( \target{(p \vee q)} \wedge \target{(\neg p \vee q)} \wedge \target{(p \vee \neg q)} \wedge \target{(\neg p \vee \neg q)}\big)$
\drawarrows
\end{document}