Wednesday, June 15, 2011

Dependency Graphs

I have to create a lot of dependency graphs for papers, and I've tried three different methods with success:

  • the LaTex pstricks package - This is my favorite because it allows you to type a series of commands directly into your paper, and you end up with a beautiful graph. You can create a lovely graph and view it in pdf without ever lifting your hands from the keyboardr. Better yet, the more you know, the more beautiful a graph you can produce! I learned this by emulating the code of my Computational Linguistics professor, Dr. Sandra Kuebler. She knows a lot about LaTex. Sadly, my Cognitive Science professor, Dr. Matthias Scheutz, says pstricks is a "really old package" that "no one uses anymore", requiring me to use...
  • OmniGraffle - I also love this method. It's laborious, but you can create your graphs without ever lifting your hands from the mouse, and you hardly have to know anything to produce beautiful graphs. Sadly, this costs money, and I'm a poor student, necessitating me to use...
  • dia - the free open-source version of OmniGraffle.
 These are all good method, but one of them requires more knowledge than the others. That's why, in this blog post, I will be explaining how to, step by step, create a simple dependency graph in LaTex using pstricks.

Draw your desired dependency graph by hand. This will make entering it into the computer much easier.  

Set up your document to accept the graph by making sure you are importing the correct packages. Most likely, you'll need the following in your preamble:

    Create the proper environment, which consists of two pstricks environments that you'll create inside a normal figure environment:

    \begin{psmatrix}[nodesep=z, rowsep=q, colsep=r]

    (x,y) are the desired width and height of the figure you'll be creating, as dimensionless units. Playing with the width when the pspicture is centered within the figure environment had the effect of offsetting center, so leave this 0, but it has no effect on any border. Playing with the height actually changes the height of a border around the graph. If you do have a border, it is possible to make the size small enough that the border falls on the graph, rather than around it.

    z, q, r are parameters in the formed of dimensioned units, e.g. 2pt, 0.5cm, or 0.3cm [the values I used in the example I copied these instructions from]

    Rowsep is only important if you have multiple rows. Just play with these values until things are placed as you like them.

    Type in the sentence to graph as the first line in the psmatrix. Since it's a matrix, they'll be separated by ampersands rather than spaces:


    Set the default values.

    Arrow direction: Decide whether you'd rather chant to yourself: "2 from 1", or "1 to 2".  As I'm transferring my graph, I look at an arc, chant one of the above while I enter it into the computer. If you would rather start with the node the arrow is pointing towards, your direction will be "<-". If you would rather start with the node the arrow is pointing from, your direction will be "->":




    Default arc height: If you want the default to be the shortest, choose 20pt. If you want it to be medium, chose 40pt.

    Count the number of arcs you will need to enter, and begin entering them. For each arc, add the following line:

    \ncbar[]{1,}{1,}\nbput{\small }

    when you have a completed arc, the line will look something like:

    \ncbar[]{1,x}{1,y}\nbput{\small z}

    where x is the node you start with, so the one the arrow is pointing towards or from, depending on the direction you selected, y is the second node, and z is the label of the arc. Inside the square brackets will be options such as how the arrow is centered horizontally above the node, and how tall the arrow is.

    For each arc, enter the node numbers and the label.

    \ncbar[]{1,2}{1,1}\nbput{\small obj} 

    For me, this created an object arc to node 2 from node 1.
    Enter the arc heights.

    If you have multiple arcs to, or from a given node, they will need to be different heights so the arrows are clearly differentiated from each other. I do this starting from the end by visually selecting in turn each node from that has multiple arrows leaving or arriving at it. If your graph does not contain crossing dependencies, some of the arcs will be "inside" others:

    [insert picture]

    make the inside 1 20pt, and add 20pts for each enveloping arc.

    Enter this informationi so that the lines look something like:

    \ncbar[arm=60pt ]{1,8}{1,5}\nbput{\small obj}
    \ncbar[arm=40pt ]{1,6}{1,8}\nbput{\small obj}
    \ncbar[arm=20pt ]{1,7}{1,8}\nbput{\small obj}

    Set the horizontal offsets. The arrows will all start from and arrive at the same point on the node unless you offset some of the arrows. There's an offset for the first node listed [offsetA] and the second node listed[offsetB].

    So in the line

    \ncbar[offsetB=-3pt]{1,8}{1,7}\nbput{\small }

    The arrow pointing to node 7 is offset by -3pt.

    For offsetA, a negative number moves it right, while for offsetB, a negative number moves it left.

    Enter the offsets like this:

    \ncbar[arm=60pt,offsetA=1.5pt]{1,5}{1,8}\nbput{\small }
    \ncbar[arm=40pt,offsetB=-3pt]{1,8}{1,6}\nbput{\small }
    \ncbar[offsetB=0pt]{1,8}{1,7}\nbput{\small }

    If you use spaces instead of commas, the words after the first space will all be listed under the graph instead of being executed in the graph.

    Compile. You won't be able to use pdflatex anymore, instead you'll have to use latex followed by dvipdf:

    latex example.tex
    dvipdf example.dvi

    Unfortunately, if you are sharing a tex file with someone, say over subversion, they may be annoyed that you have now broken pdflatex. They may be in a position to insist you do not do this (see above). How could this be fixed? If you can produce a graphic from a file with just the information above, and then insert that file into a document. I haven't figured out how to do it, but I'd sure like to!

    No comments:

    Post a Comment