1

I'm a tizk newby. I have the following code:

\begin{minipage}[c]{0.5\textwidth}

\begin{tikzpicture}

\begin{pgfonlayer}{foreground}
%links from central node
\draw[line width=1pt, color=cyan] (5,-5) to (7,-5);
\draw[line width=1pt, color=cyan] (5,-5) to (5,-3);
\draw[line width=1pt, color=cyan] (5,-5) to (5,-7);
\draw[line width=1pt, color=cyan] (5,-5) to (3,-5);
\draw[line width=1pt, color=cyan] (5,-5) to (3,-1);
\draw[line width=1pt, color=cyan] (5,-5) to (0,-1);

%some other links
\draw[line width=1pt, color=cyan] (5,-3) to (0,-1);
\draw[line width=1pt, color=cyan] (5,-3) to (3,-1);
\draw[line width=1pt, color=cyan] (0,-1) to (3,-5);
\draw[line width=1pt, color=cyan] (5,-7) to (3,-5);

%nodes
\draw[color=red, fill=red] (5, -5) circle (0.5);
\draw[color=cyan, fill=cyan] (7, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (0, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -3) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -7) circle (0.3);
\end{pgfonlayer}


\begin{pgfonlayer}{background}
\draw[fill=black, rounded corners] (-1, 0) rectangle (8, -8);
\end{pgfonlayer}

\end{tikzpicture} 


\end{minipage}

%right side
\begin{minipage}[c]{0.5\textwidth}

\begin{tikzpicture}

\begin{pgfonlayer}{foreground}
%links from central node
\draw[line width=1pt, color=cyan] (5,-5) to (7,-5);

%some other links
\draw[line width=1pt, color=cyan] (5,-3) to (0,-1);
\draw[line width=1pt, color=cyan] (5,-3) to (3,-1);
\draw[line width=1pt, color=cyan] (0,-1) to (3,-5);
\draw[line width=1pt, color=cyan] (3,-5) to (5,-7);
\draw[line width=1pt, color=cyan] (5,-7) to (3,-1);
\draw[line width=1pt, color=cyan] (3,-1) to (0,-1);
\draw[line width=1pt, color=cyan] (7,-5) to (5,-3);

%nodes
\draw[color=red, fill=red] (5, -5) circle (0.5);
\draw[color=cyan, fill=cyan] (7, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (0, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -3) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -7) circle (0.3);
\end{pgfonlayer}

%background
\begin{pgfonlayer}{background}
\draw[fill=black, rounded corners] (-1, 0) rectangle (8, -8);
\end{pgfonlayer}

\end{tikzpicture} 


\end{minipage}

that gives this output:

First, I do not get why one figure is below the other one. Why is that? I tought that with {0.5\textwidth} they could be next to each other.

Second, I would like to put a label and a caption to each tikzpicture. I found this answer but it does not work inside a minipage. Does someone know an alternative?

2
  • In code between images you have empty line. This in LaTeX means: continue in new line. Foe captions; are they should be subcaptions (labeled (a) and (b) and with common caption) os two caption labeled for example Figure 1 and Figure 2?
    – Zarko
    Dec 13, 2016 at 15:23
  • If you write X followed by an empty line, then another X, do you expect the two letters to be side by side? Same thing here, you have a paragraph break between the minipages, so you need to remove the empty line. You also need a % after the first \end{minipage}, to remove the space caused by the linebreak. Dec 13, 2016 at 15:23

2 Answers 2

1

You have more problems in your code:

  • you not provide preamble of your code: it is not fan to reconstruct it from your code
  • your image is to big that two be set in one line and not overlapped
  • off-topic: your code is over complicated

Without scaling the image size, your code generate:

enter image description here

and with scaling 0.8

enter image description here

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{caption}
\usepackage{subcaption}

\usepackage{tikz}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
   \pgfsetlayers{background,%
                 main,%
                 foreground%
                 }

\begin{document}
    \begin{figure}
\begin{subfigure}{0.45\textwidth}
    \begin{tikzpicture}[scale=0.8]
