1

how can I add captions underneath two graphs (one for each graph)? Here it is the code

\begin{center}
\tikzset{ellipsenode/.style={draw, ellipse, thick, text width=5ex, align=center, inner sep=2pt}}
\begin{tikzpicture}[node distance={1mm}, thick]
\node (center) [circle] {};
\node (1) at ($(center)+(240:25mm)$)[ellipsenode] {$y_{1}$};
\node (2) at ($(center)+(300:25mm)$)[ellipsenode] {$y_{2}$};
\node (3) at ($(center)$)[ellipsenode] {$y_{3}$}; 
\node (4) at ($(center)+(60:25mm)$)[ellipsenode] {$y_{4}$};
\node (5) at ($(center)+(120:25mm)$)[ellipsenode] {$y_{5}$}; 
\draw[-] (1) -- (3);
\draw[-] (1) -- (2);
\draw[-] (2) -- (3);
\draw[-] (4) -- (3);
\draw[-] (5) -- (3);
\draw[-] (5) -- (4);
\end{tikzpicture}

\hspace{30mm} \tikzset{ellipsenode/.style={draw, ellipse, thick, text width=5ex, align=center, inner sep=2pt}} \begin{tikzpicture}[node distance={1mm}, thick] \node (center) [circle] {}; \node (1) at ($(center)+(240:25mm)$)[ellipsenode] {$y_{1}$}; \node (2) at ($(center)+(300:25mm)$)[ellipsenode] {$y_{2}$}; \node (3) at ($(center)$)[ellipsenode] {$y_{3}$}; \node (4) at ($(center)+(60:25mm)$)[ellipsenode] {$y_{4}$}; \node (5) at ($(center)+(120:25mm)$)[ellipsenode] {$y_{5}$}; \draw[-] (1) -- (3); \draw[-] (1) -- (2); \draw[-] (2) -- (3); \draw[-] (5) -- (3); \end{tikzpicture} \end{center}

Mary
  • 13
  • 1
    Welcome to TeX.SX! Individual captionss like "Figure 1 ..." and "Figure 2..." or a shared caption with subcaptions "a) ..." and "b)..."? – leandriis May 22 '21 at 11:36
  • The approaches shwon in https://tex.stackexchange.com/a/597566/134144 should also be applicable to your graphs. Just replace the tabular environments with your tkizpictureenvironments. – leandriis May 22 '21 at 11:38
  • @leandriis thank you for your reply! I think Figure 1 and Figure 2. I need to be able to add the caption also to single graphs. Thank you again :) – Mary May 22 '21 at 11:39

2 Answers2

1

The best way to do so is using a \caption in a figure-environment. Use it like this:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.geometric}

\begin{document} \begin{figure}[ht!] \centering \tikzset{ellipsenode/.style={draw, ellipse, thick, text width=5ex, align=center, inner sep=2pt}} \begin{tikzpicture}[node distance={1mm}, thick] \node (center) [circle] {}; \node (1) at ($(center)+(240:25mm)$)[ellipsenode] {$y_{1}$}; \node (2) at ($(center)+(300:25mm)$)[ellipsenode] {$y_{2}$}; \node (3) at ($(center)$)[ellipsenode] {$y_{3}$}; \node (4) at ($(center)+(60:25mm)$)[ellipsenode] {$y_{4}$}; \node (5) at ($(center)+(120:25mm)$)[ellipsenode] {$y_{5}$}; \draw[-] (1) -- (3); \draw[-] (1) -- (2); \draw[-] (2) -- (3); \draw[-] (4) -- (3); \draw[-] (5) -- (3); \draw[-] (5) -- (4); \end{tikzpicture} \caption{Some cool figure} \end{figure}

\begin{figure}[ht!]
    \centering
    \tikzset{ellipsenode/.style={draw, ellipse, thick, text width=5ex, align=center, inner sep=2pt}}
    \begin{tikzpicture}[node distance={1mm}, thick]
        \node (center) [circle] {};
        \node (1) at ($(center)+(240:25mm)$)[ellipsenode] {$y_{1}$};
        \node (2) at ($(center)+(300:25mm)$)[ellipsenode] {$y_{2}$};
        \node (3) at ($(center)$)[ellipsenode] {$y_{3}$}; 
        \node (4) at ($(center)+(60:25mm)$)[ellipsenode] {$y_{4}$};
        \node (5) at ($(center)+(120:25mm)$)[ellipsenode] {$y_{5}$}; 
        \draw[-] (1) -- (3);
        \draw[-] (1) -- (2);
        \draw[-] (2) -- (3);
        \draw[-] (5) -- (3);
    \end{tikzpicture}
    \caption{Another cool figure}  
