2

I tried to search on the internet but I had no luck.

I did 2 figures in TikZ and I wanted to put them side by side through the subcaption package.

This is a minimal working example:

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

but they overlap. Could you help me?

Thanks a lot.

  • 1
    just put \hfill between subfigures ... – Zarko Aug 2 '17 at 8:51
  • I tried that but then the figures go out of the column width – kalmanIsAGameChanger Aug 2 '17 at 8:52
  • 1
    this is not possible, the are only pushed to text border ... please add more information to your question (from your mwe follows, that you have only one column and in this case you have enough place for your images) – Zarko Aug 2 '17 at 9:25
3

use \subfig package

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

enter image description here

|improve this answer|||||
  • They told me that subfig is deprecated :( and I went for subcaption – kalmanIsAGameChanger Aug 2 '17 at 9:00
  • they told you correct. subcaption has some advantages over subfig, (using hyperref). and changing sub images environment doesn't solve your problem. use of hfil solve it. – Zarko Aug 2 '17 at 9:28
  • subfigure is deprecated, not subfig (ctan.org/pkg/subfig) – rpapa Aug 2 '17 at 12:17
3

You could use \subcaptionbox instead environment 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} 

enter image description here

|improve this answer|||||
3

enter image description here

as i said in comment, sub figures will push (maximal) apart \hfill between them. even better is use:

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

complete mwe (i take a liberty in rewrite your image code):

\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}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.