1

My question is

How can i draw self loop and and parallel edge in my graph? i am working in beamer

Self-loop at vertex V_1

Parallel edges between V_2 and V_3

One small question how to center the figure

MWE

\documentclass[12pt,fleqn]{beamer}
\usetheme{AnnArbor}
\usepackage{setspace}
\usepackage{amsmath}
\usepackage[utf8]{inputenc}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\usetikzlibrary{decorations.markings}
\usecolortheme{beaver}
\usefonttheme{professionalfonts} % using non standard fonts for beamer
\usefonttheme{serif} % default family is serif
\addtobeamertemplate{frametitle}{}{\vspace{-0.4em}} % decrease
\begin{document}
\begin{frame}[t]
\begin{tikzpicture}
    \draw[line width=0.5pt] (0.0,3.0) -- (3.0,3.0);
    \draw[line width=0.5pt] (3.0,3.0) -- (3.0,0.0);
    \draw[line width=0.5pt] (3.0,0.0) -- (0.0,0.0);
    \draw[line width=0.5pt] (0.0,0.0) -- (0.0,3.0);
    \draw [fill=black] (0,3) circle (1.5pt);
    \draw[color=black,above] (0.0,3.10) node {$v_1$};
    \draw [fill=black] (3,3) circle (1.5pt);
    \draw[color=black,above] (3.0,3.10) node {$v_2$};
    \draw [fill=black] (3,0) circle (1.5pt);
    \draw[color=black,below] (3.0,-0.10) node {$v_3$};
    \draw [fill=black] (0,0) circle (1.5pt);
    \draw[color=black,below] (0.0,-0.10) node {$v_4$};
    \end{tikzpicture}
\end{frame}
\end{document}
3
  • Welcome to TeX.SX! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it).
    – TobiBS
    Commented Aug 25, 2020 at 7:18
  • @SDP -- please have look at the revised answer
    – js bibra
    Commented Aug 25, 2020 at 10:44
  • @SDP did the answer meet your requirement
    – js bibra
    Commented Dec 13, 2020 at 0:58

2 Answers 2

2

enter image description here

By use of automata library (similar as @js bibra in his answer: +1), arrows.meta for arrows, instead of parallel arrows are used bended, from preamble are removed packages which are loaded by beamer (xcolor, amsmath), has no sense in presentation (setspace) or is with recent LaTeX default feature ([utf]{inputenc}):

\documentclass[12pt,fleqn]{beamer}
\usetheme{AnnArbor}
\usecolortheme{beaver}
\usefonttheme{professionalfonts} % using non standard fonts for beamer
\usefonttheme{serif} % default family is serif
\addtobeamertemplate{frametitle}{}{\vspace{-0.4em}} % decrease
%\usepackage{setspace}
%\usepackage{amsmath}
%\usepackage[utf8]{inputenc}
\usepackage{pgfplots}   % it load tikz and pgf too
\pgfplotsset{compat=1.17}

\usepackage{mathrsfs}
\usetikzlibrary{arrows.meta, automata,
                positioning,
                shadows}
\begin{document}
\begin{frame}
\begin{center}
    \begin{tikzpicture}[
      node distance = 22mm and 22mm,
state/.append style = {circle, draw=orange, fill=orange, 
                       minimum size=1.2em},
every edge/.style = {draw, -Stealth, semithick}
                        ] 
    \begin{scope}[nodes=state]
\node (n1)  {$v_1$};
\node (n2)  [right=of n1]   {$v_2$};
\node (n3)  [below=of n2]   {$v_3$};
\node (n4)  [left =of n3]   {$v_4$};
    \end{scope}
\path   (n3) edge  (n4)
        (n4) edge  (n1)
        (n1) edge [loop above,looseness=15]  (n1)
        (n1) edge  (n2)
        (n2) edge [bend left=15] (n3)
        (n3) edge [bend left=15] (n2)
        ;
    \end{tikzpicture}
\end{center}
\end{frame}
\end{document}
1
  • Thanks a lot for your help.
    – SandyM
    Commented Jan 24, 2021 at 9:34
1

Add the following line

\path
(v1) edge [->,loop above] node {} (v1);

just before \endtikzpicture

enter image description here

edit -- an alternate solution which may interest and can be edited/ modified

enter image description here

\documentclass[12pt,fleqn]{beamer} 
\usetheme{AnnArbor} 
\usepackage{setspace} 
\usepackage{amsmath} 
\usepackage[utf8]{inputenc} 
\usepackage{pgf,tikz,pgfplots} 
\pgfplotsset{compat=1.15} 
\usepackage{mathrsfs} 
%\usetikzlibrary{arrows} 
%\usetikzlibrary{decorations.markings, shapes,positioning,arrows.meta,bending,automata} 
\usetikzlibrary {arrows.meta,automata,positioning,shadows,calc}
\usecolortheme{beaver} 
\usefonttheme{professionalfonts} % using non standard fonts for beamer 
\usefonttheme{serif} % default family is serif 
\addtobeamertemplate{frametitle}{}{\vspace{-0.4em}} % decrease 
\newcommand\DoubleLine[7][4pt]{%
    \path(#2)--(#3)coordinate[at start](h1)coordinate[at end](h2);
    \draw[#4]($(h1)!#1!90:(h2)$)-- node [auto=left] {#5} ($(h2)!#1!-90:(h1)$); 
    \draw[#6]($(h1)!#1!-90:(h2)$)-- node [auto=right] {#7} ($(h2)!#1!90:(h1)$);
}
\begin{document} 
    \begin{frame}[t] 
        \begin{center}
        \begin{tikzpicture}[
            ->,
            >={Stealth[round]},
            shorten >=1pt,
            auto,
            node distance=2.8cm,
            on grid,
            semithick,
        every state/.style={
            fill=red,
            draw=none,
            circular drop shadow,
            text=white
        }
    ]
        \node[state] (A)              {$v_1$};
        \node[state] (B) [right=of A] {$v_2$};
        \node[state] (D) [below=of A] {$v_4$};
        \node[state] (C) [below=of B] {$v_3$};
        
        \path (A) edge  (B) 
        (C)edge(D)
        (D)edge(A)
        (A) edge [loop above]  (A)
        ;
        
        \DoubleLine{B}{C}{<-,very thick,black}{}{->,very thick,red}{}
        \end{tikzpicture}
    \end{center}
    \end{frame} 
\end{document}

the double arrows definition is taken from here--

Labeling parallel edges in a graph

You must log in to answer this question.

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