10

I try to use TikZ to draw a wordnet like this

enter image description here

but I really don't know what to start with. Can anybody help me with drawing this graph? Unfortunately, I don't have enough time to read books about TikZ. It's a part of my hometask for today :(

2

2 Answers 2

13

A possibility using the powerful forest package:

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest}
[entity
  [inanimate-object
    [natural-object
      [geological-formation
        [natural-elevation [hill], tikz={
           \foreach \nodenam/\nodelabel in {!ul/0.000113,!u/0.000189}
             \node[anchor=east,xshift=-10pt] at (\nodenam.west) {$\nodelabel$};                              
            }
        ] 
        [shore [coast], tikz={
            \foreach \nodename/\nodelabel in {!ul/0.0000216,!u/0.0000836,!uu/0.00176,!uuu/0.0163,!uuuu/0.167,!ur/0.365}
                \node[anchor=west,xshift=10pt] at (\nodename.east) {$\nodelabel$};
          }
        ]
      ]
    ]
  ]
]
\end{forest}

\end{document}

enter image description here

11

A tree-based solution

One possible solution to this is to \usetikzlibrary{positioning} in combination with qtree and tikz-qtree. The latter two will allow you to draw tree structures like the tree that you're after, and the positioning library will allow you to define nodes in terms of other nodes in order to place the numbers relative to the words.

Here is an MWE for such a solution:

\documentclass{article}

\usepackage{qtree}
\usepackage{tikz}
\usepackage{tikz-qtree,tikz-qtree-compat}
\tikzset{every tree node/.style={align=center, anchor=north}}
\usetikzlibrary{positioning}
\usepackage{fixltx2e}

\begin{document}

\begin{tikzpicture}[baseline]
\Tree
[.\node(entity){entity};
    [.\node(inanimate){inanimate object};
        [.\node(natural){natural object};
            [.\node(geological){geological formation};
                [.\node(naturalelev){natural elevation};
                    [.\node(hill){hill}; ]
                ]
                [.\node(shore){shore};
                    [.\node(coast){coast}; ]
                ] ] ] ] ]
\node [base right=2cm of entity] (1) {0.395} ;
\node [base left=2.5cm of entity] (align) {};
\node [below=0.6cm of 1] (2) {0.167};
\node [below=0.6cm of 2] (3) {0.0163};
\node [below=0.55cm of 3] (4) {0.00176};
\node [below=0.6cm of 4] (5) {0.0000836};
\node [below=0.5cm of 5] (6) {0.0000216};
\node [below=3.8cm of align] (7) {0.000113};
\node [below=.5cm of 7] (8) {0.0000189};
\end{tikzpicture}

\end{document}

enter image description here

The disadvantage to this solution is that you have to place the additionally-defined nodes manually. Someone can probably think of a more elegant solution than I can, which would be particularly nice if you have to make many of these diagrams. Nonetheless, this solution at least seems to provide what you're after in this case.


A matrix-based solution

Another solution that avoids the problem noted directly above: namely, the problem of having to define the nodes manually each time you have to draw one of these diagrams (or each time you resize one of them) is to \usetikzlibrary{matrix}. Placing all of the information inside the rows and columns of a matrix allows one to avoid the issue of having to space the information manually using the \node command, since this is automatically done in virtue of the information being stored in the rows and columns of a matrix.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{figure}[h!]
\begin{tikzpicture}[description/.style={fill=white,inner sep=2pt}]
\matrix (m) [matrix of nodes, row sep=1.5em,
column sep=0.3em, text height=1.5ex, text depth=0.25ex]
{
& & entity & & 0.395 \\ 
& & inanimate object & & 0.167 \\ 
& & natural object & & 0.0163 \\ 
& & geological formation & & 0.00176 \\ 
0.000113 & natural elevation & & shore & 0.0000836 \\ 
0.0000189 & hill & & coast & 0.0000216 \\ 
};

\path[-]    (m-1-3) edge (m-2-3)
            (m-2-3) edge (m-3-3)
            (m-3-3) edge (m-4-3)
            (m-4-3) edge (m-5-2)
            (m-5-2) edge (m-6-2)
            (m-4-3) edge (m-5-4)
            (m-5-4) edge (m-6-4);

\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

You must log in to answer this question.

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