\end{figure}

\end{document}

Tikz with caption

You can also have the figures next to each: Putting two images beside each other

Has this answered your question?

Mico
  • 506,678
  • 1
    @Mico Yeah, because the OP showed use without text, I thought the floating would be only more confusing. I know H completely disabled floating, where ht! doesn't, so I thought H would be the safe option. However, I don't have much experience with it, so I'll trust you and update my answer. – Vincent Kuhlmann May 22 '21 at 14:38
1

I assume, that you after to have two parallel pictures with own captions. This you can simply achieved by putting pictures in tabularx table:

enter image description here

In the following MWE (Minimal Working example) is preserved their inserting in document using center environment and for caption are used \captionof {figure}{<caption text>} defined in caption package or you can use small capt-of package:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,
                shapes.geometric}
\usepackage{caption}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document} \begin{center} \tikzset{ellipsenode/.style = {% common style for nodes ellipse, draw, thick, text width=2em, align=center, inner sep=2pt}, node distance = 21mm and 7mm } \begin{tabularx}{\linewidth}{@{} C @{\hspace{30mm}} C @{}} \begin{tikzpicture} \begin{scope}[nodes=ellipsenode] \node (1) {$y_{1}$}; \node (2) [above right=of 1] {$y_{3}$}; \node (3) [below right=of 2] {$y_{2}$}; \node (4) [above left=of 2] {$y_{5}$}; \node (5) [above right=of 2] {$y_{4}$}; \end{scope} % \draw (1) -- (3) -- (2) -- (1) (2) -- (4) -- (5) -- (2); \end{tikzpicture} \captionof{figure}{Caption of left diagram} \label{fig:diagram-left} & \begin{tikzpicture} \begin{scope}[nodes=ellipsenode] \node (1) {$y_{1}$}; \node (2) [above right=of 1] {$y_{3}$}; \node (3) [below right=of 2] {$y_{2}$}; \node (4) [above left=of 2] {$y_{5}$}; \node (5) [above right=of 2] {$y_{4}$}; \end{scope} % \draw (1) -- (3) -- (2) -- (1) (2) -- (4); \end{tikzpicture} \captionof{figure}{Caption of right diagram} \label{fig:diagram-right} \end{tabularx} \end{center} \end{document}

However, if you like to enable to float figures to the best position in the document, you only need to replace center with figure and use standard captions commands:

% preamble
    \begin{figure}[ht]
\tikzset{ellipsenode/.style = {% common style for nodes
    ellipse, draw, thick, text width=2em, align=center, inner sep=2pt},
         node distance = 21mm and 7mm
}
    \begin{tabularx}{\linewidth}{@{} C @{\hspace{30mm}} C @{}}
    \begin{tikzpicture}
    \begin{scope}[nodes=ellipsenode]
\node (1) {$y_{1}$};
\node (2) [above right=of 1] {$y_{3}$};
\node (3) [below right=of 2] {$y_{2}$};
\node (4) [above  left=of 2] {$y_{5}$};
\node (5) [above right=of 2] {$y_{4}$};
    \end{scope}
%
\draw   (1) -- (3) -- (2) -- (1)
        (2) -- (4) -- (5) -- (2);
    \end{tikzpicture}
\caption{Caption of left diagram}
\label{fig:diagram-left}
    &
    \begin{tikzpicture}
    \begin{scope}[nodes=ellipsenode]
\node (1) {$y_{1}$};
\node (2) [above right=of 1] {$y_{3}$};
\node (3) [below right=of 2] {$y_{2}$};
\node (4) [above  left=of 2] {$y_{5}$};
\node (5) [above right=of 2] {$y_{4}$};
    \end{scope}
%
\draw   (1) -- (3) -- (2) -- (1)
        (2) -- (4);
    \end{tikzpicture}
\caption{Caption of right diagram}
\label{fig:diagram-right}
\end{tabularx}
    \end{figure}

I took a liberty to change your diagram code:

  • defined are common nodes style for both pictures,
  • by using positioning TikZ library nodes are positioned relative to each other.
  • nodes are grouped by scope
  • connections between nodes are drawn in two loops

Now the code slightly shorter.

Zarko
  • 296,517