2
\documentclass[a4paper,12pt]{article}
\usepackage{amsfonts}
%\usepackage{amsbsy}
\usepackage{amssymb}
\usepackage{amsmath}
%\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage{graphics}


\usepackage{tikz}


\usepackage{amsthm}
\usepackage{hyperref}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}

\begin{figure}[hbt!]
\begin{center}

  \begin{tikzpicture}[
       thick,
       acteur/.style={
         circle,
         fill=black,
         thick,
         inner sep=2pt,
         minimum size=0.2cm
       }
     ] 



           \node (a1) at (0,0) [acteur,label=below:6]{};
           \node (a2) at (1.5,0)[acteur,label=below:1]{}; 

           \node (a3) at (1.5,1.5) [acteur,label=above:4]{};
           \node (a4) at (0,1.5) [acteur,label=above:5]{}; 
           \node (a5) at (3,0) [acteur,label=below:2]{}; 
           \node (a6) at (4.5,0) [acteur,label=below:7]{};
            \node (a7) at (3,1.5) [acteur,label=above:3]{};
          \node (a8) at (4.5,1.5) [acteur,label=above:8]{};
           \draw[red] (a1) -- (a2); 
           \draw [red]  (a2) -- (a3); 
           \draw[red] (a3) -- (a4);
           \draw[green]  (a5) -- (a2);

           \draw [red](a6) -- (a5);
           \draw[red] (a5) -- (a7);
            \draw [red](a8) -- (a7);
             \draw [red](a3) -- (a7);

           \draw (2.3, -.7) node {$U$};


\end{tikzpicture} 

\end{center}


\end{figure}


\end{document}

Here I want a directed arrow from 1 to 2 with the symbol of arrow in the middle of the green edge. Please give some idea.

egreg
  • 1,121,712
vqw7Ad
  • 494
  • Try https://tex.stackexchange.com/questions/39278/tikz-arrowheads-in-the-center/39282 – Bibi Feb 24 '20 at 10:56

2 Answers2

2

You can reuse Schrödinger's cat's style for bent arrows. This style gives good results even on curved paths and accepts an optional argument, defaulting to 0.5 (= middle), to indicate where the arrow should lie on the path on which it is placed. With this style, the arrow can be drawn with simply:

\draw[attach arrow] (a2) -- (a5);

You may specify attach arrow=0.5 if you want to be explicit about the position of the arrow on the edge from a2 to a5.

I also used the scope environment to reduce redundancy in your example and

\draw[green!80!black, attach arrow] (a2) -- (a5)
  node[pos=0.5, below, black] {$U$};

to automatically place the U below the middle of the edge from a2 to a5 (no hardcoding of coordinates here). If you want to place it a little lower, you can add \usetikzlibrary{positioning} to the preamble and replace the above two lines with, for instance:

\draw[green!80!black, attach arrow] (a2) -- (a5) coordinate[pos=0.5] (mid);
\node[below=0.3cm of mid] {$U$};

Also, pos=0.5 may be replaced with midway, but this is not tremendously useful. Full code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, bending, decorations.markings}

