15

Please show me the most simple, elegant code for this:

enter image description here

How to do it in other words.

lockstep
  • 250,273
ptrcao
  • 6,603
  • You could have a look at TikZ and its possibilities to draw trees: http://www.texample.net/tikz/examples/feature/trees/ (I admit I never used them myself yet, so I can not quickly create an example) – MostlyHarmless May 25 '11 at 17:12
  • 1
    ...or use the Qtree package (http://www.ling.upenn.edu/advice/latex/qtree/) as recommended here: http://tex.stackexchange.com/questions/5447/how-can-i-draw-simple-trees-in-latex (however the lines are straight and not horizontal/vertical as in your sketch) – MostlyHarmless May 25 '11 at 17:19
  • Any suggestions on how to add the labels on the left hand side (Class, Family, ...)? Also I would like the style of connectors as shown above.

    If anyone's still game, could they please show me the code, as I am still learning.

    – ptrcao May 25 '11 at 17:44

2 Answers2

18

You can do this sort of tree easily with the tikz-qtree package:

In the code below, the labels are created using a tree with branches that are not actually drawn (using a style blank which sets [draw=none]). This ensures that they line up with the levels of the main tree. By putting two trees as nodes in a matrix we achieve a reasonable separation between the labels and the main tree.

\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\tikzset{edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode.south)
-- +(0,-8pt)
-| (\tikzchildnode)}},
blank/.style={draw=none}}
\begin{tikzpicture}
\matrix
{
\node{\Tree
    [.Class  \edge[blank]; 
    [.Order  \edge[blank];
    [.Family \edge[blank]; 
    [.Genus ]]]]};
&
\node{\Tree 
 [.{$\gamma$-proteobacteria} 
    [.Alteromonadales 
        [.Alteromonadaceae  {Glaciecola}  Alteromonas Agarivorans ] ]
    [.Vibrionales [.Vibrionacae Vibrio ]]]};\\
};           
\end{tikzpicture}

\end{document}

output of code

David Carlisle
  • 757,742
Alan Munn
  • 218,180
  • Thanks. Is there any way to create a bit more distance between the labels on the left hand side (Class, Order, ...) and the graph? – ptrcao May 26 '11 at 01:08
  • 2
    @ptrcao I've modified the example to make two trees in a matrix of nodes instead of one tree. This gives more space between the labels, since the labels are no longer connected to the main tree. – Alan Munn May 26 '11 at 02:24
10

run with xelatex

\documentclass{article}
\usepackage{pst-tree}
\def\psedge{\ncangles[angleA=-90,angleB=90]}
\psset{levelsep=14mm,treesep=1.75cm,nodesep=3pt}
\begin{document}

\hspace*{1.3cm}
\pstree{\Tr[name=class]{$\gamma$-proteobacteria}}{%
  \pstree{\Tr[name=order]{Alteromonadales}}{%
    \pstree[thistreesep=3mm]{\Tr[name=family]{Alteromonadaceae}}{%
       \Tr[name=genus]{Glaciecola}
       \Tr{Alteromonas} 
       \Tr{Agarivorans}%
  }}
  \pstree{\Tr{Vibrionales}}{%
    \pstree{\Tr{Vibrionacae}}{%
      \Tr{Vibrio}
  }}
}

\rput[rc](0,0|class){Class}\rput[rc](0,0|order){Order}%
\rput[rc](0,0|family){Family}\rput[rc](0,0|genus){Genus}%
\end{document}

enter image description here

  • Any way to offset the Alteromonas branch slightly? I'm not sure if it's necessary but I see it often being done in biology and I'm tempted to think it's deliberate, to avoid giving that branch the appearance of receiving special treatment or greater relational weight, relative to the others... – ptrcao May 26 '11 at 00:46
  • 1
    all can be set locally by thistreesep or thislevelsep –  May 26 '11 at 06:37
  • @Herbert I'm sorry for being a nuisance, can you please show explicitly? I'm really new at this! :| – ptrcao May 26 '11 at 08:35
  • @ptrcao: then tell me what exactly should be modified. For example: \pstree[thistreesep=11mm]{\Tr[name=family]{Alteromonadaceae}}{% –  May 26 '11 at 08:41
  • The line extending down to Alteromonas has to be offset by a couple of mm - right or left, it doesn't matter. This is so that it is deliberately misaligned with the vertical line directly above it descending from Alteromonadaceae. If this is confusing, just look at the image in the OP, and take it literally rather than by accident that the Alteromonas branch is slight misaligned... Thanks. – ptrcao May 26 '11 at 08:49
  • See also the alternative solution above where the vertical line is offset in the described manner. Thanks. – ptrcao May 26 '11 at 08:57
  • 1
    @ptrcao: ah that's simple, see edited answer –  May 26 '11 at 09:08
  • @Herbert Brilliant, thanks. :) Last request - is there a way to make it work with latexmk? I never really use xelatex and don't even know what it's for. Ultimately, my bibliographies need latexmk to be properly compiled... – ptrcao May 26 '11 at 10:48
  • 1
    @ptrcao: insert -shell-escape after the %O and load package autp-pst-pdf ($pdflatex=q/pdflatex -synctex=1 %O -shell-escape %S/). You have to insert a new entry (+) move it up and then delete the old one. It makes sense that you have installed Perl (http://www.actibestate.com/perl/) then the very useful program pdfcrop can be used. –  May 26 '11 at 12:34