0

I am writing an assignment. TikZ picture caption is not coming in the right position. See my code below:

\documentclass{article}    
 % added tikz libraries
\usepackage{tikz}

\usetikzlibrary{positioning}

\begin{document}

Question 1 : Prove that a complete graph with n vertices contains $\frac{n \times (n-1)}{2}$ edges. A complete graph is a simple undirected graph in which every pair of distinct vertices is connected by a unique edge.For an example see the diagram below for complete graph on six vertices.

\begin{center}    
\begin{tikzpicture}[scale=.5,auto=left,every node/.style={circle,fill=black!20}]      
        \foreach \x in {1,...,6}    
        {   \node (n\x) at (\x*60:3) {\x};    
        }
    \foreach \x in {1,...,5}    
    {   \pgfmathtruncatemacro{\lowerbound}{\x+1}   
        \foreach \y in {\lowerbound,...,6}    
        {   \draw (n\x) -- (n\y);    
            % for labelled edges
            %\draw (n\x) -- (n\y) node[sloped,pos=0.39,fill=white,fill opacity=0.3,above=0.1mm,rectangle, inner sep=0.1mm] {\tiny \x-\y};  
        }
    }
\caption{Complete graph on six vertices} \label{fig:M1}    
\end{tikzpicture}    
\end{center}
  • caption had not be part of tikzpicture! – Zarko Jan 25 '18 at 6:14
2

caption had not be part of tikzpicture! with use of caption package you have two possibilities to add caption to your figure:

\documentclass{article}
\usepackage{caption}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

Question 1 : Prove that a complete graph with n vertices contains $\frac{n \times (n-1)}{2}$ edges. A complete graph is a simple undirected graph in which every pair of distinct vertices is connected by a unique edge.For an example see the diagram below for complete graph on six vertices.

\begin{center}
\begin{tikzpicture}[scale=.5,auto=left,every node/.style={circle,fill=black!20}]
        \foreach \x in {1,...,6}
        {   \node (n\x) at (\x*60:3) {\x};}
    \foreach \x in {1,...,5}
    {   \pgfmathtruncatemacro{\lowerbound}{\x+1}
        \foreach \y in {\lowerbound,...,6}
        {   \draw (n\x) -- (n\y);}
    }
\end{tikzpicture}
\captionof{figure}{Complete graph on six vertices} \label{fig:M1}
\end{center}
%
or with figure environment
%
\begin{figure}[htb]
    \centering
\begin{tikzpicture}[scale=.5,auto=left,every node/.style={circle,fill=black!20}]
        \foreach \x in {1,...,6}
        {   \node (n\x) at (\x*60:3) {\x};}
    \foreach \x in {1,...,5}
    {   \pgfmathtruncatemacro{\lowerbound}{\x+1}
        \foreach \y in {\lowerbound,...,6}
        {   \draw (n\x) -- (n\y);}
    }
\end{tikzpicture}
\caption{Complete graph on six vertices} \label{fig:M1}
\end{figure}
\end{document}

enter image description here

note: instead of caption package you also can use capt-of package.

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.