5

I want a number of graphs (in this example four) in one figure and try this with the following code:

\usepackage{subcaption}

\begin{figure}[h]
  \begin{subfigure}[b]{0.4\textwidth}
        \centering
    \definecolor{myblue}{RGB}{80,80,160}
    \definecolor{mygreen}{RGB}{80,160,80}
    \begin{tikzpicture}
      [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
      \node (n7) at (8,8) {7};
      \node (n6) at (9,8) {6};
      \node (n5) at (10,8) {5};
      \node (n2) at (9,7) {2};
      \node (n3) at (10,7) {3};
      \node (n4) at (10,6) {4};
      \node (n1) at (6,6) {1};
      \node (n8) at (13,7) {8};

      \draw (n7) -- (n6);
      \draw (n6) -- (n5);
      \draw (n3) -- (n4);
      \draw (n7) -- (n2);
      \draw (n6) -- (n2);
      \draw (n5) -- (n2);
      \draw (n6) -- (n3);
      \draw (n5) -- (n3);
      \draw (n2) -- (n3);
      \draw (n2) -- (n4);

    \end{tikzpicture}
    \subcaption{Picture 1}
    \label{fig:1}
  \end{subfigure}
  %
  \begin{subfigure}[b]{0.4\textwidth}
        \centering
    \definecolor{myblue}{RGB}{80,80,160}
    \definecolor{mygreen}{RGB}{80,160,80}
    \begin{tikzpicture}
      [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
      \node (n7) at (8,8) {7};
      \node (n6) at (9,8) {6};
      \node (n5) at (10,8) {5};
      \node (n2) at (9,7) {2};
      \node (n3) at (10,7) {3};
      \node (n4) at (10,6) {4};
      \node (n1) at (6,6) {1};
      \node (n8) at (13,7) {8};

      \draw (n7) -- (n6);
      \draw (n6) -- (n5);
      \draw (n3) -- (n4);
      \draw (n7) -- (n2);
      \draw (n6) -- (n2);
      \draw (n5) -- (n2);
      \draw (n6) -- (n3);
      \draw (n5) -- (n3);
      \draw (n2) -- (n3);
      \draw (n2) -- (n4);

    \end{tikzpicture}
    \subcaption{Picture 2}
    \label{fig:2}
  \end{subfigure}
%
  \begin{subfigure}[b]{0.4\textwidth}
        \centering
    \definecolor{myblue}{RGB}{80,80,160}
    \definecolor{mygreen}{RGB}{80,160,80}
    \begin{tikzpicture}
      [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
      \node (n7) at (8,8) {7};
      \node (n6) at (9,8) {6};
      \node (n5) at (10,8) {5};
      \node (n2) at (9,7) {2};
      \node (n3) at (10,7) {3};
      \node (n4) at (10,6) {4};
      \node (n1) at (6,6) {1};
      \node (n8) at (13,7) {8};

      \draw (n7) -- (n6);
      \draw (n6) -- (n5);
      \draw (n3) -- (n4);
      \draw (n7) -- (n2);
      \draw (n6) -- (n2);
      \draw (n5) -- (n2);
      \draw (n6) -- (n3);
      \draw (n5) -- (n3);
      \draw (n2) -- (n3);
      \draw (n2) -- (n4);

    \end{tikzpicture}
    \subcaption{Picture 1}
    \label{fig:1}
  \end{subfigure}
%
  \begin{subfigure}[b]{0.4\textwidth}
        \centering
    \definecolor{myblue}{RGB}{80,80,160}
    \definecolor{mygreen}{RGB}{80,160,80}
    \begin{tikzpicture}
      [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
      \node (n7) at (8,8) {7};
      \node (n6) at (9,8) {6};
      \node (n5) at (10,8) {5};
      \node (n2) at (9,7) {2};
      \node (n3) at (10,7) {3};
      \node (n4) at (10,6) {4};
      \node (n1) at (6,6) {1};
      \node (n8) at (13,7) {8};

      \draw (n7) -- (n6);
      \draw (n6) -- (n5);
      \draw (n3) -- (n4);
      \draw (n7) -- (n2);
      \draw (n6) -- (n2);
      \draw (n5) -- (n2);
      \draw (n6) -- (n3);
      \draw (n5) -- (n3);
      \draw (n2) -- (n3);
      \draw (n2) -- (n4);

    \end{tikzpicture}
    \subcaption{Picture 1}
    \label{fig:1}
  \end{subfigure}
  \caption{Pictures}
  \label{all sub assigns}
\end{figure}

This results in the following figure: enter image description here

However, I want subfigure (d) in the corner right below, just like subfigure (b). My first question is how to achieve this.

My second question is whether it is possible to include a frame around each subfigure, but not around the big figure itself.

If someone has time left, my third question is how position three or four subfigures in a way that the subfigures are still seperated properly.

Answers to any of these questions is really appreciated.

3
  • Please make a minimal working example (MWE) Commented Dec 18, 2018 at 12:07
  • about the frame around each figure: add \usetikzlibrary{backgrounds} to your document and show background rectangle to each tikzpicture. And please read section 43 background Library in pgfmanual.
    – Ignasi
    Commented Dec 18, 2018 at 12:15
  • The % between all subfigures makes that all of them form a unique line which is broken after the second one. Therefore the first line characters are expanded to fill the whole line, and the second line characters are not expanded. Like any other regular paragraph. As an alternative you can distribute the figures on a tabular.
    – Ignasi
    Commented Dec 18, 2018 at 12:19

3 Answers 3

2
\documentclass{article}
\usepackage{tikz}
\usepackage{subcaption}
\begin{document}

\begin{figure}
\centering
    \begin{subfigure}[b]{0.4\textwidth}
        \centering
        \definecolor{myblue}{RGB}{80,80,160}
        \definecolor{mygreen}{RGB}{80,160,80}
        \begin{tikzpicture}
        [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
        \node (n7) at (8,8) {7};
        \node (n6) at (9,8) {6};
        \node (n5) at (10,8) {5};
        \node (n2) at (9,7) {2};
        \node (n3) at (10,7) {3};
        \node (n4) at (10,6) {4};
        \node (n1) at (6,6) {1};
        \node (n8) at (13,7) {8};

        \draw (n7) -- (n6);
        \draw (n6) -- (n5);
        \draw (n3) -- (n4);
        \draw (n7) -- (n2);
        \draw (n6) -- (n2);
        \draw (n5) -- (n2);
        \draw (n6) -- (n3);
        \draw (n5) -- (n3);
        \draw (n2) -- (n3);
        \draw (n2) -- (n4);

        \end{tikzpicture}
        \subcaption{Picture 1}
        \label{fig:1}
    \end{subfigure}
    %
    \begin{subfigure}[b]{0.4\textwidth}
        \centering
        \definecolor{myblue}{RGB}{80,80,160}
        \definecolor{mygreen}{RGB}{80,160,80}
        \begin{tikzpicture}
        [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
        \node (n7) at (8,8) {7};
        \node (n6) at (9,8) {6};
        \node (n5) at (10,8) {5};
        \node (n2) at (9,7) {2};
        \node (n3) at (10,7) {3};
        \node (n4) at (10,6) {4};
        \node (n1) at (6,6) {1};
        \node (n8) at (13,7) {8};

        \draw (n7) -- (n6);
        \draw (n6) -- (n5);
        \draw (n3) -- (n4);
        \draw (n7) -- (n2);
        \draw (n6) -- (n2);
        \draw (n5) -- (n2);
        \draw (n6) -- (n3);
        \draw (n5) -- (n3);
        \draw (n2) -- (n3);
        \draw (n2) -- (n4);

        \end{tikzpicture}
        \subcaption{Picture 2}
        \label{fig:2}
    \end{subfigure}

    \begin{subfigure}[b]{0.4\textwidth}
        \centering
        \definecolor{myblue}{RGB}{80,80,160}
        \definecolor{mygreen}{RGB}{80,160,80}
        \begin{tikzpicture}
        [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
        \node (n7) at (8,8) {7};
        \node (n6) at (9,8) {6};
        \node (n5) at (10,8) {5};
        \node (n2) at (9,7) {2};
        \node (n3) at (10,7) {3};
        \node (n4) at (10,6) {4};
        \node (n1) at (6,6) {1};
        \node (n8) at (13,7) {8};

        \draw (n7) -- (n6);
        \draw (n6) -- (n5);
        \draw (n3) -- (n4);
        \draw (n7) -- (n2);
        \draw (n6) -- (n2);
        \draw (n5) -- (n2);
        \draw (n6) -- (n3);
        \draw (n5) -- (n3);
        \draw (n2) -- (n3);
        \draw (n2) -- (n4);

        \end{tikzpicture}
        \subcaption{Picture 1}
        \label{fig:1}
    \end{subfigure}
%
    \begin{subfigure}[b]{0.4\textwidth}
        \centering
        \definecolor{myblue}{RGB}{80,80,160}
        \definecolor{mygreen}{RGB}{80,160,80}
        \begin{tikzpicture}
        [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
        \node (n7) at (8,8) {7};
        \node (n6) at (9,8) {6};
        \node (n5) at (10,8) {5};
        \node (n2) at (9,7) {2};
        \node (n3) at (10,7) {3};
        \node (n4) at (10,6) {4};
        \node (n1) at (6,6) {1};
        \node (n8) at (13,7) {8};

        \draw (n7) -- (n6);
        \draw (n6) -- (n5);
        \draw (n3) -- (n4);
        \draw (n7) -- (n2);
        \draw (n6) -- (n2);
        \draw (n5) -- (n2);
        \draw (n6) -- (n3);
        \draw (n5) -- (n3);
        \draw (n2) -- (n3);
        \draw (n2) -- (n4);

        \end{tikzpicture}
        \subcaption{Picture 1}
        \label{fig:1}
    \end{subfigure}
    \caption{Pictures}
    \label{all sub assigns}
\end{figure}

\end{document}

enter image description here

All four in a row (also with a frame here):

\begin{figure}
\centering
\makebox[\linewidth]{%
    \fbox{\begin{subfigure}[b]{0.4\textwidth}
     ...
      \end{subfigure}%
    }% end \fbox
    \fbox{...
    ...
    \% end \fbox
 }% end \makebox
\end{figure}

enter image description here

3
  • 1
    This approach doesn't assure that the contents of the figure environment fit inside the width of the text block.
    – Mico
    Commented Dec 18, 2018 at 12:50
  • Did I said something different??? ... It is only an example how more than \textwidth can be used to get something in a row!
    – user2478
    Commented Dec 18, 2018 at 12:52
  • 1
    I certainly did not claim that you made any such claim; I was merely pointing out a potential issue. Since you didn't provide a lot of text to accompany the code and screenshot, readers might be left wondering what the code achieves -- and what it does not.
    – Mico
    Commented Dec 18, 2018 at 13:36
2

When creating figures with TikZ, the easiest way is to position them using all the power of Tikz. To do this, simply place them each in a scope (see page 126 of 3.0.1a manual). To frame them, we name this scope, with the local bounding box key (see page 1033 of the manual) and we place the caption text and the frame using this scope.

The advantage is that all these placements are relative: by moving the scopes, the caption text and the frame are correctly positioned in the desired place.

Not knowing if you want the caption text Picture 1, Picture 2, Picture 3 to be inside the frame or outside, I have put ex illustration the two possibilities.

capture

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\definecolor{myblue}{RGB}{80,80,160}
\definecolor{mygreen}{RGB}{80,160,80}
\begin{tikzpicture}
[scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
\begin{scope}[local bounding box= upleft]
\node (n7) at (8,8) {7};
\node (n6) at (9,8) {6};
\node (n5) at (10,8) {5};
\node (n2) at (9,7) {2};
\node (n3) at (10,7) {3};
\node (n4) at (10,6) {4};
\node (n1) at (6,6) {1};
\node (n8) at (13,7) {8};

\draw (n7) -- (n6);
\draw (n6) -- (n5);
\draw (n3) -- (n4);
\draw (n7) -- (n2);
\draw (n6) -- (n2);
\draw (n5) -- (n2);
\draw (n6) -- (n3);
\draw (n5) -- (n3);
\draw (n2) -- (n3);
\draw (n2) -- (n4);
\node[below=3mm of upleft,rectangle, fill=none]{Picture 1};
\end{scope}

\draw([shift={(-.2,-.2)}]upleft.south west)rectangle([shift={(.2,.2)}]upleft.north east);


\begin{scope}[shift={(9,0)},local bounding box= upright]
\node (n7) at (8,8) {7};
\node (n6) at (9,8) {6};
\node (n5) at (10,8) {5};
\node (n2) at (9,7) {2};
\node (n3) at (10,7) {3};
\node (n4) at (10,6) {4};
\node (n1) at (6,6) {1};
\node (n8) at (13,7) {8};

\draw (n7) -- (n6);
\draw (n6) -- (n5);
\draw (n3) -- (n4);
\draw (n7) -- (n2);
\draw (n6) -- (n2);
\draw (n5) -- (n2);
\draw (n6) -- (n3);
\draw (n5) -- (n3);
\draw (n2) -- (n3);
\draw (n2) -- (n4);
\node[below=3mm of upright,rectangle, fill=none]{Picture 2};
\end{scope}
\draw([shift={(-.2,-.2)}]upright.south west)rectangle([shift={(.2,.2)}]upright.north east);


\begin{scope}[shift={(0,-5)},local bounding box= downleft]
\node (n7) at (8,8) {7};
\node (n6) at (9,8) {6};
\node (n5) at (10,8) {5};
\node (n2) at (9,7) {2};
\node (n3) at (10,7) {3};
\node (n4) at (10,6) {4};
\node (n1) at (6,6) {1};
\node (n8) at (13,7) {8};

\draw (n7) -- (n6);
\draw (n6) -- (n5);
\draw (n3) -- (n4);
\draw (n7) -- (n2);
\draw (n6) -- (n2);
\draw (n5) -- (n2);
\draw (n6) -- (n3);
\draw (n5) -- (n3);
\draw (n2) -- (n3);
\draw (n2) -- (n4);
\end{scope}
\node[below=3mm of downleft,rectangle, fill=none]{Picture 3};
\draw([shift={(-.2,-.2)}]downleft.south west)rectangle([shift={(.2,.2)}]downleft.north east);

\begin{scope}[shift={(9,-5)},local bounding box= downright]
\node (n7) at (8,8) {7};
\node (n6) at (9,8) {6};
\node (n5) at (10,8) {5};
\node (n2) at (9,7) {2};
\node (n3) at (10,7) {3};
\node (n4) at (10,6) {4};
\node (n1) at (6,6) {1};
\node (n8) at (13,7) {8};

\draw (n7) -- (n6);
\draw (n6) -- (n5);
\draw (n3) -- (n4);
\draw (n7) -- (n2);
\draw (n6) -- (n2);
\draw (n5) -- (n2);
\draw (n6) -- (n3);
\draw (n5) -- (n3);
\draw (n2) -- (n3);
\draw (n2) -- (n4);
\end{scope}
\node[below=3mm of downright,rectangle, fill=none]{Picture 4};
\draw([shift={(-.2,-.2)}]downright.south west)rectangle([shift={(.2,.2)}]downright.north east);

\end{tikzpicture}

\end{document}

Translated with www.DeepL.com/Translator

0

The contents of the 4 subfigure environments are all wider than 0.4\textwidth. Do note that specifying a width of a subfigure environment does not imply that its contents will fit inside the specified width.

I suggest you (a) make the subfigure environments a bit wider (0.45\textwidth, say), (b) replace \centering with \raggedright and\raggedleft, respectively, and (c) maximize the horizontal separation between the subfigures via\hfill` instructions.

The framelines in the following screenshot are generated by loading the geometry package with the option showframe. For the final version of your real document, be sure to omit the showframe option.

enter image description here

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{subcaption,tikz}
\begin{document}
\begin{figure}[h]
  \begin{subfigure}[b]{0.45\textwidth}
    \raggedright
    \definecolor{myblue}{RGB}{80,80,160}
    \definecolor{mygreen}{RGB}{80,160,80}
    \begin{tikzpicture}
      [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
      \node (n7) at (8,8) {7};
      \node (n6) at (9,8) {6};
      \node (n5) at (10,8) {5};
      \node (n2) at (9,7) {2};
      \node (n3) at (10,7) {3};
      \node (n4) at (10,6) {4};
      \node (n1) at (6,6) {1};
      \node (n8) at (13,7) {8};

      \draw (n7) -- (n6);
      \draw (n6) -- (n5);
      \draw (n3) -- (n4);
      \draw (n7) -- (n2);
      \draw (n6) -- (n2);
      \draw (n5) -- (n2);
      \draw (n6) -- (n3);
      \draw (n5) -- (n3);
      \draw (n2) -- (n3);
      \draw (n2) -- (n4);
    \end{tikzpicture}
    \subcaption{Picture 1}
    \label{fig:1}
  \end{subfigure}%
  \hfill
  \begin{subfigure}[b]{0.45\textwidth}
    \raggedleft
    \definecolor{myblue}{RGB}{80,80,160}
    \definecolor{mygreen}{RGB}{80,160,80}
    \begin{tikzpicture}
      [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
      \node (n7) at (8,8) {7};
      \node (n6) at (9,8) {6};
      \node (n5) at (10,8) {5};
      \node (n2) at (9,7) {2};
      \node (n3) at (10,7) {3};
      \node (n4) at (10,6) {4};
      \node (n1) at (6,6) {1};
      \node (n8) at (13,7) {8};

      \draw (n7) -- (n6);
      \draw (n6) -- (n5);
      \draw (n3) -- (n4);
      \draw (n7) -- (n2);
      \draw (n6) -- (n2);
      \draw (n5) -- (n2);
      \draw (n6) -- (n3);
      \draw (n5) -- (n3);
      \draw (n2) -- (n3);
      \draw (n2) -- (n4);
    \end{tikzpicture}
    \subcaption{Picture 2}
    \label{fig:2}
  \end{subfigure}

  \bigskip

  \begin{subfigure}[b]{0.45\textwidth}
    \raggedright
    \definecolor{myblue}{RGB}{80,80,160}
    \definecolor{mygreen}{RGB}{80,160,80}
    \begin{tikzpicture}
      [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
      \node (n7) at (8,8) {7};
      \node (n6) at (9,8) {6};
      \node (n5) at (10,8) {5};
      \node (n2) at (9,7) {2};
      \node (n3) at (10,7) {3};
      \node (n4) at (10,6) {4};
      \node (n1) at (6,6) {1};
      \node (n8) at (13,7) {8};

      \draw (n7) -- (n6);
      \draw (n6) -- (n5);
      \draw (n3) -- (n4);
      \draw (n7) -- (n2);
      \draw (n6) -- (n2);
      \draw (n5) -- (n2);
      \draw (n6) -- (n3);
      \draw (n5) -- (n3);
      \draw (n2) -- (n3);
      \draw (n2) -- (n4);
    \end{tikzpicture}
    \subcaption{Picture 3}
    \label{fig:3}
  \end{subfigure}%
  \hfill
  \begin{subfigure}[b]{0.45\textwidth}
    \raggedleft
    \definecolor{myblue}{RGB}{80,80,160}
    \definecolor{mygreen}{RGB}{80,160,80}
    \begin{tikzpicture}
      [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
      \node (n7) at (8,8) {7};
      \node (n6) at (9,8) {6};
      \node (n5) at (10,8) {5};
      \node (n2) at (9,7) {2};
      \node (n3) at (10,7) {3};
      \node (n4) at (10,6) {4};
      \node (n1) at (6,6) {1};
      \node (n8) at (13,7) {8};

      \draw (n7) -- (n6);
      \draw (n6) -- (n5);
      \draw (n3) -- (n4);
      \draw (n7) -- (n2);
      \draw (n6) -- (n2);
      \draw (n5) -- (n2);
      \draw (n6) -- (n3);
      \draw (n5) -- (n3);
      \draw (n2) -- (n3);
      \draw (n2) -- (n4);
    \end{tikzpicture}
    \subcaption{Picture 4}
    \label{fig:4}
  \end{subfigure}
\caption{Pictures}
\label{all sub assigns}
\end{figure}
\end{document} 
1
  • BTW, if you need to omit the fourth subfigure, just omit (or comment out) its code.
    – Mico
    Commented Dec 18, 2018 at 12:53

You must log in to answer this question.

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