# Lines connecting elements in an equation

I want to draw lines connecting various elements in an equation. Here is what I have so far:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\newcommand\connect[1]{
\draw[->] ([shift={(2pt,-2pt)}]pic cs:start#1)
to ([shift={(2pt,8pt)}]pic cs:end#1);
}

\begin{document}

\begin{gather*}
\{a, \tikzmark{startB1}b, c, \tikzmark{startD1}d\} \\ \\
\{\tikzmark{endB1}b, \tikzmark{endD1}d\}
\end{gather*}
\begin{tikzpicture}[remember picture,overlay,line width=0.5pt]
\connect{B1}
\connect{D1}
\end{tikzpicture}

\begin{gather*}
\{a, \tikzmark{startB2}bbbb, c, \tikzmark{startD2}dddd\} \\ \\
\{\tikzmark{endB2}bbbb, \tikzmark{endD2}dddd\}
\end{gather*}
\begin{tikzpicture}[remember picture,overlay,line width=0.5pt]
\connect{B2}
\connect{D2}
\end{tikzpicture}

\end{document}


I hard coded various numbers (such as 2pt, 8pt, etc) after finding them by trial and error.

Is there a way to automate this without entering hard coded numbers? I want the line to be centered at the bottom of the top element and at the top of the bottom element. For example, bbbb is considered one element.

I also wonder, if there is an entirely different way to do this that is simpler.

Clarification: In the above image, the second set of equations is not correctly rendered. Both ends of the arrows should have been centered on bbbb and dddd.

• What I usually do is create two marks e.g. one on the left of bbbb and one on the right and then use the calc library to get the point halfway between them. – cfr Nov 22 '15 at 23:56

I played a bit with tikz, and this is the result (I only show the second example, since that seem to be the problematic one):

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{tikz}

\begin{document}
\begin{gather*}
\{a,
\tikz[baseline,remember picture]{\node[anchor=base,inner xsep=0pt] (b1) {$bbbb$};},
c,
\tikz[baseline,remember picture]{\node[anchor=base,inner xsep=0pt] (d1) {$dddd$};}\} \\ \\
\{\tikz[baseline,remember picture]{\node[anchor=base,inner xsep=0pt] (b2) {$bbbb$};},
\tikz[baseline,remember picture]{\node[anchor=base,inner xsep=0pt] (d2) {$dddd$};}\}
\end{gather*}

\tikz[remember picture,overlay]{\draw[->] (b1.south) to (b2.north);}
\tikz[remember picture,overlay]{\draw[->] (d1.south) to (d2.north);}
\end{document}


An alternative is to change the last two lines into something like

\tikz[remember picture,overlay]{\draw[->,shorten <=1.2ex,shorten >=1.2ex] (b1.center) to (b2.center);}
\tikz[remember picture,overlay]{\draw[->,shorten <=1.2ex,shorten >=1.2ex] (d1.center) to (d2.center);}


The result then looks like below:

I guess it is a matter of taste.

It is very simple to do with pstricks, and more specifically with pst-node and auto-pst-pdf: put everything in a postscript environment, define the elements you want to join as nodes, and connect these nodes:

\documentclass{article}
\usepackage{amsmath}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}

\begin{document}

\begin{postscript}
\psset{nodesep=2pt, linewidth=0.5pt, arrows=->, arrowinset=0.15}
\begin{gather*}
\{a, \rnode{B1}b, c, \rnode{D1}d\} \\[3ex]
\{\rnode{B2}b, \rnode{D2}d\}
\ncline{B1}{B2}\ncline{D1}{D2}
\end{gather*}
%
\begin{gather*}
\{a, \rnode{B1}{bbbb}, c, \rnode{D1}{dddd} \} \\ \\
\{\rnode{B2}{bbbb}, \rnode{D2}{dddd}\}
\ncline{B1}{B2}\ncline{D1}{D2}
\end{gather*}
\end{postscript}

\end{document}


• Do you mean they have to in the first line? Or should the arrows join the groups of bbbb and dddd? – Bernard Nov 23 '15 at 1:22
• I see. I'll edit in a moment. – Bernard Nov 23 '15 at 1:27

This might be a start:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix (magic0) at (0,1) [matrix of nodes]{
\{ & aaaaaaa, & bbbb, & c, & dd & \}\\
};

\matrix (magic1) at (0,0) [matrix of nodes]{
\{ & a, & b, & cc, & ddd & \} \\
};

\draw[thick,->] (magic0-1-3) -- (magic1-1-4);
\draw[thick,->] (magic0-1-2) -- (magic1-1-3);
\end{tikzpicture}

\end{document}


tikz/pgf manual p.646