Take the 2-minute tour ×
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.

I would like to center a directory tree produced by the package dirtree but the following code fails. Just look at the 2nd tree... All the texts are centered. :-(

M(not)WE

\documentclass{scrartcl} 
    \usepackage[utf8]{inputenc}
    \usepackage{dirtree}


\begin{document}

\dirtree{%
    .1 sudoku/.
        .2 \_\_init\_\_.py
             \ldots{}
             \begin{minipage}[t]{5cm}
                This directory holds executable files (binary
                files or link on binary files){.}
            \end{minipage}.
        .2 file1.py.
        .2 file2.py.
}

\begin{center}
\dirtree{%
    .1 sudoku/.
        .2 \_\_init\_\_.py
             \ldots{}
             \begin{minipage}[t]{5cm}
                This directory holds executable files (binary
                files or link on binary files){.}
            \end{minipage}.
        .2 file1.py.
        .2 file2.py.
}
\end{center}

\end{document}

OUPUT

enter image description here

share|improve this question
    
This redefinition of \DTstyle helps: \renewcommand*{\DTstyle}[1]{\texttt{#1}\hfill} but this is more of a work-around. Related (same symptom, different goal): floatrow and dirtree clash –  Qrrbrbirlbel Feb 27 '13 at 16:04
    
Indeed, it doesn't center the tree. –  projetmbc Feb 27 '13 at 16:07
    
Yes, I see. The whole tree is probably put into one \linewidth wide box, so centering that box doesn’t help you much. I also do not see any interface by dirtree to change that … –  Qrrbrbirlbel Feb 27 '13 at 16:20
    
@Qrrbrbirlbel interface, what's an interface?:-) –  David Carlisle Feb 27 '13 at 16:21
    
@DavidCarlisle Interface is a way to help other users to interact with a program. –  projetmbc Feb 27 '13 at 16:32
add comment

1 Answer

up vote 6 down vote accepted

It is being centred by \begin{center} but it is always full width so it doesn't actually make any difference. So you need to modify \dirtree not to make a full width box, something like this:

enter image description here

For 2012 version of dirtree you need the following redefinition, for older versions the code in the original answer should work, see %%2012 comments for the differences

\makeatletter
\def\dirtree#1{%
  %%2012\let
  \DT@indent=\parindent
  \parindent=\z@
  %%2012\let
  \DT@parskip=\parskip
  \parskip=\z@
  %%2012\let
  \DT@baselineskip=\baselineskip
  \baselineskip=\DTbaselineskip
  \let\DT@strut=\strut
  \def\strut{\vrule width\z@ height0.7\baselineskip depth0.3\baselineskip}%
  \DT@counti=\z@
  \let\next\DT@readarg
  \next#1\@nil
  \dimen\z@=\hsize
  \advance\dimen\z@ -\DT@offset
  \advance\dimen\z@ -\DT@width
%  \setbox\z@=\hbox to\dimen\z@{%
  \setbox\z@=\hbox{%
%    \hsize=\dimen\z@
    \vbox{\hbox{\@nameuse{DT@body@1}}}%
  }%
  \dimen\z@=\ht\z@
  \advance\dimen0 by\dp\z@
  \advance\dimen0 by-0.7\baselineskip
  \ht\z@=0.7\baselineskip
  \dp\z@=\dimen\z@
  \par\leavevmode
  \kern\DT@offset
  \kern\DT@width
  \box\z@
  \endgraf
  \DT@countii=\@ne
  \DT@countiii=\z@
  \dimen3=\dimen\z@
  \@namedef{DT@lastlevel@1}{-0.7\baselineskip}%
  \loop
  \ifnum\DT@countii<\DT@counti
    \advance\DT@countii \@ne
    \advance\DT@countiii \@ne
    \dimen\z@=\@nameuse{DT@level@\the\DT@countii}\DT@all
    \advance\dimen\z@ by\DT@offset
    \advance\dimen\z@ by-\DT@all
    \leavevmode
    \kern\dimen\z@
    \DT@countiv=\DT@countii
    \count@=\z@
    %%2012\LOOP
    \DT@loop
      \advance\DT@countiv \m@ne
      \ifnum\@nameuse{DT@level@\the\DT@countiv} >
        \@nameuse{DT@level@\the\DT@countii}\relax
      \else
        \count@=\@ne
      \fi
    \ifnum\count@=\z@
    %%2012\REPEAT
    \DT@repeat
    \edef\DT@hsize{\the\hsize}%
    \count@=\@nameuse{DT@level@\the\DT@countii}\relax
    \dimen\z@=\count@\DT@all
    \advance\hsize by-\dimen\z@
    \setbox\z@=\vbox{\hbox{X\@nameuse{DT@body@\the\DT@countii}}}%
    \hsize=\DT@hsize
    \dimen\z@=\ht\z@
    \advance\dimen\z@ by\dp\z@
    \advance\dimen\z@ by-0.7\baselineskip
    \ht\z@=0.7\baselineskip
    \dp\z@=\dimen\z@
    \@nameedef{DT@lastlevel@\the\DT@countii}{\the\dimen3}%
    \advance\dimen3 by\dimen\z@
    \advance\dimen3 by0.7\baselineskip
    \dimen\z@=\@nameuse{DT@lastlevel@\the\DT@countii}\relax
    \advance\dimen\z@ by-\@nameuse{DT@lastlevel@\the\DT@countiv}\relax
    \advance\dimen\z@ by0.3\baselineskip
    \ifnum\@nameuse{DT@level@\the\DT@countiv} <
        \@nameuse{DT@level@\the\DT@countii}\relax
      \advance\dimen\z@ by-0.5\baselineskip
    \fi
    \kern-0.5\DT@rulewidth
    \hbox{\vbox to\z@{\vss\hrule width\DT@rulewidth height\dimen\z@}}%
    \kern-0.5\DT@rulewidth
    \kern-0.5\DT@dotwidth
    \vrule width\DT@dotwidth height0.5\DT@dotwidth depth0.5\DT@dotwidth
    \kern-0.5\DT@dotwidth
    \vrule width\DT@width height0.5\DT@rulewidth depth0.5\DT@rulewidth
    \kern\DT@sep
    \hbox{\box\z@}%
    \endgraf
  \repeat
  \parindent=\DT@indent
  \parskip=\DT@parskip
  %%2012\DT@baselineskip=\baselineskip
  \baselineskip=\DT@baselineskip
  \let\strut\DT@strut
}

\makeatother

Original version:


\documentclass{scrartcl} 
    \usepackage[utf8]{inputenc}
    \usepackage{dirtree,varwidth}

\makeatletter
\def\dirtree#1{%
  \let\DT@indent=\parindent
  \parindent=\z@
  \let\DT@parskip=\parskip
  \parskip=\z@
  \let\DT@baselineskip=\baselineskip
  \baselineskip=\DTbaselineskip
  \let\DT@strut=\strut
  \def\strut{\vrule width\z@ height0.7\baselineskip depth0.3\baselineskip}%
  \DT@counti=\z@
  \let\next\DT@readarg
  \next#1\@nil
  \dimen\z@=\hsize
  \advance\dimen\z@ -\DT@offset
  \advance\dimen\z@ -\DT@width
%  \setbox\z@=\hbox to\dimen\z@{%
  \setbox\z@=\hbox{%
%    \hsize=\dimen\z@
    \vbox{\hbox{\@nameuse{DT@body@1}}}%
  }%
  \dimen\z@=\ht\z@
  \advance\dimen0 by\dp\z@
  \advance\dimen0 by-0.7\baselineskip
  \ht\z@=0.7\baselineskip
  \dp\z@=\dimen\z@
  \par\leavevmode
  \kern\DT@offset
  \kern\DT@width
  \box\z@
  \endgraf
  \DT@countii=\@ne
  \DT@countiii=\z@
  \dimen3=\dimen\z@
  \@namedef{DT@lastlevel@1}{-0.7\baselineskip}%
  \loop
  \ifnum\DT@countii<\DT@counti
    \advance\DT@countii \@ne
    \advance\DT@countiii \@ne
    \dimen\z@=\@nameuse{DT@level@\the\DT@countii}\DT@all
    \advance\dimen\z@ by\DT@offset
    \advance\dimen\z@ by-\DT@all
    \leavevmode
    \kern\dimen\z@
    \DT@countiv=\DT@countii
    \count@=\z@
    \LOOP
      \advance\DT@countiv \m@ne
      \ifnum\@nameuse{DT@level@\the\DT@countiv} >
        \@nameuse{DT@level@\the\DT@countii}\relax
      \else
        \count@=\@ne
      \fi
    \ifnum\count@=\z@
    \REPEAT
    \edef\DT@hsize{\the\hsize}%
    \count@=\@nameuse{DT@level@\the\DT@countii}\relax
    \dimen\z@=\count@\DT@all
    \advance\hsize by-\dimen\z@
    \setbox\z@=\vbox{\hbox{X\@nameuse{DT@body@\the\DT@countii}}}%
    \hsize=\DT@hsize
    \dimen\z@=\ht\z@
    \advance\dimen\z@ by\dp\z@
    \advance\dimen\z@ by-0.7\baselineskip
    \ht\z@=0.7\baselineskip
    \dp\z@=\dimen\z@
    \@nameedef{DT@lastlevel@\the\DT@countii}{\the\dimen3}%
    \advance\dimen3 by\dimen\z@
    \advance\dimen3 by0.7\baselineskip
    \dimen\z@=\@nameuse{DT@lastlevel@\the\DT@countii}\relax
    \advance\dimen\z@ by-\@nameuse{DT@lastlevel@\the\DT@countiv}\relax
    \advance\dimen\z@ by0.3\baselineskip
    \ifnum\@nameuse{DT@level@\the\DT@countiv} <
        \@nameuse{DT@level@\the\DT@countii}\relax
      \advance\dimen\z@ by-0.5\baselineskip
    \fi
    \kern-0.5\DT@rulewidth
    \hbox{\vbox to\z@{\vss\hrule width\DT@rulewidth height\dimen\z@}}%
    \kern-0.5\DT@rulewidth
    \kern-0.5\DT@dotwidth
    \vrule width\DT@dotwidth height0.5\DT@dotwidth depth0.5\DT@dotwidth
    \kern-0.5\DT@dotwidth
    \vrule width\DT@width height0.5\DT@rulewidth depth0.5\DT@rulewidth
    \kern\DT@sep
    \hbox{\box\z@}%
    \endgraf
  \repeat
  \parindent=\DT@indent
  \parskip=\DT@parskip
  \DT@baselineskip=\baselineskip
  \let\strut\DT@strut
}

\makeatother

\begin{document}

\noindent X\dotfill X

\dirtree{%
    .1 sudoku/.
        .2 \_\_init\_\_.py
             \ldots{}
             \begin{minipage}[t]{5cm}
                This directory holds executable files (binary
                files or link on binary files){.}
            \end{minipage}.
        .2 file1.py.
        .2 file2.py.
}

\begin{center}
\begin{varwidth}{\textwidth}
  \dirtree{%
    .1 sudoku/.
        .2 \_\_init\_\_.py
             \ldots{}
             \begin{minipage}[t]{5cm}
                This directory holds executable files (binary
                files or link on binary files){.}
            \end{minipage}.
        .2 file1.py.
        .2 file2.py.
}
\end{varwidth}
\end{center}

\end{document}
share|improve this answer
    
Thanks for this so simple solution... I'm kiding. Thanks ! –  projetmbc Feb 27 '13 at 16:34
    
@projetmbc well I only changed 2 or 3 lines but it wasn't very convenient to offer a "patch" and unfortunately the whole dirtree is essentially one macro so I just copied it all and changed it here and there. –  David Carlisle Feb 27 '13 at 16:38
    
I've one problem of compilation. Herer is the error sent : No file centeredDirTree.aux. ! Undefined control sequence. \iterate ...untiv =\DT@countii \count@ =\z@ \LOOP \advance \DT@countiv \m@ne... l.116 }. –  projetmbc Feb 27 '13 at 16:48
    
I've just copied and pasted the code in my file. –  projetmbc Feb 27 '13 at 16:57
1  
@projetmbc dirtree 2012 uses the name DT@loop instead of LOOP so I posted an update to the answer. –  David Carlisle Feb 27 '13 at 17:32
show 3 more comments

Your Answer

 
discard

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.