% https://tex.stackexchange.com/a/524935/73317 (Schrödinger's cat)
\tikzset{
    attach arrow/.style={
    decoration={
        markings,
         mark=at position 0 with {\pgfextra{%
         \pgfmathsetmacro{\tmpArrowTime}{\pgfkeysvalueof{/tikz/arc arrow/length}/(\pgfdecoratedpathlength)}%
         \xdef\tmpArrowTime{\tmpArrowTime}}},
        mark=at position {#1-3*\tmpArrowTime} with {\coordinate(@1);},
        mark=at position {#1-2*\tmpArrowTime} with {\coordinate(@2);},
        mark=at position {#1-1*\tmpArrowTime} with {\coordinate(@3);},
        mark=at position {#1+\tmpArrowTime/2} with {\coordinate(@4);
        \draw[-{Stealth[length=\pgfkeysvalueof{/tikz/arc arrow/length},bend]}]
          plot[smooth] coordinates {(@1) (@2) (@3) (@4)};},
        },
     postaction=decorate,
     },
     % Default arrow location: in the middle of the path
     attach arrow/.default=0.5,
     arc arrow/length/.initial=2mm,
}

\begin{document}

\begin{tikzpicture}[
  thick,
  acteur/.style={
    circle,
    thick,
    fill=black,
    inner sep=2pt,
    minimum size=0.2cm
  }]

  \begin{scope}[nodes={acteur}]
    \node (a1) at (0,0)     [label=below:6] {};
    \node (a2) at (1.5,0)   [label=below:1] {};

    \node (a3) at (1.5,1.5) [label=above:4] {};
    \node (a4) at (0,1.5)   [label=above:5] {};
    \node (a5) at (3,0)     [label=below:2] {};
    \node (a6) at (4.5,0)   [label=below:7] {};
    \node (a7) at (3,1.5)   [label=above:3] {};
    \node (a8) at (4.5,1.5) [label=above:8] {};
  \end{scope}

  \begin{scope}[color=red]
    \draw (a1) -- (a2);
    \draw (a2) -- (a3);
    \draw (a3) -- (a4);

    \draw (a6) -- (a5);
    \draw (a5) -- (a7);
    \draw (a8) -- (a7);
    \draw (a3) -- (a7);
  \end{scope}

  \draw[green!80!black, attach arrow] (a2) -- (a5)
    node[pos=0.5, below, black] {$U$};
\end{tikzpicture}

\end{document}

enter image description here

With the alternative based on \usetikzlibrary{positioning} and:

\draw[green!80!black, attach arrow] (a2) -- (a5) coordinate[pos=0.5] (mid);
\node[below=0.3cm of mid] {$U$};

you would get:

enter image description here

Note: there are other ways to draw the graph (e.g., TikZ matrices and \usetikzlibrary{graphs}), but the present question is about the arrow, and better tackle one problem at a time, especially when beginning. :-)

frougon
  • 24,283
  • 1
  • 32
  • 55
1

Use tikz-euclide package.

\documentclass[a4paper,12pt]{article}
\usepackage{amsfonts}
%\usepackage{amsbsy}
\usepackage{amssymb}
\usepackage{amsmath}
%\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage{graphics}


\usepackage{tikz}
\usepackage{tkz-euclide}% Newly added package


\usepackage{amsthm}
\usepackage{hyperref}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}

\begin{figure}[hbt!]
\begin{center}

  \begin{tikzpicture}[
       thick,
       acteur/.style={
         circle,
         fill=black,
         thick,
         inner sep=2pt,
         minimum size=0.2cm
       },
       decoration={markings,
       mark=at position 0.5 with {\arrow[scale=2]{>}};} % decoration setup
     ] 



           \node (a1) at (0,0) [acteur,label=below:6]{};
           \node (a2) at (1.5,0)[acteur,label=below:1]{}; 

           \node (a3) at (1.5,1.5) [acteur,label=above:4]{};
           \node (a4) at (0,1.5) [acteur,label=above:5]{}; 
           \node (a5) at (3,0) [acteur,label=below:2]{}; 
           \node (a6) at (4.5,0) [acteur,label=below:7]{};
            \node (a7) at (3,1.5) [acteur,label=above:3]{};
          \node (a8) at (4.5,1.5) [acteur,label=above:8]{};
           \draw[red] (a1) -- (a2); 
           \draw [red]  (a2) -- (a3); 
           \draw[red] (a3) -- (a4);
        \draw[postaction={decorate},green]  (a5) -- (a2); % I just added the decoration option here

           \draw [red](a6) -- (a5);
           \draw[red] (a5) -- (a7);
            \draw [red](a8) -- (a7);
             \draw [red](a3) -- (a7);

%          \tkzDrawSegment[postaction={decorate},green](a2,a5)
% This command gives the same result

           \draw (2.3, -.7) node {$U$};


\end{tikzpicture} 

\end{center}


\end{figure}


\end{document}

enter image description here

Now you play with it.