Tikz 子图重叠

Tikz 子图重叠

我尝试在互联网上搜索,但没有找到。

我在 TikZ 中制作了 2 个图形,我想通过包将它们并排放置subcaption

这是一个最小的工作示例:

\documentclass[letterpaper, 10 pt, conference,tikz]{memoir}  % Comment this line out 
\usepackage{subcaption}
%TikZ 
\usepackage{tikz}
% Begin document
\begin{document}    
\begin{figure}
    \begin{subfigure}{0.2\textwidth}
    \begin{tikzpicture}[scale=1]
    %\draw[dashed](0,0)--(2,2);
    %\draw (0,0) grid (10,10);
    %%%%%%%%%%%%%
    %% NODES
    %\draw [red,line width=1,fill=black] (0,1) circle [radius=0.3];
    \draw [line width=1,fill=black] (0,1) circle [radius=0.3];
    \node [white] at (0,1) {$\mathbf{1}$};
    \draw [line width=1,fill=black] (2,1) circle [radius=0.3];
    \node [white] at (2,1) {$\mathbf{2}$};
    \draw [line width=1,fill=black] (2,3) circle [radius=0.3];
    \node [white] at (2,3) {$\mathbf{3}$};
    \draw [line width=1,fill=black] (5,1) circle [radius=0.3];
    \node [white] at (5,1) {$\mathbf{4}$};
    \draw [line width=1,fill=black] (5,3) circle [radius=0.3];
    \node [white] at (5,3) {$\mathbf{5}$};
    %\draw [line width =1.3,->] (0.12,1.28) to (0.82,2.74);
    %\draw [line width =1.3,<->] (0.3,1) to (1.7,1);
    %%%%%%%%%%%%%
    %% EDGES
    \draw [line width =1.3] (0.3,1) to (1.7,1);
    \draw [line width =1.3] (2,2.8) to (2,1.2);
    \draw [line width =1.3] (2.2,1) to (4.8,1);
    \draw [line width =1.3] (5,1.2) to (5,2.8);
    \draw [line width =1.3] (2.2,3) to (4.8,3);
    \end{tikzpicture} 
           \caption{Subfigure A}
            \label{fig:subfig8}
        \end{subfigure}  
    \begin{subfigure}{0.32\textwidth}
    \begin{tikzpicture}[scale=1]
    %\draw[dashed](0,0)--(2,2);
    %\draw (0,0) grid (10,10);
    %%%%%%%%%%%%%
    %% NODES
    %\draw [red,line width=1,fill=black] (0,1) circle [radius=0.3];
    \draw [line width=1,fill=black] (0,1) circle [radius=0.3];
    \node [white] at (0,1) {$\mathbf{1}$};
    \draw [line width=1,fill=black] (2,1) circle [radius=0.3];
    \node [white] at (2,1) {$\mathbf{2}$};
    \draw [line width=1,fill=black] (2,3) circle [radius=0.3];
    \node [white] at (2,3) {$\mathbf{3}$};
    \draw [line width=1,fill=black] (5,1) circle [radius=0.3];
    \node [white] at (5,1) {$\mathbf{4}$};
    \draw [line width=1,fill=black] (5,3) circle [radius=0.3];
    \node [white] at (5,3) {$\mathbf{5}$};
    %\draw [line width =1.3,->] (0.12,1.28) to (0.82,2.74);
    %\draw [line width =1.3,<->] (0.3,1) to (1.7,1);
    %%%%%%%%%%%%%
    %% EDGES
    %\draw [line width =1.3] (0.3,1) to (1.7,1);
    \draw [line width =1.3] (2,2.8) to (2,1.2);
    \draw [line width =1.3] (2.2,1) to (4.8,1);
    \draw [line width =1.3] (5,1.2) to (5,2.8);
    \draw [line width =1.3] (2.2,3) to (4.8,3);
    \end{tikzpicture}   
            \caption{Subfigure B}
            \label{fig:subfig10}
        \end{subfigure}
    \caption{ciao} 
    \label{fig:subfig1.a.4}
    \end{figure}   
