3

With \includegraphics and PNG images I'm able to align graphics side by side. However, when I replace \includegraphics with a tikzpicture, the graphs are not side by side. Instead, they are one on top of the other. Is there a way to make tikz graphs side by side?

enter image description here

\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}
2
  • 4
    Have you tried to remove the empty line between the sub-figures? – gernot Jan 31 at 20:53
  • omigosh @gernot! that was it, thank you! Please post as an answer. – at. Jan 31 at 20:54
2

Each subfigure environment becomes a box (a minipage). The empty line between the boxes starts a new paragraph, so they will be typeset below each other.

To put the subfigures side by side, replace the empty line by a horizontal space like \quad, \qquad, \hspace{...}, \hfill, or nothing at all.

enter image description here

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.