代码

代码

我想画一些树tikz并像这样排列它们(模型):

小样

请注意,枚举与顶部对齐。

理想情况下,这应该“正常工作”,根据需要断线,并自动调整所有这些图形的宽度。

到目前为止我最好的看法是:

截屏

数字不一致,我无法用论点说服他们这样做[t]。此外,我必须手动指定每个宽度:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees}
\usepackage{tikz-qtree}

\newenvironment{tikztree}
{
    \tikzpicture[sibling distance=1cm, level distance=1cm, baseline]
    \tikzstyle{every node}=[draw, circle, fill=black, inner sep=0pt, minimum size=2mm]
}{
    \endtikzpicture
}

\begin{document}

    \begin{enumerate}
        \begin{minipage}{2cm}\item \begin{tikztree}[baseline]\Tree[.{} ];\end{tikztree}\end{minipage}
        \begin{minipage}{2cm}\item \begin{tikztree}[baseline]\Tree[.{} [.{} ] ];\end{tikztree}\end{minipage}
        \begin{minipage}{3cm}\item \begin{tikztree}[baseline]\Tree[.{} [.{} ] [.{} ] ];\end{tikztree}\end{minipage}
        \vspace{5ex}

        \begin{minipage}{4cm}\item \begin{tikztree}[baseline]\Tree[.{} [.{} [.{} ] [.{} ] ] [.{} ] ];\end{tikztree}\end{minipage}
        \begin{minipage}{2cm}\item \begin{tikztree}[baseline]\Tree[.{} [.{} [.{} ] ] ];\end{tikztree}\end{minipage}
        \begin{minipage}{3cm}\item \begin{tikztree}[baseline]\Tree[.{} [.{} [.{} ] [.{} ] ] ];\end{tikztree}\end{minipage}
    \end{enumerate}

\end{document}

实现这一目标的最佳方法是什么?

答案1

您可以对 s 使用相同的方法minipage。为所有 s 赋予相同(合适)的宽度minipage并使用\hfills。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees}
\usepackage{tikz-qtree}

\newenvironment{tikztree}
{
    \tikzpicture[sibling distance=1cm, level distance=1cm, baseline]
    \tikzstyle{every node}=[draw, circle, fill=black, inner sep=0pt, minimum size=2mm]
}{
    \endtikzpicture
}

\begin{document}

    \begin{enumerate}
        \begin{minipage}[t]{3cm}\item \begin{tikztree}[baseline]\Tree[.{} ];\end{tikztree}\end{minipage}\hfill
        \begin{minipage}[t]{3cm}\item \begin{tikztree}[baseline]\Tree[.{} [.{} ] ];\end{tikztree}\end{minipage}\hfill
        \begin{minipage}[t]{3cm}\item \begin{tikztree}[baseline]\Tree[.{} [.{} ] [.{} ] ];\end{tikztree}\end{minipage}
        \vspace{5ex}

        \begin{minipage}[t]{3cm}\item \begin{tikztree}[baseline]\Tree[.{} [.{} [.{} ] [.{} ] ] [.{} ] ];\end{tikztree}\end{minipage}\hfill
        \begin{minipage}[t]{3cm}\item \begin{tikztree}[baseline]\Tree[.{} [.{} [.{} ] ] ];\end{tikztree}\end{minipage}\hfill
        \begin{minipage}[t]{3cm}\item \begin{tikztree}[baseline]\Tree[.{} [.{} [.{} ] [.{} ] ] ];\end{tikztree}\end{minipage}
    \end{enumerate}

\end{document}

在此处输入图片描述

回复评论:

我们需要[t]说明符,以便使所有树都对齐在顶部,如果没有说明符,树将对齐在中心(垂直)。此外,\hfill将确保item水平方向均匀分布,从而对齐。

答案2

这是使用该multienumerate软件包的快速解决方案。它不是最强大的软件包,但可以解决问题。等待其他解决方案并进行比较。

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees}
\usepackage{tikz-qtree}

