一个 Tikz 子图与其他子图不对齐

一个 Tikz 子图与其他子图不对齐

我创建了 4 张 TikZ 图片,并使用该subcaption包来获得 4 张图片的 2x2 图形。但是,右上角的图形与其他图形完全不对齐,我不明白为什么。

这是我的代码:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{makeidx}
\usepackage{graphicx}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{subcaption}

\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}

\begin{figure}

\begin{subfigure}[b]{0.35\textwidth}

    \centering
    \resizebox{\linewidth}{!}{

    \begin{tikzpicture}[scale=5]

    % Define useful points for colouring (colouring first)

    \coordinate (H1) at (0,0.9);
    \coordinate (H2) at (0.1,0.8);
    \coordinate (H3) at (0.8,0.1);
    \coordinate (H4) at (0.9,0);

    \coordinate (INTER) at (0.465,0.4);

    \coordinate (T1) at (0,0.4);
    \coordinate (T2) at (1,0.4);

    % Colouring (0,0)

    \fill[color=blue!10]
    (T1) -- (T2) -- (1,1) -- (0,1) -- cycle;

    % Box and labels

    \draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- (0,0);
    \draw (0,1) node[left]{$\theta$};
    \draw (1,0) node[below]{$s$};

    % h^(-1) curve

    \draw (0,0.9) to[bend left = 5] (0.1,0.8);
    \draw (0.1,0.8) to[bend right = 5] (0.8,0.1);
    \draw (0.8,0.1) to[bend left = 5] (0.9,0);

    \draw (0.13,0.6) node {$h^{-1}(\cdot)$};

    % \t hat line

    \draw[dashed] (0,0.4) -- (1,0.4);
    \draw (0,0.4) node[left] {$\hat{\theta}$};

    % s hat line

    \draw (0.465,1) -- (0.465,0);
    \draw (0.465,0) node[below] {$\hat{s}$};

    \end{tikzpicture}

    }
    \caption{$(x_{1},x_{2}) = (0,0)$}
    \label{fig:subfigA}

\end{subfigure}
\begin{subfigure}[b]{0.35\textwidth}

    \centering
    \resizebox{\linewidth}{!}{

    \begin{tikzpicture}[scale=5]

    % Define useful points for colouring

    \coordinate (H1) at (0,0.9);
    \coordinate (H2) at (0.1,0.8);
    \coordinate (AxH2) at (0.1,1);
    \coordinate (H3) at (0.8,0.1);
    \coordinate (H4) at (0.9,0);

    \coordinate (INTER) at (0.465,0.4);
    \coordinate (AxINTER) at (0.465,1);

    \coordinate (T1) at (0,0.4);
    \coordinate (T2) at (1,0.4);

    % Colouring (1,0)

    \fill[color=blue!10]
    (0,0.9) to[bend left = 5] (0.1,0.8) -- (AxH2) -- (0,1) -- cycle;

    \fill[color=blue!10]
    (0.1,0.8) to[bend right = 5] (0.8,0.1) -- (T2) -- (1,1) -- (AxH2) -- cycle;

    \fill[color=white]
    (INTER) to[bend right = 5] (0.8,0.1) -- (T2) -- cycle;

    % Box and labels

    \draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- (0,0);
    \draw (0,1) node[left]{$\theta$};
    \draw (1,0) node[below]{$s$};

    % h^(-1) curve

    \draw (0,0.9) to[bend left = 5] (0.1,0.8);
    \draw (0.1,0.8) to[bend right = 5] (0.8,0.1);
    \draw (0.8,0.1) to[bend left = 5] (0.9,0);

    \draw (0.13,0.6) node {$h^{-1}(\cdot)$};

    % \t hat line

    \draw[dashed] (0,0.4) -- (1,0.4);
    \draw (0,0.4) node[left] {$\hat{\theta}$};

    % s hat line

    \draw (0.465,1) -- (0.465,0);
    \draw (0.465,0) node[below] {$\hat{s}$};

    \end{tikzpicture}

    }
    \caption{$(x_{1},x_{2}) = (1,0)$}
    \label{fig:subfigB}

