答案1
我可以用 TikZ 做到这一点。使用fresh nodes
允许重复标签,并使用空标签和边缘作为填充。我认为相当简洁。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}
\graph[fresh nodes]{
A -> B -> C -> D -> E;
{""} -!- -> B -> C -> D -> E;
{""} -!- {""} -!- C -> D -> E;
{""} -!- B -> C -> D -> E;
{""} -!- {""} -!- {""} -!- D -> E;
};
\end{tikzpicture}
\end{document}
答案2
行表示为
A & B & C & D & E \\
您可以执行以下操作:
\documentclass{article}
\usepackage{amsmath,array}
\usepackage{lipsum}
\ExplSyntaxOn
\NewDocumentEnvironment{rdiagram}{b}
{
\gerlach_rdiagram:n { #1 }
}
{}
\seq_new:N \l__gerlach_rdiagram_rows_seq
\seq_new:N \l__gerlach_rdiagram_row_seq
\int_new:N \l__gerlach_rdiagram_cols_int
\tl_new:N \l__gerlach_rdiagram_body_tl
\cs_new_protected:Nn \gerlach_rdiagram:n
{
% reset the counter
\int_zero:N \l__gerlach_rdiagram_cols_int
% split into rows
\seq_set_split:Nnn \l__gerlach_rdiagram_rows_seq { \\ } { #1 }
% check for a trailing \\
\seq_pop_right:NN \l__gerlach_rdiagram_rows_seq \l_tmpa_tl
\tl_if_blank:VF \l_tmpa_tl
{
\seq_put_right:NV \l__gerlach_rdiagram_rows_seq \l_tmpa_tl
}
% count the number of cells
\seq_map_inline:Nn \l__gerlach_rdiagram_rows_seq
{
\seq_set_split:Nnn \l__gerlach_rdiagram_row_seq { & } { ##1 }
\int_compare:nT { \seq_count:N \l__gerlach_rdiagram_row_seq > \l__gerlach_rdiagram_cols_int }
{
\int_set:Nn \l__gerlach_rdiagram_cols_int { \seq_count:N \l__gerlach_rdiagram_row_seq }
}
}
\renewcommand{\arraystretch}{1.2}
\setlength{\arraycolsep}{0pt}
\tl_clear:N \l__gerlach_rdiagram_body_tl
\seq_map_function:NN \l__gerlach_rdiagram_rows_seq \__gerlach_rdiagram_row:n
\begin{array}{ c *{\int_eval:n { \l__gerlach_rdiagram_cols_int - 1 }}{>{{}}c<{{}}c} }
\tl_use:N \l__gerlach_rdiagram_body_tl
\end{array}
}
\cs_new_protected:Nn \__gerlach_rdiagram_row:n
{
\seq_set_split:Nnn \l__gerlach_rdiagram_row_seq { & } { #1 }
\tl_put_right:Nx \l__gerlach_rdiagram_body_tl
{
\prg_replicate:nn
{ \l__gerlach_rdiagram_cols_int - \seq_count:N \l__gerlach_rdiagram_row_seq }
{ && }
\seq_use:Nn \l__gerlach_rdiagram_row_seq { & \rightarrow & }
\exp_not:N \\
}
}
\ExplSyntaxOff
\begin{document}
\lipsum[1][1-5]
\[
\begin{rdiagram}
A & B & C & D & E \\
B & C & D & E \\
B & C & D & E \\
C & D & E \\
B & C & D & E \\
B & C & D & E \\
D & E
\end{rdiagram}
\]
\lipsum[2][1-5]
\end{document}
其理念是将正文拆分成行,然后对每行计算&
分隔项的数量。这用于设置 中的列数array
,然后使用它来添加适当数量的&&
;&
则替换为&\rightarrow&
。