为什么我的 tikz 图表不是并排的?

为什么我的 tikz 图表不是并排的?

使用\includegraphics和 PNG 图像,我能够并排对齐图形。但是,当我用 替换 时\includegraphicstikzpicture图形并不并排。相反,它们一个在另一个之上。有没有办法让 tikz 图形并排?

在此处输入图片描述

\begin{figure}
\begin{subfigure}{0.15\textwidth}
\begin{tikzpicture}[->,>=stealth',node distance=1cm,
  thick,main node/.style={circle,fill,inner sep=1.5pt}]
  \node[main node] (1) [label=above:{$Z$}]{};
  \node[main node] (2) [below left =0.5cm of 1,label=left:$X$]{};
  \node[main node] (3) [below right =0.5cm of 1,label=right:$Y$] {};
  \path[every node/.style={font=\sffamily\small}]
    (1) edge node {} (2)
    (1) edge node {} (3)
    (2) edge node {} (3);
\end{tikzpicture}
\end{subfigure}

\begin{subfigure}{0.15\textwidth}
\begin{tikzpicture}[->,>=stealth',node distance=1cm,
  thick,main node/.style={circle,fill,inner sep=1.5pt}]
  \node[main node] (1) [label=above:{$Z$}]{};
  \node[main node] (2) [below left =0.5cm of 1,label=left:$X$]{};
  \node[main node] (3) [below right =0.5cm of 1,label=right:$Y$] {};
  \path[every node/.style={font=\sffamily\small}]
    (1) edge node {} (3)
    (2) edge node {} (3);
\end{tikzpicture}
\end{subfigure}
\end{figure}

答案1

每个subfigure环境都变成一个框(a minipage)。框之间的空行开始一个新段落,因此它们将排版在彼此下方。

要将subfigures 并排放置,请将空行替换为水平空格,如\quad\qquad\hspace{...}\hfill,或者什么都不写。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\usepackage{subcaption}
\begin{document}
\quad
\begin{figure}
  \centering
\begin{subfigure}{0.2\textwidth}
\begin{tikzpicture}[->,>=stealth',node distance=1cm,
  thick,main node/.style={circle,fill,inner sep=1.5pt}]
  \node[main node] (1) [label=above:{$Z$}]{};
  \node[main node] (2) [below left =0.5cm of 1,label=left:$X$]{};
  \node[main node] (3) [below right =0.5cm of 1,label=right:$Y$] {};
  \path[every node/.style={font=\sffamily\small}]
    (1) edge node {} (2)
    (1) edge node {} (3)
    (2) edge node {} (3);
\end{tikzpicture}
\end{subfigure}
\quad
\begin{subfigure}{0.2\textwidth}
\begin{tikzpicture}[->,>=stealth',node distance=1cm,
  thick,main node/.style={circle,fill,inner sep=1.5pt}]
  \node[main node] (1) [label=above:{$Z$}]{};
  \node[main node] (2) [below left =0.5cm of 1,label=left:$X$]{};
  \node[main node] (3) [below right =0.5cm of 1,label=right:$Y$] {};
  \path[every node/.style={font=\sffamily\small}]
    (1) edge node {} (3)
    (2) edge node {} (3);
\end{tikzpicture}
\end{subfigure}
\end{figure}
\end{document}

相关内容