55

I need to draw an Hasse Diagram using LaTeX. TikZ is the solution I would like to use. I tried with a simple structure involving trees but obviously when I need to join two nodes, it is not possible.

I searched a little but found no immediate solutions for Hasse Diagrams in TikZ. Which one is the fastest structure I can use? I would like also to have the tree syntax flexibility without manually specifying where nodes should be places. I just want to specify nodes and connections.

1
  • 2
    have a look at these node positioning examples. A Hasse Diagram basically looks like a bunch of nodes, an TikZ is very good at drawing bunches of nodes. These examples should help you to get going.
    – romeovs
    Mar 9, 2012 at 12:44

4 Answers 4

68

Here is a brief tutorial to get you started: As in any other piece of software, break it down into its components.

  1. First step is to draw the nodes and give them name. Lets start with the top node located at the origin and name it (top):

    \documentclass{article}
    \usepackage{tikz}
    \begin{document}
    \begin{tikzpicture}
        \node (top) at (0,0) {$\{x,y\}$};
    \end{tikzpicture}
    \end{document}
    

    So now you have:

    enter image description here

    which is not too exciting.

    Note: I included the full code here, but subsequent steps I show only the additions to the above to make it easier to follow.

  2. Next, add the left and right nodes. These should be located relative to the (top) node so that in case we decide to move the position of the top node, these two will move along with it:

    \node [below left  of=top] (left)  {$x$};
    \node [below right of=top] (right) {$y$};
    

    So, now things a looking a bit more useful:

    enter image description here

    If these are not far enough apart, you could move them, again relatively, by using something like xshift=<length>, or xshift=<length>.

  3. Next step is to draw the lines connecting these nodes via their node names (color used here to see the correspondence between the code and the output):

    \draw [red,  thick] (top) -- (left);
    \draw [blue, thick] (top) -- (right);
    

    to get:

    enter image description here

    If the lines are not long enough you can shorten then via a negative amount. For the top we use shorten <=-2pt, and for the bottom shorten >=-2pt to get:

    enter image description here

    Using the to syntax as opposed to the -- allows you to get fancier and control the angle at the out point via out=<angle>, and the angle of the line coming in via in=<angle>:

    \draw [red,  thick, shorten <=-2pt, shorten >=-2pt, out=-120, in=90] (top) to (left);
    \draw [blue, thick, shorten <=-2pt, shorten >=-2pt, out=-60,  in=90] (top) to (right);
    

    which if it had been used below would have produced:

    enter image description here

    As pointed out by Paul Gaborit, the out and in options are really only for the to directive so some might prefer a syntax that more explicitly places those options for the to as in:

    \draw [red,  thick, shorten <=-2pt, shorten >=-2pt] (top) to [out=-120, in=90] (left);
    \draw [blue, thick, shorten <=-2pt, shorten >=-2pt] (top) to [out=-60,  in=90] (right);
    

References:

Other tutorial like answers here which might be useful for new tikz users include:

Code:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    % First, locate each of the nodes and name them
    \node (top) at (0,0) {$\{x,y\}$};
    \node [below left  of=top] (left)  {$x$};
    \node [below right of=top] (right) {$y$};

    % Now draw the lines:
    \draw [red,  thick, shorten <=-2pt, shorten >=-2pt] (top) -- (left);
    \draw [blue, thick, shorten <=-2pt, shorten >=-2pt] (top) -- (right);
\end{tikzpicture}
\end{document}
7
  • 11
    I appreciate the tutorial way in which you wrote this answer.
    – N.N.
    Mar 9, 2012 at 17:40
  • Nice answer. I guess the at (top) part in step 2 is souperflous, since it’s overwritten by the below left key, isn’t it.
    – Tobi
    May 16, 2012 at 15:51
  • @Tobi: Good point. But, then these two lines ` \node (top) at (0,0) {$\{x,y\}$}; \node [below left of=(top)] (left){$x$};` result in No shape named (top) is known.. May 16, 2012 at 18:11
  • It works with \node [below left of=top] (left) {$x$}; The shape names in this syntax are assumed as names and not as coordinates, unlike as in at (top), so one must omit the parens.
    – Tobi
    May 21, 2012 at 7:43
  • @Tobi: Ok, have updated the syntax. Please let me know if I missed anything. Thanks... May 21, 2012 at 8:05
45

Recently, I also had to draw Hasse diagrams, so I post a few examples to get you going. My approach has been rather simple: specifying the nodes and draw the necessary lines between them with --. I just post a few examples to get you going.

Here's a variation of the diamond:

