7

How can I reduce the overall size of trees drawn by tikz-qtree? I've got a huge parse tree that doesn't even try to fit on one page. I'd like to either resize the full tree or tweak the placing algorithm to make the tree more dense.

1 Answers1

8

Put the entire tree in a \resizebox from the graphicx package. Here's what the graphics guide has to say about this command:

\resizebox{⟨h-length⟩}{⟨v-length⟩}{⟨text⟩}

Scale text so that the width is h-length. If ! is used as either length argument, the other argument is used to determine a scale factor that is used in both directions. Normally v-length refers to the height of the box, but in the star form, it refers to the ‘height + depth’. As normal for LaTeX2ε box length arguments, \height, \width, \totalheight, \depth may be used to refer to the original size of the box.

\bigskip, \newpage, \hfill, and \hspace{0pt} are just used to make the example spacing look nicer. Note that in order for the vertical resizing to work, you need to put the \Tree in a tikzpicture environment. And of course, you can also just use 5cm as a length; I only included lengths related to typesetting dimensions in my examples.

\documentclass[12pt]{article}

\usepackage[a6paper,showframe]{geometry}% just to demonstrate the effects

\usepackage{graphicx}

\usepackage{tikz-qtree}

\begin{document}
\setlength{\parindent}{0cm}% get rid of paragraph indents for this example

%
% HORIZONTAL RESIZING FOR WIDE TREES
%

Original Size:

\Tree[.Top A B C D E F G H I J K L M N O P ]

\bigskip

Full linewidth:

\resizebox{\linewidth}{!}{%
\Tree[.Top A B C D E F G H I J K L M N O P ]
}

\bigskip

Linewidth minus 1cm:

\resizebox{\dimexpr\linewidth-1cm}{!}{%
\Tree[.Top A B C D E F G H I J K L M N O P ]
}

\bigskip

Half linewidth:

\resizebox{.5\linewidth}{!}{%
\Tree[.Top A B C D E F G H I J K L M N O P ]
}

\bigskip

%
% VERTICAL RESIZING FOR TALL TREES
%

Page 2: original size

Page 3: textheight, textheight minus 1cm, half textheight

\newpage

\begin{tikzpicture}\Tree[.A [.B [.C [.D [.E [.F [.G [.H [.I [.J [.K [.L [.M [.N [.O [. P ]]]]]]]]]]]]]]]]\end{tikzpicture}

\newpage

\hfill
\resizebox{!}{\textheight}{\begin{tikzpicture}
\Tree[.A [.B [.C [.D [.E [.F [.G [.H [.I [.J [.K [.L [.M [.N [.O [. P ]]]]]]]]]]]]]]]]
\end{tikzpicture}}
\hfill
\resizebox{!}{\dimexpr\textheight-1cm}{\begin{tikzpicture}
\Tree[.A [.B [.C [.D [.E [.F [.G [.H [.I [.J [.K [.L [.M [.N [.O [. P ]]]]]]]]]]]]]]]]
\end{tikzpicture}}
\hfill
\resizebox{!}{.5\textheight}{\begin{tikzpicture}
\Tree[.A [.B [.C [.D [.E [.F [.G [.H [.I [.J [.K [.L [.M [.N [.O [. P ]]]]]]]]]]]]]]]]
\end{tikzpicture}}
\hfill\hspace{0pt}

\end{document}

output page 1

output page 2

output page 3

doncherry
  • 54,637
  • Nice, how can I adjust it? Needs to be a bit smaller than that. – Reactormonk Oct 24 '12 at 22:56
  • @Tass I'm working on an example :) – doncherry Oct 24 '12 at 22:58
  • @Tass Does this answer your question? – doncherry Oct 24 '12 at 23:20
  • @doncherry Is it OK if I vote for duplicate to this question http://tex.stackexchange.com/questions/39944/visualizing-broad-tikz-tree-graph pointing to this? It's been lingering for ages... – percusse Oct 24 '12 at 23:22
  • @percusse: You mean closing the "visualizing" question? That might make sense. But then again, I could imagine there's an answer to that other question involving TikZ parameters, which wouldn't apply to this one here? I really don't know a lot about TikZ, so I'm unsure. This question here should certainly at least be mentioned in a comment to the other question. – doncherry Oct 24 '12 at 23:27
  • @doncherry Indeed other question is asked in such a way that paralyzes our system :) – percusse Oct 24 '12 at 23:29
  • 2
    No, this isn't really a duplicate because the size of the other tree would turn into microtype (in the Cold War spy sense) if you shrunk it using resize box. Also, shrinking in this way is always a bit of a compromise. @Tass might also consider using the pdflscape package to put the tree on a landscape page within a regular document. – Alan Munn Oct 25 '12 at 01:20
  • @AlanMunn The ratio of the tree size is approx. that of a page. – Reactormonk Oct 25 '12 at 01:47
  • @AlanMunn I don't really understand why we keep that question open. It's no way answerable anyway without shrinking the table. It just suggests that a tree is not the way to go. – percusse Oct 27 '12 at 23:01
  • @percusse While I don't know a lot about TikZ (trees), I would expect there are way of making a tree more compact without just shrinking the entire thing, e.g. by making the leaves closer to each other, having it grow in another direction, decreasing font size, positioning the leaves in a special manner, limiting characters per line in the leaves. I think that'd be a way to keep these two questions as separate and useful questions. (Probably, both would need some minor rewording to go in these directions.) – doncherry Oct 27 '12 at 23:23
  • @doncherry But that means it's a duplicate since we have a lot of sibling/level distance questions here too. But none is applicable because there is no MWE or any kind of useful info on that. Maybe labels overlap maybe some other complication etc... – percusse Oct 27 '12 at 23:42
  • @percusse Ah, I didn't know/consider that. Yes, then it may well be a duplicate, but not of this question. – doncherry Oct 27 '12 at 23:57