将目录树居中

将目录树居中

我想让该包生成的目录树居中,dirtree但以下代码失败了。看看第二棵树……所有文本都居中了。:-(

M(不)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}

输出

在此处输入图片描述

答案1

它被居中\begin{center},但始终是全宽的,所以实际上没有任何区别。因此,您需要进行修改,\dirtree不要制作全宽框,如下所示:

在此处输入图片描述

对于 2012 版本的 dirtree,您需要进行以下重新定义,对于旧版本,原始答案中的代码应该可以工作,请参阅%%2012注释以了解差异

\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

原始版本:


\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}

答案2

一个简单但不优雅的解决方案是将 放在dirtree一个与 宽度完全相同的迷你页面中dirtree。如果树会随时间发生很大变化,宽度可能必须更新。我不知道是否可以以某种方式计算它的宽度。

\begin{figure}
\centering
\begin{minipage}{7cm}
\dirtree{%
.1 /.
.2 +example.
.3 gtp\_constraint.m.
.3 gtp\_objective.m.
.3 gtp\_input\_decoder.m.
.3 main.m.
.2 mofepso.m.
.2 mofepsooptions.m.
}
\end{minipage}
\end{figure}

相关内容