\begin{tikzpicture}[scale=.7]
  \node (one) at (0,2) {$1$};
  \node (a) at (-3,0) {$a$};
  \node (b) at (-1,0) {$b$};
  \node (c) at (1,0) {$c$};
  \node (d) at (3,0) {$d$};
  \node (zero) at (0,-2) {$0$};
  \draw (zero) -- (a) -- (one) -- (b) -- (zero) -- (c) -- (one) -- (d) -- (zero);
\end{tikzpicture}

This code gives the following Hasse diagram:

Hasse diagram: diamond variation

Here is an example of a hexagon for the use of polar coordinates:

\begin{tikzpicture}[scale=.7]
  \node (one) at (90:2cm) {$1$};
  \node (b) at (150:2cm) {$b$};
  \node (a) at (210:2cm) {$a$};
  \node (zero) at (270:2cm) {$0$};
  \node (c) at (330:2cm) {$c$};
  \node (d) at (30:2cm) {$d$};
  \draw (zero) -- (a) -- (b) -- (one) -- (d) -- (c) -- (zero);
\end{tikzpicture}

which gives as output:

Hasse diagram: hexagon

And here is a representation of the boolean algebra of subsets of a three element set, to illustrate intersecting lines:

\begin{tikzpicture}
  \node (max) at (0,4) {$(1,1,1)$};
  \node (a) at (-2,2) {$(0,1,1)$};
  \node (b) at (0,2) {$(1,0,1)$};
  \node (c) at (2,2) {$(1,1,0)$};
  \node (d) at (-2,0) {$(0,0,1)$};
  \node (e) at (0,0) {$(0,1,0)$};
  \node (f) at (2,0) {$(1,0,0)$};
  \node (min) at (0,-2) {$(0,0,0)$};
  \draw (min) -- (d) -- (a) -- (max) -- (b) -- (f)
  (e) -- (min) -- (f) -- (c) -- (max)
  (d) -- (b);
  \draw[preaction={draw=white, -,line width=6pt}] (a) -- (e) -- (c);
\end{tikzpicture}

which draws the diagram

Hasse diagram: subsets of 3

2
  • Strange enough, I also find it more easy to work, if I do not use positioning commands, such as above etc. This way you also get better control. May 21, 2012 at 10:10
  • @Yiannis Lazarides: I agree. I wouldn't know how to obtain the result of the first example easily with the positioning commands. The nodes (min) and (max) are below and above the middle of the line from (b) to (c). If you want to get (min) at that position by using below and right or left, you'll probably end up defining a node between (b) and (c) that you otherwise don't want. That seems a bit unnatural. On the other hand, the last diagram would be easy to draw with positioning commands.
    – Egbert
    May 21, 2012 at 15:12
2

With Xy-pic package,

\documentclass[border=5pt]{standalone}

\usepackage[all,pdf]{xy}
\usepackage{lmodern,amssymb}
\begin{document}
$$
\def\arl{\ar@{-}}
\xymatrix{
        & \{x,y,z\}\arl[dl]\arl[d]\arl[dr] & \\
\{x,y\}\arl[d]\arl[dr] & \{x,z\}\arl[dl]|\hole\arl[dr]|\hole & \{y,z\}\arl[dl]\arl[d] \\
\{x\}\arl[dr]   & \{y\}\arl[d]   & \{z\}\arl[dl] \\
        & \{\varnothing\} \\
}
$$
\end{document}

And you get:

1

1

Here is another nice and simple solution:

enter image description here

\begin{tikzpicture}
    \matrix (A) [matrix of nodes, row sep=2cm, nodes={minimum width=4cm}]
    {
        $\{x,y\}$ & $\{x,z\}$ & $\{y,z\}$ \\
        $\{x\}$ & $\{y\}$ & $\{z\}$ \\
        & $\{\emptyset\}$ \\
    };
    \path (A-1-1)--(A-1-2) node[above=2cm] (link) {$\{x,y,z\}$};
    
    \foreach \i in {1,...,3}
    \draw (link.south) -- (A-1-\i.north);
    
    \foreach \i/\j in {1/2, 3/2, 2/1, 1/1, 3/3, 2/3}
    \draw (A-1-\i.south)--(A-2-\j.north);
    
    \foreach \i/\j in {1/2, 2/2, 3/2}
    \draw (A-2-\i.south)--(A-3-\j.north);
\end{tikzpicture}
1
  • This doesn't compile for me. Can you please list the packages you are using, etcetera?
    – user65526
    Oct 10, 2022 at 8:30

You must log in to answer this question.

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