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

  • 1
    Do you use something like tikz? You question isn't really clear. 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
  • 1
    Use pgf/tikz. :-) 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:


    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

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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