TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a (rather long-ish) hierarchical structure, typeset with the dirtree package. However, I have some issues when breaking the tree over to a new page -- the line may protrude up to the very top of the sheet writeable area, extending into the headings area.

Note that I used the headings here only to better illustrate the issue, they are not necessary to exemplify the problem. But I do have a similar page setup, and it looks particularly unpleasant with the horizontal rule being intersected by the tree lines.

Here's an example:



.1 spam.
.2 eggs.
.2 ham.
.3 circus.
.3 parrot.
.4 knights.
.3 spamalot.
.4 ni.
.5 brian.
.5 vikings.
.2 meaning.

Now, the package documentation says that a command \DTsplittree is in the To-Do list, that will allow splitting the tree over several pages. Any ideas for a dirty hack or a workaround that I might be able to use in the meantime?

I've used some other tricks from the book, like reducing the line spacing and playing with font size, and I guess I could split it in two a few smaller parts (which I will have to fit again between pages!) if I can't find another way of doing it. But I'm more interested in a TeXnical solution.

Update Let's see if this will get answered if some 100 bonus rep is thrown at it.

share|improve this question
up vote 7 down vote accepted

The dirtree package is neat! Below is a slight modification of the package to page break sanely. It requires LaTeX and the zref package, so I don't claim it's the right solution, but it does seem to work (at least with extremely limited testing). As this modification uses references, it takes multiple runs of LaTeX to see the new page breaking behavior.

% This file was dirtree.tex. It was modified by TH. to support
% splitting the tree at pagebreaks sanely. It has been renamed to
% durtree.sty. It will now only work with LaTeX. It is meant as a
% quick hack to solve the problem at hand. Any bugs in it are my own,
% please don't blame M. Charpentier for them!
%% This is file `dirtree.tex',
%% generated with the docstrip utility.
%% The original source files were:
%% dirtree.dtx  (with options: `tex')
%% For the copyright see the source file.
%% Any modified versions of this file must be renamed
%% with new filenames distinct from dirtree.tex.
%% For distribution of the original source see the terms
%% for copying and modification in the file dirtree.dtx.
%% This generated file may be distributed as long as the
%% original source files, as listed above, are part of the
%% same distribution. (The sources need not necessarily be
%% in the same archive or directory.)
%% Package `dirtree.dtx'
%% -----------------------------------------------
%% Copyright (C) 2004-2006 Jean-C\^ome Charpentier
%% -----------------------------------------------
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2003/12/01 or later.
%% See CTAN archives in directory macros/latex/base/lppl.txt.
%%   This work consists of the files dirtree.ins and dirtree.dtx.
%%   Derived files are dirtree.tex and dirtree.sty.
%%   dirtree is a package displaying directory trees.
\ProvidesPackage{durtree}[\filedate\space v\fileversion]
\def\@nameedef#1{\expandafter\edef\csname #1\endcsname}
\newdimen\DT@offset \DT@offset=0.2em
\newdimen\DT@width \DT@width=1em
\newdimen\DT@sep \DT@sep=0.2em
\advance\DT@all \DT@width
\advance\DT@all \DT@sep
\newdimen\DT@rulewidth \DT@rulewidth=0.4pt
\newdimen\DT@dotwidth \DT@dotwidth=1.6pt
\newdimen\DTbaselineskip \DTbaselineskip=\baselineskip
\newcount\DT@treenum \DT@treenum=0
  \advance\DT@all by\DT@width
  \advance\DT@all by\DT@sep
  \global\advance\DT@treenum by\@ne
  \def\strut{\vrule width\z@ height0.7\baselineskip depth0.3\baselineskip}%
  \advance\dimen@ -\DT@offset
  \advance\dimen@ -\DT@width
  \setbox\z@=\hbox to\dimen@{%
  \advance\dimen@ by\dp\z@
  \advance\dimen@ by-0.7\baselineskip
    \advance\DT@countii \@ne
    \advance\DT@countiii \@ne
    \advance\dimen@ by\DT@offset
    \advance\dimen@ by-\DT@all
      \advance\DT@countiv \m@ne
      \ifnum\@nameuse{DT@level@\the\DT@countiv} >
      \ifnum0\zref@extract{DT\the\DT@treenum.\the\DT@countiv}{abspage} =
    \advance\hsize by-\dimen@
    \advance\dimen@ by\dp\z@
    \advance\dimen@ by-0.7\baselineskip
    \advance\dimen3 by\dimen@
    \advance\dimen3 by0.7\baselineskip
    \advance\dimen@ by-\@nameuse{DT@lastlevel@\the\DT@countiv}\relax
    \advance\dimen@ by0.3\baselineskip
    \ifnum\@nameuse{DT@level@\the\DT@countiv} <
      \advance\dimen@ by-0.5\baselineskip
      \advance\dimen@ by.4\baselineskip
    \hbox{\vbox to\z@{\vss\hrule width\DT@rulewidth height\dimen@}}%
    \vrule width\DT@dotwidth height0.5\DT@dotwidth depth0.5\DT@dotwidth
    \vrule width\DT@width height0.5\DT@rulewidth depth0.5\DT@rulewidth
\def\DT@readarg.#1 #2. #3\@nil{%
  \advance\DT@counti \@ne
  \expandafter\def\csname DT@body@\the\DT@counti\expandafter\endcsname
%% End of file `durtree.sty'.

It might be instructive to diff this file with dirtree.tex to see what I changed in detail. Basically, I added an absolute page ref after the first \strut that gets added around each entry. Then, when typesetting, it checks if the page number of the current entry matches the previous entry at that level. If they match, nothing different happens (\ifDT@split is false). Otherwise, it only draws the vertical rule to the top of the first entry on the current page.

I also changed the \dimen\z@ and \dimen0 to \dimen@. I guess this is technically slightly faster, but mostly it was annoying me.

share|improve this answer
@TH Barring a couple of pixels short on top left, the revised code works very well. You certainly deserve the bounty on top of which I would like to add another 100 - if there is a way to do it. If there is would you please post how on meta? – Yiannis Lazarides Dec 4 '10 at 16:01
@Yiannis: I'm not sure I know what you mean by a couple of pixels short on the top left. If there's a bug, I can try to fix it. – TH. Dec 4 '10 at 21:31
Оutstanding! I kinda lost hope for this to get answered, but I was pleasantly surprised by your answer. I can't spend much time right now studying the diff between the versions, but in the few quick tests I ran it works exactly the way I want it to. Thanks a lot! You deserve definitely all the rep I can throw your way for this answer. BTW, have you considered conntacting the package author to see whether he'll include some of your code in a future version of dirtree? – Martin Tapankov Dec 5 '10 at 17:59
@Martin: I hadn't really thought about it. dirtree works with both plain TeX and LaTeX. My solution works only with LaTeX. It wouldn't be too hard to implement a generic solution, but that'd basically be duplicating the reference mechanism which seems like the wrong solution. The tricky part is that TeX can construct more than a page of material before deciding that it is going to break the page so I'm not actually sure what the right way to handle this is. – TH. Dec 5 '10 at 23:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.