Search code examples
graphlatexnodesgraph-theory

How to make a graph (nodes and edges) on Latex?


I new coding in latex and I want to make high-quality graphs (nodes and edges), in particular, I need to make this kind of plots.graph

Is it that even possible?


Solution

  • That is not only "even possible", that is always possible. This sort of diagram can be easily created using TikZ and similar packages.

    If you already have the coordinates of the nodes, the directions of the arrows etc., you may start from this. This is not your diagram, but is a good starting point.

    \documentclass[tikz,margin=3]{standalone}
    \usetikzlibrary{shadows} % Shadows for nodes
    \begin{document}
    \begin{tikzpicture}
    \tikzset{% This is the style settings for nodes
        dep/.style={circle,minimum size=1cm,fill=orange!20,draw=orange,
                    general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
        cli/.style={circle,minimum size=1cm,fill=white,draw,
                    general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
        spl/.style={cli,append after command={
                      node[circle,draw,dotted,
                           minimum size=1.5cm] at (\tikzlastnode.center) {}}},
        c1/.style={-stealth,very thick,red!80!black},
        v2/.style={-stealth,very thick,yellow!65!black},
        v4/.style={-stealth,very thick,purple!70!black}}
    \node[dep] (0) at (0,0) {0};
    \node[cli] (7) at (-1,-2) {7};
    \node[spl] (8) at (2,-2) {8};
    \draw[c1] (0) to[bend right] (7);
    \draw[v2] (7) -- (8);
    \draw[v4] (8) -- (2,-1) -- (0);
    \end{tikzpicture}
    \end{document}
    

    enter image description here

    You can use a matrix or whatever you want to add a legend

    \documentclass[tikz,margin=3]{standalone}
    \usetikzlibrary{shadows,matrix} % Shadows for nodes
    \begin{document}
    \begin{tikzpicture}
    \tikzset{% This is the style settings for nodes
        dep/.style={circle,minimum size=#1,fill=orange!20,draw=orange,
                    general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
        dep/.default=1cm,
        cli/.style={circle,minimum size=#1,fill=white,draw,
                    general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
        cli/.default=1cm,
        spl/.style={cli=#1,append after command={
                      node[circle,draw,dotted,
                           minimum size=1.5cm] at (\tikzlastnode.center) {}}},
        spl/.default=1cm,
        c1/.style={-stealth,very thick,red!80!black},
        v2/.style={-stealth,very thick,yellow!65!black},
        v4/.style={-stealth,very thick,purple!70!black}}
    \begin{scope}[local bounding box=graph]
        \node[dep] (0) at (0,0) {0};
        \node[cli] (7) at (-1,-2) {7};
        \node[spl] (8) at (2,-2) {8};
        \draw[c1] (0) to[bend right] (7);
        \draw[v2] (7) -- (8);
        \draw[v4] (8) -- (2,-1) -- (0);
    \end{scope}
    \matrix[draw,anchor=north west,xshift=1ex,matrix of nodes,row sep=.5ex,
            column 2/.style={text width=2.5cm,align=left,anchor=center}] 
    at (graph.north east) {
        \draw[c1] (-.5,0) -- (.5,0); & $C_1$\\
        \draw[v2] (-.5,0) -- (.5,0); & $V_2$\\
        \draw[v4] (-.5,0) -- (.5,0); & $V_4$\\
        \draw[dotted] circle (.25);  & Split Delivery\\
        \node[cli=.5cm] {};          & Cliente\\
        \node[dep=.5cm] {};          & Deposito\\
    };
    \end{tikzpicture}
    \end{document}
    

    enter image description here

    It seems that you haven't known much about TikZ yet, then this question on TeX.SE may be useful to you.