\begin{pgfonlayer}{foreground}
%links from central node
\draw[line width=1pt, color=cyan] (5,-5) to (7,-5);
\draw[line width=1pt, color=cyan] (5,-5) to (5,-3);
\draw[line width=1pt, color=cyan] (5,-5) to (5,-7);
\draw[line width=1pt, color=cyan] (5,-5) to (3,-5);
\draw[line width=1pt, color=cyan] (5,-5) to (3,-1);
\draw[line width=1pt, color=cyan] (5,-5) to (0,-1);
%some other links
\draw[line width=1pt, color=cyan] (5,-3) to (0,-1);
\draw[line width=1pt, color=cyan] (5,-3) to (3,-1);
\draw[line width=1pt, color=cyan] (0,-1) to (3,-5);
\draw[line width=1pt, color=cyan] (5,-7) to (3,-5);
%nodes
\draw[color=red, fill=red] (5, -5) circle (0.5);
\draw[color=cyan, fill=cyan] (7, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (0, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -3) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -7) circle (0.3);
\end{pgfonlayer}
\begin{pgfonlayer}{background}
\draw[fill=black, rounded corners] (-1, 0) rectangle (8, -8);
\end{pgfonlayer}
    \end{tikzpicture}
\caption{left figure}
    \end{subfigure}
\hfill
\begin{subfigure}{0.45\textwidth}
    \begin{tikzpicture}[scale=0.8]
\begin{pgfonlayer}{foreground}
%links from central node
\draw[line width=1pt, color=cyan] (5,-5) to (7,-5);
%some other links
\draw[line width=1pt, color=cyan] (5,-3) to (0,-1);
\draw[line width=1pt, color=cyan] (5,-3) to (3,-1);
\draw[line width=1pt, color=cyan] (0,-1) to (3,-5);
\draw[line width=1pt, color=cyan] (3,-5) to (5,-7);
\draw[line width=1pt, color=cyan] (5,-7) to (3,-1);
\draw[line width=1pt, color=cyan] (3,-1) to (0,-1);
\draw[line width=1pt, color=cyan] (7,-5) to (5,-3);
%nodes
\draw[color=red, fill=red] (5, -5) circle (0.5);
\draw[color=cyan, fill=cyan] (7, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (0, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -3) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -1) circle (0.3);
\draw[color=cyan, fill=cyan] (3, -5) circle (0.3);
\draw[color=cyan, fill=cyan] (5, -7) circle (0.3);
\end{pgfonlayer}
%background
\begin{pgfonlayer}{background}
\draw[fill=black, rounded corners] (-1, 0) rectangle (8, -8);
\end{pgfonlayer}
    \end{tikzpicture}
\caption{right figure}
    \end{subfigure}
\caption{My figures}
    \label{fig:myfigure}
    \end{figure}
\end{document}

Addendum off-topic your code can be significantly shorter ... see, if the following solution is useful for you:

  • lines in nodes are determined for both images ones with \tikzset
  • circles are drawn as node
  • determined is default style for nodes and lines
  • black bacground is drawn as node which fir all other nodes

Your code than become:

\documentclass{article}
\usepackage{geometry}
\usepackage{caption}
\usepackage{subcaption}

\usepackage{tikz}
\usetikzlibrary{backgrounds, fit, scopes}

\begin{document}
    \begin{figure}[htb]