\end{subfigure}
\begin{subfigure}[b]{0.35\textwidth}

    \centering
    \resizebox{\linewidth}{!}{

    \begin{tikzpicture}[scale=5]

    % Define useful points for colouring

    \coordinate (H1) at (0,0.9);
    \coordinate (H2) at (0.1,0.8);
    \coordinate (AxH2) at (0.1,1);
    \coordinate (H3) at (0.8,0.1);
    \coordinate (AxH3) at (0.8,1);
    \coordinate (H4) at (0.9,0);

    \coordinate (INTER) at (0.465,0.4);
    \coordinate (AxINTER) at (0.465,1);

    \coordinate (T1) at (0,0.4);
    \coordinate (T2) at (1,0.4);

    % Colouring (0,1)

    \fill[color=blue!10]
    (H2) to[bend right = 5] (H3) -- (AxH3) -- (AxH2) -- cycle;

    \fill[color=blue!10]
    (H3) to[bend left = 5] (H4) -- (1,1) -- (AxH3) -- cycle;

    \fill[color=blue!10]
    (H4) to[bend left = 5] (H4) -- (1,1) -- (AxH3) -- cycle;

    \fill[color=blue!10]
    (H4) -- (1,0) -- (1,1) -- cycle;

    \fill[color=blue!10]
    (0,1) -- (0,0.4) -- (1,0.4) -- (1,1) -- cycle;

    % Box and labels

    \draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- (0,0);
    \draw (0,1) node[left]{$\theta$};
    \draw (1,0) node[below]{$s$};

    % h^(-1) curve

    \draw (0,0.9) to[bend left = 5] (0.1,0.8);
    \draw (0.1,0.8) to[bend right = 5] (0.8,0.1);
    \draw (0.8,0.1) to[bend left = 5] (0.9,0);

    \draw (0.13,0.6) node {$h^{-1}(\cdot)$};

    % \t hat line

    \draw[dashed] (0,0.4) -- (1,0.4);
    \draw (0,0.4) node[left] {$\hat{\theta}$};

    % s hat line

    \draw (0.465,1) -- (0.465,0);
    \draw (0.465,0) node[below] {$\hat{s}$};

    \end{tikzpicture}

    }
    \caption{$(x_{1},x_{2}) = (0,1)$}
    \label{fig:subfigC}

\end{subfigure}
\begin{subfigure}[b]{0.35\textwidth}

    \centering
    \resizebox{\linewidth}{!}{

    \begin{tikzpicture}[scale=5]

    % Define useful points for colouring

    \coordinate (H1) at (0,0.9);
    \coordinate (H2) at (0.1,0.8);
    \coordinate (AxH2) at (0.1,1);
    \coordinate (H3) at (0.8,0.1);
    \coordinate (AxH3) at (0.8,1);
    \coordinate (H4) at (0.9,0);

    \coordinate (INTER) at (0.465,0.4);
    \coordinate (AxINTER) at (0.465,1);

    \coordinate (T1) at (0,0.4);
    \coordinate (T2) at (1,0.4);

    % Colouring (1,1)

    \fill[color=blue!10]
    (0,0.9) to[bend left = 5] (0.1,0.8) -- (AxH2) -- (0,1) -- cycle;

    \fill[color=blue!10]
    (H2) to[bend right = 5] (H3) -- (AxH3) -- (AxH2) -- cycle;

    \fill[color=blue!10]
    (H3) to[bend left = 5] (H4) -- (1,1) -- (AxH3) -- cycle;

    \fill[color=blue!10]
    (H4) to[bend left = 5] (H4) -- (1,1) -- (AxH3) -- cycle;

    \fill[color=blue!10]
    (H4) -- (1,0) -- (1,1) -- cycle;


    % Box and labels

    \draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- (0,0);
    \draw (0,1) node[left]{$\theta$};
    \draw (1,0) node[below]{$s$};

    % h^(-1) curve

    \draw (0,0.9) to[bend left = 5] (0.1,0.8);
    \draw (0.1,0.8) to[bend right = 5] (0.8,0.1);
    \draw (0.8,0.1) to[bend left = 5] (0.9,0);

    \draw (0.13,0.6) node {$h^{-1}(\cdot)$};

    % \t hat line

    \draw[dashed] (0,0.4) -- (1,0.4);
    \draw (0,0.4) node[left] {$\hat{\theta}$};

    % s hat line

    \draw (0.465,1) -- (0.465,0);
    \draw (0.465,0) node[below] {$\hat{s}$};

    \end{tikzpicture}

    }
    \caption{$(x_{1},x_{2}) = (1,1)$}
    \label{fig:subfigD}

\end{subfigure}

\label{fig:EqLemma}

\end{figure}

\end{document}

编译的结果是: 编译

我检查了许多相关问题,但没有发现任何类似的问题(但我首先犯了一个经典错误,在子图环境之间放置了一个空行......)。

答案1

为了解决这个问题,你应该做两件事:

  1. \\在第二个和第三个子图之间添加换行符。
  2. 然后(因为这会改变整个图形的水平对齐方式),\centering在下方添加\begin{figure}

您的代码中的内容如下:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering % <-- add this

\begin{subfigure}[b]{0.35\textwidth}
  % your figure here
\end{subfigure}
\begin{subfigure}[b]{0.35\textwidth}
  % your figure here
\end{subfigure}
\\ % <-- add this
\begin{subfigure}[b]{0.35\textwidth}
  % your figure here
\end{subfigure}
\begin{subfigure}[b]{0.35\textwidth}
  % your figure here
\end{subfigure}
\end{figure}
\end{document}

在此处输入图片描述

该错误与 TikZ 无关,如果您使用示例图像代替 TikZ 图片,仍会发生该错误。

相关内容