\end{document} 

但它们有重叠。你能帮助我吗?

多谢。

答案1

在此处输入图片描述

正如我在评论中所说,子图形将\hfill在它们之间推开(最大)。更好的方法是使用:

\begin{figure}{...}
\centering    % <--- added
\begin{subfigure}
...
\end{subfigure}{...}
\hfil       % <--- added
\begin{subfigure}
...
\end{subfigure}
...
\end{figure}

完成 mwe (我擅自重写了你的图像代码):

\documentclass[letterpaper, 10 pt, conference]{memoir} 
\usepackage{subcaption}
%TikZ
\usepackage{tikz}
\usetikzlibrary{positioning}

% Begin document
\begin{document}
\begin{figure}
\centering  % <--- for centering sub figures on page
    \begin{subfigure}{0.32\linewidth}
    \centering
    \begin{tikzpicture}[
node distance = 10mm and 10mm, %  <--- control distances between nodes
C/.style = {circle, draw, fill=black, 
            font=\bfseries, text=white}
                        ]
%% NODES
\node (n1)  [C] {1};
\node (n2)  [C,right=of n1] {2};
\node (n4)  [C,right=of n2] {4};
\node (n3)  [C,above=of n2] {3};
\node (n5)  [C,above=of n4] {5};
%% EDGES
\draw [line width =1.3pt]   
    (n1) to (n2)    
    (n2) to (n3)    (n2) to (n4)    (n3) to (n5)    (n5) to (n4);
    \end{tikzpicture}
\caption{Subfigure A}
    \label{fig:subfig8}
\end{subfigure}
\hfil  % <--- push sub figures appart
\begin{subfigure}{0.32\linewidth}
\centering
    \begin{tikzpicture}[
node distance = 10mm and 10mm, %  <--- control distances between nodes
C/.style = {circle, draw, fill=black,
            font=\bfseries, text=white}
                        ]
%% NODES
\node (n1)  [C] {1};
\node (n2)  [C,right=of n1] {2};
\node (n4)  [C,right=of n2] {4};
\node (n3)  [C,above=of n2] {3};
\node (n5)  [C,above=of n4] {5};
%% EDGES
\draw [line width =1.3pt]
    (n2) to (n3)    (n2) to (n4)    (n3) to (n5)    (n5) to (n4);
    \end{tikzpicture}
\caption{Subfigure B}
    \label{fig:subfig10}
\end{subfigure}
    \caption{ciao}
    \label{fig:subfig1.a.4}
    \end{figure}
\end{document}

答案2

使用 \subfig 包

\documentclass[letterpaper, 10 pt, conference,tikz]{memoir}  % Comment this line out 
% \usepackage{subcaption}

\usepackage{tikz}
\usepackage{subfig}

% Begin document
\begin{document}    
\begin{figure}
\centering
    \subfloat[subfigure A]{  \label{fig:subfig8}
    \begin{tikzpicture}[scale=1]
    \draw [line width=1,fill=black] (0,1) circle [radius=0.3];
    \node [white] at (0,1) {$\mathbf{1}$};
    \draw [line width=1,fill=black] (2,1) circle [radius=0.3];
    \node [white] at (2,1) {$\mathbf{2}$};
    \draw [line width=1,fill=black] (2,3) circle [radius=0.3];
    \node [white] at (2,3) {$\mathbf{3}$};
    \draw [line width=1,fill=black] (5,1) circle [radius=0.3];
    \node [white] at (5,1) {$\mathbf{4}$};
    \draw [line width=1,fill=black] (5,3) circle [radius=0.3];
    \node [white] at (5,3) {$\mathbf{5}$};

    \draw [line width =1.3] (0.3,1) to (1.7,1);
    \draw [line width =1.3] (2,2.8) to (2,1.2);
    \draw [line width =1.3] (2.2,1) to (4.8,1);
    \draw [line width =1.3] (5,1.2) to (5,2.8);
    \draw [line width =1.3] (2.2,3) to (4.8,3);
    \end{tikzpicture} 

    }  \hfill
    \subfloat[subfigure B]{
    \begin{tikzpicture}[scale=1]

    \draw [line width=1,fill=black] (0,1) circle [radius=0.3];
    \node [white] at (0,1) {$\mathbf{1}$};
    \draw [line width=1,fill=black] (2,1) circle [radius=0.3];
    \node [white] at (2,1) {$\mathbf{2}$};
    \draw [line width=1,fill=black] (2,3) circle [radius=0.3];
    \node [white] at (2,3) {$\mathbf{3}$};
    \draw [line width=1,fill=black] (5,1) circle [radius=0.3];
    \node [white] at (5,1) {$\mathbf{4}$};
    \draw [line width=1,fill=black] (5,3) circle [radius=0.3];
    \node [white] at (5,3) {$\mathbf{5}$};

    \draw [line width =1.3] (2,2.8) to (2,1.2);
    \draw [line width =1.3] (2.2,1) to (4.8,1);
    \draw [line width =1.3] (5,1.2) to (5,2.8);
    \draw [line width =1.3] (2.2,3) to (4.8,3);
    \end{tikzpicture}   
            \label{fig:subfig10}
}
    \caption{ciao} 
    \label{fig:subfig1.a.4}
    \end{figure}   