\tikzset{    
C/.style args = {#1/#2}{shape=circle,
                        fill=#1, minimum size=#2},% C as circle ...
L/.style      = {line width=1pt, color=#1},% L as line
C/.default = cyan/5mm,
L/.default = cyan
        }
\begin{subfigure}{0.45\textwidth}
    \begin{tikzpicture}[scale=0.8]
% nodes
\node (n1) [C=red/8mm] at (5, -5) {};
\node (n2) [C] at (7, -5) {};
\node (n3) [C] at (0, -1) {};
\node (n4) [C] at (5, -3) {};
\node (n5) [C] at (3, -1) {};
\node (n6) [C] at (5, -7) {};
\node (n7) [C] at (3, -5) {};
% links from central node
\draw[L]    (n1) -- (n2)    (n1) -- (n4)    (n1) -- (n6)
            (n1) -- (n7)    (n1) -- (n5)    (n1) -- (n3)
% other links
            (n4) -- (n3)    (n4) -- (n5)    (n3) -- (n7)
            (n6) -- (n7);
% background
\scoped[on background layer]
    \node[fill=black, rounded corners, inner sep=3mm,
          fit= (n2) (n3) (n6)] {};
    \end{tikzpicture}
\caption{left figure}
    \end{subfigure}%
\hfill
\begin{subfigure}{0.45\textwidth}
    \begin{tikzpicture}[scale=0.8]
% nodes
\node (n1) [C=red/8mm] at (5, -5) {};
\node (n2) [C] at (7, -5) {};
\node (n3) [C] at (0, -1) {};
\node (n4) [C] at (5, -3) {};
\node (n5) [C] at (3, -1) {};
\node (n6) [C] at (3, -5) {};
\node (n7) [C] at (5, -7) {};
% links from central node
\draw[L]    (n1) -- (n2)
% some other links
            (n4) -- (n3)    (n4) -- (n2)    (n3) -- (n6) 
            (n6) -- (n7)    (n7) -- (n5)    (n5) -- (n3);
            (n2) -- (n4);
% background
\scoped[on background layer]
    \node[fill=black, rounded corners, inner sep=3mm,
          fit= (n2) (n3) (n7)] {};
    \end{tikzpicture}
\caption{right figure}
    \end{subfigure}
\caption{My figures}
    \label{fig:myfigure}
    \end{figure}
\end{document}

enter image description here

1

Half the question has already been answered in the comments, which is: use % between the minipages to avoid line and paragraph breaking between the minipages.

To get subcaptions, use the subcaption package. It provides both the \subcaption{} command and the subfigure environment. Inside a subfigure Environment, you can use the normal \caption cmd, it will behave like \subcaption, and inside minipages, use \subcaption.

Regarding the TikZ newbie part, I recommend reading the first sections "A Picture for Karl" and the others, they're quite fun and instructive at the same time. I did some improvement in the drawing, using styles and nodes to make it more flexible.

\documentclass{article}
\usepackage{tikz,subcaption}
\usetikzlibrary{backgrounds}
\usepackage[margin=0.5cm]{geometry}
\begin{document}
\vspace*{1cm}
Using the referencing mechanism: Subfigure \ref{asubfig}, Subfigure \ref{anothersubfig} and  Figure \ref{thewholefig}
  \begin{figure}[h]
  \centering
  \begin{minipage}[c]{0.5\textwidth}
    \begin{tikzpicture}[links/.style={cyan, minimum size=0.6cm, circle, inner sep=0pt, fill, draw},
                        special/.style={red, minimum size=1cm, circle, inner sep=0pt, draw, fill}]

      %nodes
      \node[special] at (5, -5) (sp) {};
      \begin{scope}[every node/.style={links}]
        \node at (7, -5) (a) {};
        \node at (0, -1) (b) {};
        \node at (5, -3) (c) {};
        \node at (3, -1) (d) {};
        \node at (3, -5) (e) {};
        \node at (5, -7) (f) {};
      \end{scope}
      \foreach \i in {a,...,f} \draw[cyan, line width=1pt] (sp) -- (\i) node[black]{\i};
      \draw[cyan, line width=1pt] (f) -- (e) -- (b) -- (c) -- (d);

      \begin{scope}[on background layer]
        \fill[black, rounded corners] (-1, 0) rectangle (8, -8);
      \end{scope}
    \end{tikzpicture}
      \subcaption{A subfigure\label{asubfig}}
  \end{minipage}%
  %
  \begin{minipage}[c]{0.5\textwidth}
    \begin{tikzpicture}[links/.style={cyan, minimum size=0.6cm, circle, inner sep=0pt, fill, draw},
                        special/.style={red, minimum size=1cm, circle, inner sep=0pt, draw, fill}]
      %nodes
      \node[special] at (5, -5) (sp) {};
      \begin{scope}[every node/.style={links}]
        \node at (7, -5) (a) {};
        \node at (0, -1) (b) {};
        \node at (5, -3) (c) {};
        \node at (3, -1) (d) {};
        \node at (3, -5) (e) {};
        \node at (5, -7) (f) {};
      \end{scope}
      \draw[cyan, line width=1pt] (d) -- (f) -- (e) -- (b) -- (d) -- (c) -- (a) -- (sp) (c) -- (b);
      \begin{scope}[on background layer]
        \fill[black, rounded corners] (-1, 0) rectangle (8, -8);
      \end{scope}
    \end{tikzpicture}  
    \subcaption{Another subfigure\label{anothersubfig}}
  \end{minipage}
  \caption{The whole figure\label{thewholefig}}
\end{figure}
\end{document}

enter image description here

You must log in to answer this question.

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