6

How do you draw a simple figure like the following in TikZ? It seems like the only way to start learning TikZ is by adopting examples, but I found the only two fairly relevant to what I wanted too complicated for what I am trying to achieve here. What I am trying to accomplish by getting a piece of code for this figure, is not only the figure itself, but also insight in how TikZ does drawings like these, so I can use it to make similar figures in the future.

enter image description here

codd
  • 1,239

2 Answers2

12

Actually this is rather simple. There are several ways to do it. One way would be to place the first node using \node (NAME) at (POSITION) {TEXT}; and then further nodes using \node (NAME) [below left=Y and X of NODE] {TEXT}; etc.. Arrows can be drawn using \draw [->] (NODE1) -- (NODE2);. Add a trailing node [OPTIONS] {TEXT} before the ; to add labels to the arrows.

\documentclass{standalone}% For the example only, any class will do

\usepackage{tikz}
\usetikzlibrary{positioning}% To get more advances positioning options
\usetikzlibrary{arrows}% To get more arrow heads

\begin{document}
\begin{tikzpicture}[>=triangle 45,font=\sffamily]
    \node (X) at (0,0) {x};
    \node (Y) [below left=2cm and 1cm of X]  {y};% 2cm below, 1cm to the left (optional)
    \node (Z) [below right=2cm and 1cm of X] {z};
    \node (U) [below left=2cm and 1cm of Z]  {u};
    \draw [semithick,->] (X) -- (Y);
    \draw [semithick,->] (X) -- (Z);
    \draw [semithick,->] (Y) -- (U) node [midway,below,sloped] {*};
    \draw [semithick,->] (Z) -- (U) node [midway,below,sloped] {*};
\end{tikzpicture}
\end{document}

Diagram

Martin Scharrer
  • 262,582
  • Looks great! Is it also possible to get the font from mathmode? Could you also tell me what >=triangle 45 stands for? – codd Apr 08 '12 at 20:05
  • 2
    @codd To have the font from mathmode, write the node text in mathmode, e.g. node{$x$}. triangle 45 is the name of an arrowhead, which is specified with >=NameOfArrowhead. – Torbjørn T. Apr 08 '12 at 20:09
4

There are several possibilities to make this graph. The way I prefer is the next code. I avoid \usetikzlibrary{positioning} because I prefer to use scale than setting node distance. The command path is the most important. I use it with relative coordinates ++(-1,-2). ++(-1,-2) signifies below left=2cm and 1cm of X etc.

Then I use a scope to set some styles. Every paths use semithick,-> and I prefer with -- node (U) by default pos=.5. It's preferable to use this syntax because you can use the same with the to command.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}[>    = triangle 45,
                    font = \sffamily,
                    scale= 2]
   \path  (0,  0)  node (X)  {x} 
        ++(-1,-2)  node (Y)  {y}
        ++(1, -2)  node (U)  {u}
        ++(1,  2)  node (Z)  {z};
\begin{scope}[semithick,
              ->,
              every node/.style = {below,sloped}]
   \draw  (X) -- (Y);
   \draw  (X) -- (Z);
   \draw  (Y) -- node  {*} (U) ;
   \draw  (Z) -- node  {*} (U) ;  
\end{scope}
\end{tikzpicture}
\end{document} 

enter image description here

Update This method needs explanations. Like the first way, I use every node to in the scope to place the labels * but I placed > = triangle 45 only in the scope because the arrows are used only in this part. Then It's possible to use edge instead of -- but it's interesting to compare how to get the arrows from x and how to get the arrows through u.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}[font  = \sffamily,
                    scale = 2]
   \path  ( 0,  0)  node (X)  {x} 
        ++(-1, -2)  node (Y)  {y}
        ++( 1, -2)  node (U)  {u}
        ++( 1,  2)  node (Z)  {z};
\begin{scope}[semithick,->,
              > = triangle 45,
              every node/.style = {below,sloped}]
   \draw  (X) edge (Y)
              edge (Z)
          (Y) edge node  {*} (U) 
          (Z) edge node  {*} (U) ;  
\end{scope}
\end{tikzpicture}
\end{document}
Alain Matthes
  • 95,075