\newenvironment{tikztree}
{
    \tikzpicture[sibling distance=1cm, level distance=1cm, baseline]
    \tikzstyle{every node}=[draw, circle, fill=black, inner sep=0pt, minimum size=2mm]
}{
    \endtikzpicture
}
\usepackage{multienum}

\begin{document}

\begin{multienumerate}
    \mitemxxx{%
        \begin{tikztree}[baseline]
        \Tree[.{} ];
        \end{tikztree}
        }{%
        \begin{tikztree}[baseline]
        \Tree[.{} [.{} ] ];
        \end{tikztree}
        }{%
        \begin{tikztree}[baseline]
        \Tree[.{} [.{} ] [.{} ] ];
        \end{tikztree}
        }
    \vskip\baselineskip
    \mitemxxx{%
        \begin{tikztree}[baseline]
        \Tree[.{} [.{} [.{} ] [.{} ] ] [.{} ] ];
        \end{tikztree}
        }{%
        \begin{tikztree}[baseline]
        \Tree[.{} [.{} [.{} ] ] ];
        \end{tikztree}
        }{%
        \begin{tikztree}[baseline]
        \Tree[.{} [.{} [.{} ] [.{} ] ] ];
        \end{tikztree}
        }
\end{multienumerate}
\end{document}

答案3

包提供的内联列表enumitem也是可能的。

代码

\documentclass{article}
\usepackage{tikz-qtree}
\usepackage[inline]{enumitem}

\newenvironment{tikztree}
{
    \tikzpicture[sibling distance=1cm, level distance=1cm, baseline]
    \tikzset{every node/.style={draw, circle, fill=black, inner sep=0pt, minimum size=2mm}}
}{
    \endtikzpicture
}

\begin{document}
All in one line with automatic line break:

\noindent
\begin{enumerate*}[itemjoin=\hfill]
\item \begin{tikztree}\Tree[.{} ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} ] [.{} ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} [.{} ] [.{} ] ] [.{} ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} [.{} ] ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} [.{} ] [.{} ] ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} ] [.{} ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} [.{} ] [.{} ] ] [.{} ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} [.{} ] ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} [.{} ] [.{} ] ] ];\end{tikztree}
\end{enumerate*}

\vskip30pt
Break into two lines:

\noindent
\begin{enumerate*}[itemjoin=\hfill]
\item \begin{tikztree}\Tree[.{} ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} ] [.{} ] ];\end{tikztree}
\end{enumerate*}
\\ \bigskip
\begin{enumerate*}[itemjoin=\hfill,resume]
\item \begin{tikztree}\Tree[.{} [.{} [.{} ] [.{} ] ] [.{} ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} [.{} ] ] ];\end{tikztree}
\item \begin{tikztree}\Tree[.{} [.{} [.{} ] [.{} ] ] ];\end{tikztree}
\end{enumerate*}

\end{document}

输出

在此处输入图片描述

答案4

这个tasks包(它是exsheets包的一部分)是我在类似情况下使用的。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees}
\usepackage{tikz-qtree}
\usepackage{tasks}

\newenvironment{tikztree}
{
\tikzpicture[sibling distance=1cm, level distance=1cm, baseline]
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=0pt, minimum size=2mm]
}{
\endtikzpicture
}

\begin{document}

\begin{tasks}[after-item-skip = 5ex, counter-format = tsk. ](3)
    \task \begin{tikztree}\Tree[.{} ];\end{tikztree}
    \task \begin{tikztree}\Tree[.{} [.{} ] ];\end{tikztree}
    \task \begin{tikztree}\Tree[.{} [.{} ] [.{} ] ];\end{tikztree}
    \task \begin{tikztree}\Tree[.{} [.{} [.{} ] [.{} ] ] [.{} ] ];\end{tikztree}
    \task \begin{tikztree}\Tree[.{} [.{} [.{} ] ] ];\end{tikztree}
    \task \begin{tikztree}\Tree[.{} [.{} [.{} ] [.{} ] ] ];\end{tikztree}
\end{tasks}

\end{document}

任务环境中的树

(此外,由于您输入baseline了环境定义,因此不需要为每棵树都定义它。)

相关内容