# Tikz subfigures alignment

I created 5 figures and used subcaption package to align them. My hope is to put three figures in the above line, and the remaining two is the second line in the center.

But the current one is, one graph by one line as shown in the following image.

The code is:

\begin{figure}[t]
\centering
\begin{subfigure}[b]{0.32\textwidth}
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]

\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[above] {$-1$} (s2);

\end{tikzpicture}
\caption{$G_{c_1}$.}
\end{subfigure}

\begin{subfigure}[b]{0.32\textwidth}
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[below left] {$-1$} (s4)
(s2) edge[->] node[right] {$-1$} (s4);

\end{tikzpicture}
\caption{$G_{c_2}$.}
\end{subfigure}

\begin{subfigure}[b]{0.32\textwidth}
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[below left] {$-1$} (s4)
(s3) edge[->] node[below] {$-1$} (s4);
\end{tikzpicture}
\caption{$G_{c_3}$.}
\end{subfigure}
\\
\begin{subfigure}[b]{0.32\textwidth}
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\end{tikzpicture}
\caption{$G_{c_4}$.}
\end{subfigure}

\begin{subfigure}[b]{0.32\textwidth}
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[above] {$-1$} (s2)
(s1) edge[->] node[below left] {$-1$} (s4)
(s2) edge[->] node[right] {$-1$} (s4);

\end{tikzpicture}
\caption{$G_{C}$.}
\end{subfigure}

\caption{Example of compatible preferences.}
\label{fig:efficiency}
\end{figure}


Could you solve this problem? I saw these problems about images not about tikz.

The example full code is although it's impossible to give my full code because it's based on a conference style.

\documentclass[twocolumn, letterpaper]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,shapes,calc,decorations.shapes, decorations.pathreplacing}
\usepackage{caption,subcaption}

\begin{document}

\begin{figure}[t]
\centering
\begin{subfigure}[b]{0.32\textwidth}
\centering
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]

\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[above] {$-1$} (s2);

\end{tikzpicture}
\caption{$G_{c_1}$.}
\end{subfigure}
\begin{subfigure}[b]{0.32\textwidth}
\centering
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[below left] {$-1$} (s4)
(s2) edge[->] node[right] {$-1$} (s4);

\end{tikzpicture}
\caption{$G_{c_2}$.}
\end{subfigure}
\begin{subfigure}[b]{0.32\textwidth}
\centering
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[below left] {$-1$} (s4)
(s3) edge[->] node[below] {$-1$} (s4);
\end{tikzpicture}
\caption{$G_{c_3}$.}
\end{subfigure}
\\
\begin{subfigure}[b]{0.32\textwidth}
\centering
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\end{tikzpicture}
\caption{$G_{c_4}$.}
\end{subfigure}
\begin{subfigure}[b]{0.32\textwidth}
\centering
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[above] {$-1$} (s2)
(s1) edge[->] node[below left] {$-1$} (s4)
(s2) edge[->] node[right] {$-1$} (s4);

\end{tikzpicture}
\caption{$G_{C}$.}
\end{subfigure}

\caption{Example of compatible preferences.}
\label{fig:efficiency}
\end{figure}

\end{document}

• Just remove the empty lines between the subfigures. – Schrödinger's cat Sep 4 '19 at 5:27
• Oh, really? The problem still occurs if we delete empty lines between subfugures. – Sato Sep 4 '19 at 5:33

I will be happy to remove this. Use \linewidth instead of \textwidth.

\documentclass[twocolumn, letterpaper]{article}
\usepackage{tikz}
\usepackage{caption,subcaption}

\begin{document}

\begin{figure}[t]
\centering
\begin{subfigure}[b]{0.32\linewidth}
\centering
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]

\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[above] {$-1$} (s2);

\end{tikzpicture}
\caption{$G_{c_1}$.}
\end{subfigure}
\begin{subfigure}[b]{0.32\linewidth}
\centering
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[below left] {$-1$} (s4)
(s2) edge[->] node[right] {$-1$} (s4);

