Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?)

share|improve this question

migrated from stackoverflow.com Nov 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

2 Answers 2

up vote 8 down vote accepted

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:


    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)$);

The result then looks like this:

TikZ relations

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:

modified TikZ relations

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:


            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)$);

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:

Further modified TikZ relations

share|improve this answer
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


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)

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.