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

share|improve this question
2  
Have you looked at the tutorials in the first part of the manual? –  Torbjørn T. Apr 8 '12 at 19:42
1  
Concur. I provide my students with a 15 minute demonstration/introduction to TikZ. Then they using the first part of the manual, successfully create simple to mildly complicated diagrams. The very best way to learn TikZ is to do Tikz. Best wishes. –  R. Schumacher Apr 8 '12 at 19:55
    
Have a look at the example (incl. code) at texample.net, especially texample.net/tikz/examples/feature/node-positioning –  Martin Scharrer Apr 8 '12 at 19:58
    
Perhaps have a look at How to draw a poset Hasse Diagram using TikZ which provides a tutorial to get you started. –  Peter Grill Apr 8 '12 at 19:59
5  
The resource A minimal Introduction to Tikz might be less overwhelming than the whole manual. Specifically, page 14-15 has everything I think you need. –  DJP Apr 8 '12 at 20:10
show 1 more comment

2 Answers 2

up vote 7 down vote accepted

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

share|improve this answer
    
Looks great! Is it also possible to get the font from mathmode? Could you also tell me what >=triangle 45 stands for? –  codd Apr 8 '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 8 '12 at 20:09
add comment

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

Your Answer

 
discard

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.