\end{document}

在此处输入图片描述

答案3

您可以改用\subcaptionbox环境subfigure

\documentclass[letterpaper, 10 pt, conference,tikz]{memoir}  % Comment this line out 
\usepackage{subcaption}
\usepackage{tikz}
\begin{document}
\begin{figure}
    \centering
    \subcaptionbox{Subfigure A\label{fig:subfig8}}{%
      \begin{tikzpicture}[scale=1]
        \draw [line width=1,fill=black] (0,1) circle [radius=0.3];
        \node [white] at (0,1) {$\mathbf{1}$};
        \draw [line width=1,fill=black] (2,1) circle [radius=0.3];
        \node [white] at (2,1) {$\mathbf{2}$};
        \draw [line width=1,fill=black] (2,3) circle [radius=0.3];
        \node [white] at (2,3) {$\mathbf{3}$};
        \draw [line width=1,fill=black] (5,1) circle [radius=0.3];
        \node [white] at (5,1) {$\mathbf{4}$};
        \draw [line width=1,fill=black] (5,3) circle [radius=0.3];
        \node [white] at (5,3) {$\mathbf{5}$};
        \draw [line width =1.3] (0.3,1) to (1.7,1);
        \draw [line width =1.3] (2,2.8) to (2,1.2);
        \draw [line width =1.3] (2.2,1) to (4.8,1);
        \draw [line width =1.3] (5,1.2) to (5,2.8);
        \draw [line width =1.3] (2.2,3) to (4.8,3);
      \end{tikzpicture}%
    }%
    \hfill
    \subcaptionbox{Subfigure B\label{fig:subfig10}}{%
      \begin{tikzpicture}[scale=1]
        \draw [line width=1,fill=black] (0,1) circle [radius=0.3];
        \node [white] at (0,1) {$\mathbf{1}$};
        \draw [line width=1,fill=black] (2,1) circle [radius=0.3];
        \node [white] at (2,1) {$\mathbf{2}$};
        \draw [line width=1,fill=black] (2,3) circle [radius=0.3];
        \node [white] at (2,3) {$\mathbf{3}$};
        \draw [line width=1,fill=black] (5,1) circle [radius=0.3];
        \node [white] at (5,1) {$\mathbf{4}$};
        \draw [line width=1,fill=black] (5,3) circle [radius=0.3];
        \node [white] at (5,3) {$\mathbf{5}$};
        \draw [line width =1.3] (2,2.8) to (2,1.2);
        \draw [line width =1.3] (2.2,1) to (4.8,1);
        \draw [line width =1.3] (5,1.2) to (5,2.8);
        \draw [line width =1.3] (2.2,3) to (4.8,3);
      \end{tikzpicture}%
    }   
    \caption{ciao} 
    \label{fig:subfig1.a.4}
    \end{figure}   
\end{document} 

在此处输入图片描述

相关内容