\end{tikzpicture}
\caption{$G_{c_2}$.}
\end{subfigure}
\begin{subfigure}[b]{0.32\linewidth}
\centering
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[below left] {$-1$} (s4)
(s3) edge[->] node[below] {$-1$} (s4);
\end{tikzpicture}
\caption{$G_{c_3}$.}
\end{subfigure}
\\
\begin{subfigure}[b]{0.32\linewidth}
\centering
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\end{tikzpicture}
\caption{$G_{c_4}$.}
\end{subfigure}
\begin{subfigure}[b]{0.32\linewidth}
\centering
\begin{tikzpicture}[scale=0.9, every node/.style={scale=0.9}]
\node[draw, shape = circle] at (0, 0) (s1) {$s_1$};
\node[draw, shape = circle] at (1.5, 0) (s2) {$s_2$};
\node[draw, shape = circle] at (0, -1.5) (s3) {$s_3$};
\node[draw, shape = circle] at (1.5, -1.5) (s4) {$s_4$};

\path[draw, thick]
(s1) edge[->] node[above] {$-1$} (s2)
(s1) edge[->] node[below left] {$-1$} (s4)
(s2) edge[->] node[right] {$-1$} (s4);

\end{tikzpicture}
\caption{$G_{C}$.}
\end{subfigure}

\caption{Example of compatible preferences.}
\label{fig:efficiency}
\end{figure}

\end{document}


• Thanks a lot. However, the problem is not solved while I succeeded by your code in the plain file. I guess the problem may be from two-column by a conference style because the space of width is small. How can I do for this? Changing the scale did not work. – Sato Sep 4 '19 at 5:53
• @Sato Provide a complete example starting with \documentclass and ending with \end{document}. – Schrödinger's cat Sep 4 '19 at 5:55
• Added an example. – Sato Sep 4 '19 at 6:03
• @Sato I added a solution. – Schrödinger's cat Sep 4 '19 at 6:08
• Perfect! Thank you very much! – Sato Sep 4 '19 at 6:17

A off-topic supplement to answer of @Schrödinger's cat with some suggestion how to make code more clear and shorter. Main changes are marked with comments in code:

\documentclass[twocolumn, letterpaper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning, % <--- new
quotes}      % <--- new
\usepackage{caption, subcaption}

\usepackage{lipsum}
\begin{document}
\lipsum[66]
\begin{figure}[ht]
\tikzset{% <--- common settings of styles for all pictures in figure
node distance = 11mm and 11mm, % used is relative positioning of nodes
C/.style = {circle, draw, inner sep=2pt},
every edge/.style = {draw, thick, ->},
every edge quotes/.style = {font=\scriptsize, inner sep=1pt, auto}
}
\newcommand\preference{% <--- here is defined common part of all pictures
\node (s1) [C]              {$s_1$};
\node (s2) [C,right=of s1]  {$s_2$};
\node (s3) [C,below=of s1]  {$s_3$};
\node (s4) [C,below=of s2]  {$s_4$};
}
\centering
\begin{subfigure}{0.32\linewidth}
\centering
\begin{tikzpicture}
\preference
\draw   (s1) edge["$-1$"]   (s2);
\end{tikzpicture}
\caption{$G_{c_1}$.}
\end{subfigure}
\hfill
\begin{subfigure}{0.32\linewidth}
\centering
\begin{tikzpicture}
\preference
\draw   (s1) edge["$-1$"]   (s4)
(s2) edge["$-1$"]   (s4);
\end{tikzpicture}
\caption{$G_{c_2}$.}
\end{subfigure}
\hfill
\begin{subfigure}{0.32\linewidth}
\centering
\begin{tikzpicture}
\preference
\draw   (s1) edge["$-1$"]   (s4)
(s3) edge["$-1$"]   (s4);
\end{tikzpicture}
\caption{$G_{c_3}$.}
\end{subfigure}

% empty line start new row with pictures
\bigskip % more vertical space between rows of pictures
\begin{subfigure}{0.32\linewidth}
\centering
\begin{tikzpicture}
\preference
\end{tikzpicture}
\caption{$G_{c_4}$.}
\end{subfigure}
\hfil
\begin{subfigure}{0.32\linewidth}
\centering
\begin{tikzpicture}
\preference
\draw   (s1) edge["$-1$"]   (s2)
(s1) edge["$-1$"]   (s4)
(s3) edge["$-1$"]   (s4);
\end{tikzpicture}
\caption{$G_{C}$.}
\end{subfigure}
\caption{Example of compatible preferences.}
\label{fig:efficiency}
\end{figure}
\lipsum[1-4]
\end{document}