2

I'm fairly new to tikz-dependency,tikz and pgfplot and I am trying to use it for plotting the output of several dependency parsers. However whenever I render a document using PDFLateX or LatexMk I end up with the following output with the graph centered in an odd position.

Misaligned dependency graph The code to generate such an output is:

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{pgfplots}
\usepackage{tikz-dependency}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\begin{dependency}
    \begin{deptext}[column sep=0.2cm]
        The \& old \& car \& broke \& down \& in \& the \& car \& park \\
    \end{deptext}
\deproot{4}{root}
\depedge{3}{1}{det}
\depedge{3}{2}{amod}
\depedge{4}{3}{nsubj}
\depedge{4}{5}{compound:prt}
\depedge{9}{6}{case}
\depedge{9}{7}{det}
\depedge{9}{8}{compound}    
\depedge{4}{9}{nmod:in}
\end{dependency}
\end{tikzpicture}
\caption{Stanford Output Sentence 1}
\end{figure}
\end{document}

I'm using:

  • Mac OS X 10.13.2
  • pdfTeX 3.14159265-2.6-1.40.18 (TeX Live 2017) or
  • LatexMk 4.52c with the same issue with both build tools.

The caption leads me to believe the figure is centered but I guess some part of how I declare the tikzpicture might be wrong. I'm not sure how to solve this and I hope someone else has come across this before and workout out how to fix this.

2 Answers2

1

Simply removing the tikzpicture from your code results in centered diagram:

Sample output

\documentclass{article}

\usepackage[margin=1cm]{geometry}
\usepackage{pgfplots}
\usepackage{tikz-dependency}
\pgfplotsset{compat=1.14}

\begin{document}
\begin{figure}
  \centering
  \begin{dependency}
    \begin{deptext}[column sep=0.2cm]
      The \& old \& car \& broke \& down \& in \& the \& car \& park \\
    \end{deptext}
    \deproot{4}{root}
    \depedge{3}{1}{det}
    \depedge{3}{2}{amod}
    \depedge{4}{3}{nsubj}
    \depedge{4}{5}{compound:prt}
    \depedge{9}{6}{case}
    \depedge{9}{7}{det}
    \depedge{9}{8}{compound}
    \depedge{4}{9}{nmod:in}
  \end{dependency}
  \caption{Stanford Output Sentence 1}
\end{figure}
\end{document}

Indeed you should not include a dependency inside a tikzpicture and as the dependency environment already creates a tikzpicture and nesting of tikzpicture environments does not behave well. See What should be included in "good practices" for Tikz 101 for good tikz practices including not nesting.

By the way, you should not really be using [!h]: figures are floats and designed to be moved around. If you want your diagram at particular place in the text then you can use the center environment combined with \captionof from the caption package to add a caption. See How to influence the position of float environments like figure and table in LaTeX?

Andrew Swann
  • 95,762
0

Answering my own question with something that works well enough for my needs. I didn't know about the usetikzlibrary{positioning} command for my preamble. When I put that in and changed the first two lines of the figure to:

\begin{figure}[p]
\begin{tikzpicture}[align=center]

then I got what I needed.

  • Also I got rid of \centering – user3552510 Jan 02 '18 at 16:33
  • While that sort of gets you what you're after, it is more or less by accident, and I have to say that the approach is entirely wrong. Andrew Swann's comment above is the correct answer: remove \begin{tikzpicture} and \end{tikzpicture}. The dependency environment is not supposed to be used inside a tikzpicture. – Torbjørn T. Jan 14 '18 at 13:35