同一行中的树状图

同一行中的树状图

我试图将树形图放在同一行,但它们却位于不同的行。

\documentclass[12pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{mathtools}
\let\cleardoublepage=\clearpage
\usepackage{tikz}
\tikzset{
treenode/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=blue!20},
root/.style     = {treenode, font=\Large, bottom color=red!30},
env/.style      = {treenode, font=\ttfamily\normalsize},
dummy/.style    = {circle,draw}
}
\begin{document}
\begin{tikzpicture}
[
grow                    = right,
sibling distance        = 6em,
level distance          = 8em,
edge from parent/.style = {draw, -latex},
every node/.style       = {font=\footnotesize},
sloped
]
\node [root] {$L_1$}
child { node [env] {z}
    edge from parent node [below] {1-p} }
child { node [env] {x}
    edge from parent node [above] {p} };

\end{tikzpicture}

$\sim$

\begin{tikzpicture}
[
grow                    = right,
sibling distance        = 6em,
level distance          = 8em,
edge from parent/.style = {draw, -latex},
every node/.style       = {font=\footnotesize},
sloped
]
\node [root] {$L_2$}
child { node [env] {z}
    edge from parent node [below] {1-p} }
child { node [env] {y}
    edge from parent node [above] {p} };
\end{tikzpicture}
\end{document}

答案1

您必须使用注释来抑制图片之间的换行符:

\documentclass[12pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{mathtools}
\let\cleardoublepage=\clearpage
\usepackage{tikz}
\tikzset{
treenode/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=blue!20},
root/.style     = {treenode, font=\Large, bottom color=red!30},
env/.style      = {treenode, font=\ttfamily\normalsize},
dummy/.style    = {circle,draw}
}
\begin{document}
\begin{tikzpicture}
[
grow                    = right,
sibling distance        = 6em,
level distance          = 8em,
edge from parent/.style = {draw, -latex},
every node/.style       = {font=\footnotesize},
sloped
]
\node [root] {$L_1$}
child { node [env] {z}
    edge from parent node [below] {1-p} }
child { node [env] {x}
    edge from parent node [above] {p} };

\end{tikzpicture}%
%
$\sim$%
%
\begin{tikzpicture}
[
grow                    = right,
sibling distance        = 6em,
level distance          = 8em,
edge from parent/.style = {draw, -latex},
every node/.style       = {font=\footnotesize},
sloped
]
\node [root] {$L_2$}
child { node [env] {z}
    edge from parent node [below] {1-p} }
child { node [env] {y}
    edge from parent node [above] {p} };
\end{tikzpicture}
\end{document}

这产生了 生成的图像 但请注意, 不在~图片中间。可以使用\raisebox或类似的东西将其移到那里...


解释

每次你输入一个空行,TeX 都会将其视为当前段落的分隔符,从而导致换行。这就是为什么在你的例子中,图像位于彼此之下。
注释会阻止 TeX 看到当前行中的所有后续字符。请执行避免出现任何空白(空格、制表符、换行符)开始下一行的行首,因此可用于“左修剪”下一行的内容。
在给定的示例中,放置的注释会删除图像之间的任何空格,这样 TeX 就不会产生换行符。您可以通过手动删除空格来获得相同的结果(例如,\end{tikzpicture}\begin{tikzpicture}在中间没有任何空格的情况下书写)。

相关内容