# Adding arrows connecting foreign keys in relations using latex

How can I create arrows connecting the foreign keys to the corresponding primary key in LaTeX?

The end result I'm trying to achieve is this:

What is the best way to do this in LaTeX (I'm still new to the subject. Is there some practical way, or is it better to create this in another program and import it as image (or PDF?)

-

## migrated from stackoverflow.comNov 9 '11 at 13:23

This question came from our site for professional and enthusiast programmers.

Do you use something like tikz? You question isn't really clear. – Marco Daniel Nov 9 '11 at 14:39
I edited the question, it should be clearer now. By the way, I am using pdflatex and TexMaker as editor – decden Nov 9 '11 at 14:55
Use pgf/tikz. :-) – Martin Schröder Nov 9 '11 at 15:24

TikZ doesn't have automatic routing, unfortunately... So if you want those arrows to move around the text you are going to have to do it manually. Fortunately, that isn't very difficult. The easiest way I can think of is to change the words you want to link into TikZ nodes and then draw the arcs that connect them. You could do that for your example as follows:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\noindent
relation1(\tikz[remember picture]\node[inner sep=0pt] (1id) {\textbf{id}};, \tikz[remember picture]\node[inner sep=0pt] (1r2) {relation2\_id};, \tikz[remember picture]\node[inner sep=0pt] (1r3) {relation3\_id};, attribute1)\\[.5cm]
relation2(\tikz[remember picture]\node[inner sep=0pt] (2id) {\textbf{id}};, \tikz[remember picture]\node[inner sep=0pt] (2r3) {relation3\_id};, attribute2)\\[.5cm]
relation3(\tikz[remember picture]\node[inner sep=0pt] (3id) {\textbf{id}};, attribute3)
\begin{tikzpicture}[overlay, remember picture]
\path[draw,->] (1r2.south) ++(0,-.1) -- ++(0,-.2) -| ($(2id.north) + (0,.1)$);
\path[draw,->] (1r3.south) ++(0,-.1) -- ++(0,-.2) -- ++(1,0) -- ++(0,-1) -| ($(3id.60) + (0,.1)$);
\path[draw,->] (2r3.south) ++(0,-.1) -- ++(0,-.2) -| ($(3id.120) + (0,.1)$);
\end{tikzpicture}
\end{document}


The result then looks like this:

Update: A small addition, for the sake of completeness. You can approach the style in the picture (line/arrow-wise) by changing the options to path to [draw, thick, -latex]. That along with modifying the line placement a little bit gives the following result:

Update 2: In response to your comments, the underline and letters like j make the box around the text larger. To counteract this, we can use a combination of text height and text depth. The code would look as follows:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\noindent
\tikzset{
text depth=0ex,
text height=1ex,
inner sep=0pt,
}%
relation1(\tikz[remember picture]\node (1id) {\underline{id}};, \tikz[remember picture]\node (1r2) {relatjon2\_id};, \tikz[remember picture]\node (1r3) {relation3\_id};, attribute1)\\[.5cm]
relation2(\tikz[remember picture]\node (2id) {\textbf{id}};, \tikz[remember picture]\node (2r3) {relation3\_id};, attribute2)\\[.5cm]
relation3(\tikz[remember picture]\node (3id) {\textbf{id}};, attribute3)
\begin{tikzpicture}[overlay, remember picture]
\path[draw,thick,-latex] (1r2.south) ++(0,-.1) -- ++(0,-.2) -| ($(2id.north) + (0,.1)$);
\path[draw,thick,-latex] (1r3.south) ++(0,-.1) -- ++(0,-.2) -- ++(1,0) -- ++(0,-.97) -| ($(3id.60) + (0,.1)$);
\path[draw,thick,-latex] (2r3.south) ++(0,-.1) -- ++(0,-.2) -| ($(3id.120) + (0,.1)$);
\end{tikzpicture}
\end{document}


Also note the use of tikzset here to set the node properties for all nodes at once. The resulting image with an underlined id and a relatjon:

-
What if i wanted to also underline the ids? By using \underline{id} instead of \textbf{id}, the id is sadly drawn a little bit higher. – decden Nov 10 '11 at 10:23
I just noticed that if you add for example a j/p/q or similar to the text the node jumps up – decden Nov 10 '11 at 11:17

without knowing any coordinate. Run it with xelatex

\documentclass{article}
\usepackage{pst-node}
\parindent=0pt
\begin{document}

relation1(\textbf{id}, \rnode{t2}{relation2\_id}, \rnode{t3}{relation3\_id}, attribute1)\\[.7cm]
relation2(\rnode{mid}{\textbf{id}}, \rnode{mr3}{relation3\_id}, \rnode[r]{ma2}{attribute2) }\\[.7cm]
relation3(\rnode{bid}{\textbf{id}}, attribute3)
\psset{linecolor=red,linewidth=1.5pt,arrowscale=1.5,angleA=-90,angleB=90}
\ncangle{->}{t2}{mid}
\ncangle[nodesepB=0]{t3}{ma2}
\ncangle[angleA=90,angleB=90,nodesepA=0,armB=4mm,offsetB=1mm]{->}{ma2}{bid}
\ncangles[armA=3mm,offsetB=-1mm]{->}{mr3}{bid}
\end{document}


-