我能够使用 dirtree 手动在 LaTeX 中生成复杂的树项(请参阅下面的 MWE)。为了避免代码重复,我尝试在 LaTeX 命令中设置每个树项的声明。但是,我得到了错误! Use of \next doesn't match its definition.
预期结果如下:
以下是不使用命令的最小工作示例(MWE):
\documentclass[8pt]{scrartcl}
\usepackage{dirtree}
\usepackage{floatrow}
\usepackage{blindtext}
\thisfloatsetup{%
objectset=raggedright,
}
\begin{document}
\dirtree{%
.1 node1 \hfill \begin{minipage}[t]{1cm}
{ }
\end{minipage} \hfill \begin{minipage}[t]{3.5cm}
long comment multiline line number 1
\end{minipage}.
.2 node2 \hfill \begin{minipage}[t]{1cm}
type 0
\end{minipage} \hfill \begin{minipage}[t]{3.5cm}
long comment multiline line number 2
\end{minipage}.
.3 leaf1 \hfill \begin{minipage}[t]{1cm}
type 1
\end{minipage} \hfill \begin{minipage}[t]{3.5cm}
long comment multiline line number 3
\end{minipage}.
.3 leaf2 \hfill \begin{minipage}[t]{1cm}
type 1
\end{minipage} \hfill \begin{minipage}[t]{3.5cm}
long comment multiline line number 4
\end{minipage}.
.2 node3 \hfill \begin{minipage}[t]{1cm}
type 0
\end{minipage} \hfill \begin{minipage}[t]{3.5cm}
very very very very long comment multiline line number 5
\end{minipage}.
.3 leaf3 \hfill \begin{minipage}[t]{1cm}
type 2
\end{minipage} \hfill \begin{minipage}[t]{3.5cm}
long comment multiline line number 6
\end{minipage}.
.3 leaf4 \hfill \begin{minipage}[t]{1cm}
{}
\end{minipage} \hfill \begin{minipage}[t]{3.5cm}
no type specified
\end{minipage}.
}
\end{document}
这里尝试定义一个命令\treedef
以避免重复冗余代码
\documentclass[8pt]{scrartcl}
\usepackage{dirtree}
\usepackage{floatrow}
\usepackage{blindtext}
% #1: Level
% #2: Name
% #3: Type
% #4: Comment
\newcommand{\treedef}[4]{
.{#1} {#2} \hfill \begin{minipage}[t]{1cm}
{#3}
\end{minipage} \hfill \begin{minipage}[t]{3.5cm}
{#4}
\end{minipage}.
}
\thisfloatsetup{%
objectset=raggedright,
}
\begin{document}
\dirtree{%
\treedef{1}{node1}{}{long comment multiline line number 1}
\treedef{2}{node2}{type 0}{long comment multiline line number 2}
\treedef{3}{leaf1}{type 1}{long comment multiline line number 3}
\treedef{3}{leaf2}{type 1}{long comment multiline line number 4}
\treedef{2}{node3}{type 0}{very very very very long comment multiline line number 5}
\treedef{3}{leaf3}{type 2}{long comment multiline line number 6}
\treedef{3}{leaf4}{}{no type specified}
}
\end{document}
答案1
\documentclass[8pt]{scrartcl}
\usepackage{dirtree}
\usepackage{floatrow}
\usepackage{blindtext}
\usepackage{etoolbox}
% #1: Level
% #2: Name
% #3: Type
% #4: Comment
\newcommand{\treedef}[4]{%
\appto\dirtreecode{% ← must have no space at the beginning
.#1 #2 \hfill \begin{minipage}[t]{1cm}
#3
\end{minipage} \hfill \begin{minipage}[t]{3.5cm}
#4
\end{minipage}. % ← must have a space here
}%
}
\thisfloatsetup{%
objectset=raggedright,
}
\begin{document}
\def\dirtreecode{}
\treedef{1}{node1}{}{long comment multiline line number 1}
\treedef{2}{node2}{type 0}{long comment multiline line number 2}
\treedef{3}{leaf1}{type 1}{long comment multiline line number 3}
\treedef{3}{leaf2}{type 1}{long comment multiline line number 4}
\treedef{2}{node3}{type 0}{very very very very long comment multiline line number 5}
\treedef{3}{leaf3}{type 2}{long comment multiline line number 6}
\treedef{3}{leaf4}{}{no type specified}
%\show\dirtreecode % execute this if you want to see what is going to be put inside \dirtree{...}
\expandafter\dirtree\expandafter{\dirtreecode}
\end{document}
我忽略了“水平盒子未满”错误消息,因为它们不是这里的主要问题。
基本上,代码使用 构建整个“主体” \appto
,然后将其传递给\dirtree
使用\expandafter
。