0

I have these two subfigures in a figure environment. I am unable to keep them inside the margin of my document. Any help will be great. Thanks

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

Below is the result I am obtaining at the moment. enter image description here. I am not sure what is happening as I cannot find any whitespace. Both linewidth and textwidth give the same result.

Any help will be great. Thanks

5
  • Please provide a working MWE (Minimal Working Example). Moreover, your code has bugs; e.g. node 5 requires the definition of node 13 to be before it. I believe if you add \centering after figure environment is should be fixed. Also replace \hfill with % after the end of the first subfigure.
    – Aria
    Jun 24, 2021 at 4:23
  • 1
    Your tikz picture is larger than the available space as well. This is mandated by node distance = 15mm. That is why the pictures are push to the right and are not centered, add \centering to subfigures as well.
    – Aria
    Jun 24, 2021 at 4:54
  • In addition, maybe you can rescale your tikzpictures, using scale=.5, every node/.style={scale=0.7} just find the right parameters!
    – NBur
    Jun 24, 2021 at 7:36
  • You can add \draw[red] (current bounding box.south west) rectangle (current bounding box.north east); before the end of the tikzpicture to see how big it is, but that is only part of the problem. You might also try using \subfloat (also in subcaption package) instead of subfigure. Jun 24, 2021 at 12:56
  • Thanks everyone I used a combination of your comments and it is fixed now.
    – Latex User
    Jun 27, 2021 at 4:13

1 Answer 1

2

I try to reproduce your images. In this I use syntax of the positioning library and adopt vertical and horizontal distances between nodes/vertices so, that both images can fit in the text area.

All nodes/vertices have the same size. They are placed in image code in the order, that coordinates for relative positions are always already defined.

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

enter image description here

1
  • @LatexUser, I didn't write any comments to your question, just provide a working solution. So I don't understand your comment :-(. If my solution works for you, you my consider to accept it (by clicking on the check mark at top left side of answer).
    – Zarko
    Jun 27, 2021 at 4:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .