7

I'm very sorry about dull question, but I am totally confused by TikZ. Would anyone help me to draw following diagram, please?

enter image description here

What I've done so far is as follows.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}
\begin{document}
\begin{tikzpicture}[%
    every node/.style={{font=\tt},>=stealth'},%
    list/.style={rectangle split, rectangle split parts=3,%
    draw,>=stealth', rectangle split horizontal}%
    ]
\node at (-1,1) (upperLeftHidden) {};
\node[list] (center) [below right=of upperLeftHidden] {x} %at (0,0)
    edge[<-,bend right=20,shorten <=5pt,>=stealth']
        (upperLeftHidden);
\node [below left=of center] {t}
    edge[<-,bend left=20,shorten <=3pt,>=stealth'] (center);
\node[list] (bot) [below right=of center] {\nodepart{second} f} %at (0,0)
    edge[<-,bend right=20,shorten <=5pt,>=stealth']
        (center)
    edge[->,bend right=20,shorten <=5pt,>=stealth']
        (center.north);
%\node [below left=of bot] {e} % doesn't work for some reason
    %edge[<-,bend left=20,shorten <=3pt,>=stealth'] (bot)
\end{tikzpicture}
\end{document}

Which results in:

enter image description here

2
  • 5
    please show what you have tried so far. That way you learn by mistake rather than just getting a solution.
    – nickpapior
    Jan 23, 2012 at 9:52
  • 3
    A start: TikZ manual, 16.3 Multi-Part Nodes, 48.6 Shapes with Multiple Text Parts (shape: rectangle split), 16.11 Connecting Nodes.
    – Stefan Kottwitz
    Jan 23, 2012 at 10:34

3 Answers 3

7

Another variation, unfortunately morbusg was faster than me :)

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes.multipart,calc}

\begin{document}

\begin{tikzpicture}[%
    every node/.style={{font=\tt},-latex},%
    list/.style={rectangle split, rectangle split parts=3, draw, rectangle split horizontal},%
    ]
\node[list, name=adin] at (0,0) {x};
\node[list, name=dwa] at (1,-1) {\nodepart{second} f};
\draw[bend left=10,-latex] (-1,1) to ($(adin.two north) + (-0.1,0.1)$);
\draw[bend right=20,-latex] (adin.two) to ++(-1,-1) node[below] {t};
\draw[bend right=20,-latex] (dwa.one) to ++(-1,-1) node[below] {e};
\draw[bend left=30,-latex] (adin.three) to ($(dwa.two north) + (0,0.1)$);
\draw[-latex,rounded corners,out=-45,in=45] (dwa.three) .. controls (3,-3) and (1.5,3) ..  ($(adin.two north) + (0.1,0.1)$);
\end{tikzpicture}

\end{document}

enter image description here


Edit 1: With some improvements thanks to morbusg:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes.multipart,calc,scopes}

\begin{document}

\begin{tikzpicture}[%
    every node/.style={{font=\tt},-latex},%
  list/.style={rectangle split, rectangle split parts=3, draw, rectangle split horizontal}]

\node[list, name=adin] at (0,0) {x};
\node[list, name=dwa] at (1,-1) {\nodepart{second} f};

{[-latex]
    \draw[bend left=10] (-1,1) to ($(adin.two north) + (-0.1,0.1)$);
    \draw[bend right=20] (adin.two) to ++(-1,-1) node[below] {t};
    \draw[bend right=20] (dwa.one) to ++(-1,-1) node[below] {e};
    \draw[bend left=30] (adin.three) to ($(dwa.two north) + (0,0.1)$);
    \draw[out=-45,in=45,looseness=4] (dwa.three) to ($(adin.two north) + (0.1,0.1)$);
}
\end{tikzpicture}

\end{document}

enter image description here

1
  • 1
    Haha, that was non-intentional, but looking at your profile's location "Rostov-na-Donu, Russia", this is a nice coincidence. Da Nesaschto! Jan 23, 2012 at 14:37
10

Here's some ideas:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart,positioning,scopes}
\begin{document}
\begin{tikzpicture}
  [ >=latex,shorten >=1pt,
    triplet/.style={
      draw,
      rectangle split, rectangle split parts=3,
      rectangle split ignore empty parts=false,
      rectangle split horizontal=true, font=\tt}]

  \node [triplet] (x) {x};
  \node [triplet,below right of=x] (f) {\nodepart{two}f};

  {[->]
    \draw (x.two) to[bend right=30] +(-1,-1) node[below] {\tt t};
    \draw (x.three) to[bend left=30] (f.two north);
    \draw ([yshift=1ex]f.three)
      to[out=45,in=75,looseness=2] ([xshift=1pt]x.two north);
    \draw (f.one) to[bend right=30] +(-1,-1) node[below] {\tt e};
  }
  \draw[<-] (x.two north) to[bend right=15] +(-1,1);
\end{tikzpicture}
\end{document}

enter image description here

1
  • Nice solution, neither did I know about the possibility of {[->] <draw commands> }, nor about the looseness option. Thanks! Jan 23, 2012 at 11:43
9

I agree with zeroth's comment but if you are totally confused by TikZ perhaps it will be a good idea to help you.

In your diagram, you have : texts and arrows. TikZ gives you a tool named node to place text inside a shape. With a node, TikZ defines for you some objects (anchors) to draw correct edges between the nodes.

First we place the nodes:

\begin{tikzpicture}
\node (e) at (3,1){e}; % {e} label of the node (e) reference or name of the node
\node (t) at (2,2){t};
\node (u) at (0,6){}; 

\node (g) at (4,2){}; 
\node (g) at (5,2){f};   
\node (h) at (6,2){};

\node (x) at (2,4){x}; 
\node (y) at (3,4){};   
\node (z) at (4,4){};
\end{tikzpicture} 

The shapes of some nodes are drawn and the shapes are similar. By default, the shape is a rectangle and with the option minimum width=1cm we can get a square.

We want to give the same shape for all nodes also this can be achieved with every node/.style= minimum width=1cm. The nodes are placed with 1 cm between the centers

\begin{tikzpicture} [every node/.style={minimum size=1cm}] 
\node (e) at (2,0){e}; % {e} label of the node (e) reference or name of the node
\node (t) at (1,1){t};
\node (u) at (0,6){}; % a name but an empty label
% now we draw the shape for the next nodes, we can add an option 
\tikzset{every node/.append style={draw}} 
\node (g) at (4,2){}; 
\node (g) at (5,2){f};   
\node (h) at (6,2){};         
\node (x) at (2,4){x}; 
\node (y) at (3,4){};   
\node (z) at (4,4){};     
\end{tikzpicture}

To draw an arrow, you need to give two nodes or two coordinates (start and end): For example \draw[->,>=latex] (y) -- (t); draws a line between two nodes. The points at each extremities of the arrow are selected by tikZ. You can give these points

 \draw[->,>=latex] (y.center) --(t.north);%

The anchors defined by TikZ :

enter image description here

When you create a node, you define some special points (anchors) around the shape. An option outer sep can be used to modify the positions of anchors around the node. With outer sep=2pt , the arrows are placed at 2 pts of the shape. We can change the node style.

[every node/.style={minimum size=1cm,outer sep=2pt}] In the next picture you can see the action of outer sep. The gray part is added by outer sep. You get the same result with other shapes.

enter image description here

Now to draw a simple arrow like the one between (y) and (t), we can use :

   \draw[->,>=latex] (y.center) to[out=-135,in=90] (t.north);  

[->,>=latex] are options : -> to get an arrow and >=latex for the style of the head.

[out=-135,in=90] are options for the powerful command to . The arrow begins with an angle=-135 from the point (y.center) and arrives with an angle 90 at (t.north).

The next arrow is more complicated to draw because we need to decompose it in several parts:

  \draw[->,>=latex] (h.center) to [out=-90,in=180] +(1,-1) 
                               to [out=0,in=-60] +(+1,+3)
                               to [out=120,in=90] (y.north);

start : (h.center) with an angle of -90 next step : +(1,-1) these coordinates are relative to last coordinates of (h.center) next step : +(1,+3) final : (y.north)

What can we improve? Like in others answers, we can use multiple text parts but the principle are the same. There are other possibilities.

Final code :

\documentclass[11pt]{scrartcl}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture} [every node/.style={minimum size=1cm,outer sep=2pt}] 
\node (e) at (2,0){e}; % {e} label of the node (e) reference or name of the node
\node (t) at (1,1){t};
\node (u) at (0,6){}; 
% we draw the shape for the next nodes we can add an option 
\tikzset{every node/.append style={draw}} 
\node (g) at (4,2){}; 
\node (g) at (5,2){f};   
\node (h) at (6,2){};

\node (x) at (2,4){x}; 
\node (y) at (3,4){};   
\node (z) at (4,4){}; 

\draw[->,>=latex] (u.east) to [out=0,in=120] (y.north);
\draw[->,>=latex] (y.center) to[out=-145,in=90] (t.north); 
\draw[->,>=latex] (h.center) to [out=-90,in=180] +(1,-1) 
                             to [out=0,in=-60] +(+1,+3)
                             to [out=120,in=70] (y.north);

% the arrow is placed at the end of a path. You need to study the concept of path 
\end{tikzpicture}
\end{document}  

enter image description here

Now I think you can finish the diagram.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .