Tikz 子图对齐

Tikz 子图对齐

我创建了 5 个图形,并使用 subcaption 包将它们对齐。我希望将三个图形放在上面一行,其余两个放在中间的第二行。

在此处输入图片描述

但是目前的情况是,一行一行的绘制,如下图所示。 在此处输入图片描述

代码如下:

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

你能解决这个问题吗?我看到这些问题与图像有关,而不是与 tikz 有关。

示例完整代码是,尽管由于它基于会议风格,所以我不可能提供完整的代码。

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

答案1

我很乐意删除它。使用\linewidth而不是\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}

在此处输入图片描述

答案2

对 @Schrödinger's cat 的回答的一个题外补充,其中有一些关于如何使代码更清晰、更简短的建议。主要更改已在代码中用注释标记:

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

在此处输入图片描述

相关内容