边距图和子图

边距图和子图

我在图形环境中有这两个子图。我无法将它们保留在文档的边缘内。任何帮助都将非常有用。谢谢

\begin{figure}[H]   
   
   \medskip
   
   \begin{subfigure}[t]{.4\textwidth}
       \begin{tikzpicture}[node distance={15mm}, thick, main/.style = {draw, circle}, sink/.style = {draw, circle, fill=blue!20}] 
           \node[sink] (1) {$1$}; 
           \node[main] (2) [below left of=1] {$2$}; 
           \node[main] (3) [left of=2] {$3$}; 
           \node[main] (7) [right of=2] {$7$}; 
           \node[main] (9) [right of=7] {$9$}; 
           \node[main] (10) [below of=2] {$10$}; 
           \node[main] (11) [below right of=10] {$11$}; 
           \node[main] (15) [below left of=10] {$15$};     
           \node[main] (4) [below left of=3] {$4$};    
           \node[main] (5) [below right of=13] {$5$}; 
           \node[main] (6) [below left of=4] {$6$};    
           \node[main] (8) [below right of=11] {$8$};  
           \node[main] (12) [below right of=5] {$12$};     
           \node[main] (14) [right of=4] {$14$};
           \node[main] (13) [below of=7] {$13$};                                                                                                               
           \draw[<-] (1) -- (3); 
           \draw[<-] (13) -- (2);              
           \draw[<-] (1) -- (7);   
           \draw[<-] (1) -- (9);   
           \draw[<-] (7) -- (5);
           \draw[<-] (3) -- (14);
           \draw[<-] (14) -- (4);
           \draw[<-] (4) -- (6);
           \draw[<-] (14) -- (10);
           \draw[<-] (12) -- (8);
           \draw[<-] (10) -- (15);
           \draw[<-] (7) -- (13);      
           \draw[<-] (13) -- (11);
           \draw[<-] (5) -- (12);          
                                                                                                                                                                                                                                                                                           
       \end{tikzpicture}   
       \caption{1a}
   \end{subfigure}\hfill
   \begin{subfigure}[t]{.4\textwidth}
       \begin{tikzpicture}[node distance={15mm}, thick, main/.style = {draw, circle}, sink/.style = {draw, circle, fill=blue!20}] 
           \node[sink] (1) {$1$}; 
           \node[main] (2) [below left of=1] {$2$}; 
           \node[main] (3) [left of=2] {$3$}; 
           \node[main] (7) [right of=2] {$7$}; 
           \node[main] (9) [right of=7] {$9$}; 
           \node[main] (10) [below of=2] {$10$}; 
           \node[main] (11) [below right of=10] {$11$}; 
           \node[main] (15) [below left of=10] {$15$};     
           \node[main] (4) [below left of=3] {$4$};    
           \node[main] (5) [below right of=13] {$5$}; 
           \node[main] (6) [below left of=4] {$6$};    
           \node[main] (8) [below right of=11] {$8$};  
           \node[main] (12) [below right of=5] {$12$};     
           \node[main] (14) [right of=4] {$14$};
           \node[main] (13) [below of=7] {$13$};                                                                                                               
           \draw[<-] (1) -- (3); 
           \draw[<-] (1) -- (9);   
           \draw[<-] (3) -- (14);
           \draw[<-] (14) -- (10);
           \draw[<-] (10) -- (15);                                                                                                                                                 
       \end{tikzpicture}   
       \caption{1b}    
   \end{subfigure}
   \caption{1    }
\end{figure}

以下是我目前获得的结果。在此处输入图片描述。我不确定发生了什么,因为我找不到任何空格。linewidth 和 textwidth 都给出相同的结果。

任何帮助都非常感谢。谢谢

答案1

我尝试重现您的图像。在此过程中,我使用positioning库的语法并采用节点/顶点之间的垂直和水平距离,以便两个图像都可以适合文本区域。

所有节点/顶点都具有相同的大小。它们按顺序放置在图像代码中,相对位置的坐标始终已定义。

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning}

\usepackage{subcaption}

\begin{document}
    \begin{figure}[ht]
\tikzset{
    node distance = 7mm and 4mm,
    V/.style = {circle, draw, semithick, minimum size=2em,
                fill=#1,
                inner sep=0pt, outer sep=0pt},
  V/.default = none,
every edge/.append style = {draw, thick, {Triangle[scale=0.8]}-}
        }
        
\begin{subfigure}[t]{.45\textwidth}
\begin{tikzpicture}
    \begin{scope}[nodes=V]
\node[V=blue!20] (1) {$1$};
\node (2)   [below left=of 1]   {2};
\node (3)   [left=of  2]        {3};
\node (7)   [right=of 2]        {7};
\node (9)   [right=of 7]        {9};
\node (14)  [below=of 3]        {14};
\node (4)   [left=of 14]        {4};
\node (10)  [right=of 14]       {10};
\node (6)   [below left=of 4]   {6};
\node (15)  [below left=of 10]  {15};
%
\node (13)  [below=of 7]        {13};
\node (11)  [below right=of 10] {11};
\node (5)   [right=of 11]       {5};
\node (8)   [below left=of 5]   {8};
\node (12)  [below right=of 5]  {12};
    \end{scope}
%connections
 \draw  (1)     edge (3) 
        (13)    edge (2)
        (1)     edge (7)
        (1)     edge (9)
        (7)     edge (5)
        (3)     edge (14)
        (14)    edge (4)
        (4)     edge (6)
        (14)    edge (10)
        (12)    edge (8)
        (10)    edge (15)
        (7)     edge (13)
        (13)    edge (11)
        (5)     edge (12);
 \end{tikzpicture}
    \caption{1a}
    \end{subfigure}\hfill\begin{subfigure}[t]{.45\textwidth}
\begin{tikzpicture}
    \begin{scope}[nodes=V]
\node[V=blue!20] (1) {$1$};
\node (2)   [below left=of 1]   {2};
\node (3)   [left=of  2]        {3};
\node (7)   [right=of 2]        {7};
\node (9)   [right=of 7]        {9};
\node (14)  [below=of 3]        {14};
\node (4)   [left=of 14]        {4};
\node (10)  [right=of 14]       {10};
\node (6)   [below left=of 4]   {6};
\node (15)  [below left=of 10]  {15};
%
\node (13)  [below=of 7]        {13};
\node (11)  [below right=of 10] {11};
\node (5)   [right=of 11]       {5};
\node (8)   [below left=of 5]   {8};
\node (12)  [below right=of 5]  {12};
    \end{scope}
%connections
 \draw  (1)     edge (3)
        (1)     edge (9)
        (3)     edge (14)
        (14)    edge (10)
        (10)    edge (15);
\end{tikzpicture}
    \caption{1b}
   \end{subfigure}
\caption{Comparison different connections between vertices}
    \end{figure}
\end{document}

在此处输入图片描述

相关内容