Author

Alejandro Alcalde

Data Scientist and Computer Scientist. Creator of this blog.

Alejandro Alcalde's posts | Porfolio

My computer science final project was about building a dependency parser for Spanish. Writing the documentation I had to draw some trees representing a dependency graph and a visual explanation of what an SVM (Support Vector Machine ) is.

To draw all this pictures I’ve used Tikz, a LaTeX package. Here is the code:

Drawing a dependency tree with Tikz

Example 1

Here is the code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-dependency}

\begin{document}

\begin{dependency}[edge slant=15pt,label theme = simple, edge theme = iron]
  \begin{deptext}[column sep=1em]
    This \& time \& around \& , \& they \& 're \& moving \& even \& faster \&
    . \\
  \end{deptext}
  \deproot{7}{ROOT}
  \depedge{2}{1}{DET}
  \depedge[edge height=1.9cm]{7}{2}{TMOD}
  \depedge{2}{3}{ADVMOD}
  \depedge{7}{6}{AUX}
  \depedge{7}{5}{NSUBJ}
  \depedge{7}{4}{PUNCT}
  \depedge{7}{10}{PUNCT}
  \depedge{7}{9}{ADVMOD}
  \depedge[edge start x offset=-4pt]{9}{8}{ADVMOD}
\end{dependency}

\end{document}

And the result:

Example 2

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree,tikz-qtree-compat}

\begin{document}

\tiny
\begin{tikzpicture}[every node/.style={align=center},scale=.9]
  \tikzset{
    edge from parent/.style={
      draw,edge from parent
      path={(\tikzparentnode.south)-- +(0,-8pt)-| (\tikzchildnode)}
    },
    frontier/.style={distance from root=208pt}, % Align leaf nodes
    level 1+/.style={level distance=18pt} % Distance between levels
  }

   \Tree [.S
             [.NP Rolls-Royce\\NNP Motor\\NNP Cars\\NNPS Inc\\NNP ]
             [.VP said\\VBD
                [.SBAR [.none ]
                   [.S
                      [.NP it\\PRP ]
                      [. VP expects\\VBZ
                         [.S
                            [.NP its\\PRP\$ U.S\\NNP sales\\NNS ]
                            [.VP to\\TO
                               [.VP remain\\VB
                                  [.ADJP steady\\JJ ]
                                  [.PP at\\IN
                                     [.NP
                                        [.QP about\\IN 1200\\CD ]
                                        cars\\NNS
                                     ]
                                  ]
                               ]
                            ]
                         ]
                      ]
                   ]
                ]
             ]
         ]
\end{tikzpicture}
\end{document}

Result:

Example 3

Finally, one last example:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree,tikz-qtree-compat}

\begin{document}
  \begin{tikzpicture}[every node/.style={align=center},level distance=30pt]
    \tikzset{edge from parent/.append style={<-, >=latex,thick}}
   \Tree [.said\\VBD
             [.Inc.\\NNP Rolls-Royce\\NNP Motor\\NNP Cars\\NNPS ]
             [.expects\\VBZ it\\PRP
                [.remain\\VB
                   [.sales\\NNS its\\PRP\$ U.S\\NNP ]
                   to\\TO
                   steady\\JJ
                   [.at\\IN [.cars\\NNS [.about\\IN 1200\\CD ] ] ]
                ]
             ]
           ]
   \end{tikzpicture}
\end{document}

Drawing a Support Vector Machine In tikz

I’ve also had to draw a visual explanation of what a SVM is:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree,tikz-qtree-compat}
\usetikzlibrary{calc}

\begin{document}


  \tikzset{
    leftNode/.style={circle,minimum width=.5ex, fill=none,draw},
    rightNode/.style={circle,minimum width=.5ex, fill=black,thick,draw},
    rightNodeInLine/.style={solid,circle,minimum width=.7ex, fill=black,thick,draw=white},
    leftNodeInLine/.style={solid,circle,minimum width=.7ex, fill=none,thick,draw},
  }
  \begin{tikzpicture}[
        scale=2,
        important line/.style={thick}, dashed line/.style={dashed, thin},
        every node/.style={color=black},
    ]
    \draw[dashed line, yshift=.7cm]
       (.2,.2) coordinate (sls) -- (2.5,2.5) coordinate (sle)
       node[solid,circle,minimum width=2.8ex,fill=none,thick,draw] (name) at (2,2){}
       node[leftNodeInLine] (name) at (2,2){}
       node[solid,circle,minimum width=2.8ex,fill=none,thick,draw] (name) at (1.5,1.5){}
       node[leftNodeInLine] (name) at (1.5,1.5){}
       node [above right] {$w\cdot x + b > 1$};

    \draw[important line]
       (.7,.7) coordinate (lines) -- (3,3) coordinate (linee)
       node [above right] {$w\cdot x + b = 0$};

    \draw[dashed line, xshift=.7cm]
       (.2,.2) coordinate (ils) -- (2.5,2.5) coordinate (ile)
       node[solid,circle,minimum width=2.8ex,fill=none,thick,draw] (name) at (1.8,1.8){}
       node[rightNodeInLine] (name) at (1.8,1.8){}
       node [above right] {$w\cdot x + b < -1$};

    \draw[very thick,<->] ($(sls)+(.2,.2)$) -- ($(ils)+(.2,.2)$)
       node[sloped,above, near end] {Margen};

    \foreach \Point in {(.9,2.4), (1.3,2.5), (1.3,2.1), (2,3), (1,2.9)}{
      \draw \Point node[leftNode]{};
    }

    \foreach \Point in {(2.9,1.4), (2.3,.5), (3.3,.1), (2,0.9), (2.5,1)}{
      \draw \Point node[rightNode]{};
    }
  \end{tikzpicture}
\end{document}

References

While writing this snippets, I had to ask some questions on tex.stackexchange.com, those questions can be found in the following links:

Spot a typo?: Help me fix it by contacting me or commenting below!

Categories:Tags: