1

I want to place 4 tikz graphs in 4 seperate subfigures. I'm using subcaption and the \subcaptionbox environment. However, in the resulting figure the second-row graphs overlap the top-row captions. Moreover, the vertical distance between the graphs and their corresponding captions seems too large. I prefer a solution that does not use some fixed constant variables (like vspace, even though it produced no effect when I tried it), and I don't really mind using a different subfigure environment than subcaptionbox. The code is this:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{subcaption}
\usetikzlibrary{graphs}
\usetikzlibrary{graphs.standard}
\begin{document}

\begin{figure}
  \centering
 \subcaptionbox{}[0.45\textwidth][l]{
     \begin{tikzpicture}
       \tikz \graph [nodes={circle, draw},simple] {subgraph K_n [n=4, clockwise];
             1--[edge label=A]2,3--[edge label=B]4,2--[edge label=C]3,1--[edge    label'=D]4,1--[edge label=E]3,2--[edge label=F]4; };
     \end{tikzpicture}
 }
 \subcaptionbox{}[0.45\textwidth][l]{
     \begin{tikzpicture}
       \tikz \graph [nodes={circle, draw},simple] {subgraph K_n [n=4, clockwise];
             1--[edge label=A]2,3--[edge label=B]4,2--[edge label=C]3,1--[edge label'=D]4,1-!-3,2-!-4; };
     \end{tikzpicture}
 }
 \subcaptionbox{}[0.45\textwidth][l]{
     \begin{tikzpicture}
       \tikz \graph [nodes={circle, draw},simple] {subgraph K_n [n=4, clockwise];
             1--[edge label=A]2,3--[edge label=B]4,2-!-3,1-!-4,1--[edge label=E]3,2--[edge label=F]4; };
     \end{tikzpicture}
 }
 \subcaptionbox{}[0.45\textwidth][l]{
     \begin{tikzpicture}
       \tikz \graph [nodes={circle, draw},simple] {subgraph K_n [n=4, clockwise];
             1-!-2,3-!-4,2--[edge label=C]3,1--[edge label'=D]4,1--[edge label=E]3,2--[edge label=F]4; };
     \end{tikzpicture}
 }
 \caption{4-Vertex graph, and the 3 possible solutions}
 \label{fig:4vertex}
\end{figure}
\end{document}

And the result looks like this: enter image description here

(Bonus: get the edge labels on the horizontal and vertical edges ('E','F') to be placed such that the edges do not overlap them)

1
  • You put a TikZ picture inside a TikZ picture if you do \begin{tikzpicture} \tikz … \end{tikzpicture}. Don't do this. And drop the [l] argment. If you add pos=.25 (or any other value) to the --[<options>], it will apply to all nodes. Commented Jul 15 at 18:59

1 Answer 1

1

You're putting a TikZ picture inside another TikZ picture if you do

\begin{tikzpicture}
  \tikz …
\end{tikzpicture}

The \tikz macro is the command version of the tikzpicture environment. Only one of them should be used.

I also dropped the [l] argument in the \subcaptionbox.


Since the four diagrams are very similar, I've created a command that does all the common things and its mandatory argument are just the edges then. I'm using the quotes library for the easier "<edge label>" syntax. Here you can also use ' to swap the side and add options after it for the nodes. Here I'm using near end (pos = 0.75), very near end (pos = 0.875) and very near start (pos = .125) for adjusting the placement of the Es and Fs along the middle part.

The very first diagram doesn't have much space to add these labels clearly. Maybe you need to make the radius bigger or apply sloped to the labels so that it is clear to which edge they belong.

Code

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{subcaption}
\usetikzlibrary{graphs}
\usetikzlibrary{graphs.standard}
\usetikzlibrary{quotes}
\begin{document}
\begin{figure}
\newcommand*\myGraph[2][]{%
  \tikz
    \graph[nodes={circle, draw}, simple,#1]
      {subgraph K_n [n=4, clockwise];#2};}
\centering
\subcaptionbox{}[0.45\textwidth]{%
  \myGraph{1--["A"]2, 3--["B"]4, 2--["C"]3, 1--["D"']4,
    1--["E" near end]3, 2--["F" near end]4}
}
\subcaptionbox{}[0.45\textwidth]{
  \myGraph{1--["A"]2, 3--["B"]4, 2--["C"]3, 1--["D"']4, 1-!-3, 2-!-4}
}

\bigskip % maybe?
\subcaptionbox{}[0.45\textwidth]{
  \myGraph{1--["A"]2, 3--["B"]4, 2-!-3, 1-!-4,
    1--["E" very near end]3, 2--["F"' very near end]4}
}
\subcaptionbox{}[0.45\textwidth]{
  \myGraph{1-!-2, 3-!-4, 2--["C"]3, 1--["D"']4,
    1--["E" very near start]3, 2--["F" very near end]4}
}
\caption{4-Vertex graph, and the 3 possible solutions}
\label{fig:4vertex}
\end{figure}
\end{document}

Output

enter image description here

1
  • Thank you for going the extra mile of refactoring out the common pattern and introducing the quotes library! Commented Jul 16 at 5:19

You must log in to answer this question.

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