# Automatically draw a graph with a cycle in Tikz?

I know how to automatically draw graphs with nodes being only bound to the next node in a "line". But how would I do it if I have a cycle like this:

• did you try graph drawing library? – percusse Oct 20 '17 at 15:38
• no, what is this? – Cherry Oct 20 '17 at 15:41
• It's another library of TikZ. Check in the manual – percusse Oct 20 '17 at 15:46
• PGF/TikZ has cycle (IIRC) in place of last node in path. – Jakub Narębski Oct 20 '17 at 22:31
• What kind of method have you used for nodes in a line? Of course, if you have three nodes a, b and c you can use \draw (a) -- (b) -- (c) -- (a);, but you were talking about "automatically", so I'm guessing you've used something else. – Torbjørn T. Oct 20 '17 at 22:45

Maybe you intend a tikz-cd:

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[every arrow/.append style={dash},
row sep = 10pt]
&&d\ar[dddd]\\
&b\ar[ur]\\
a\ar[dr]\ar[ur]\\
&c\ar[dr]\\
&&e\\
\end{tikzcd}
\end{document}


If your nodes are not math nodes you could use \text{...} from amsmath package:

\documentclass{article}
\usepackage{tikz-cd}
\usepackage{amsmath}
\begin{document}
\begin{tikzcd}[every arrow/.append style={dash}, row sep = 10pt]
&&\text{d}\ar[dddd]\\
&\text{b}\ar[ur]\\
\text{a}\ar[dr]\ar[ur]\\
&\text{c}\ar[dr]\\
&&\text{e}\\
\end{tikzcd}
\end{document}


or, if your texts are more complicated that a simple letter, a tikz matrix directly:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[row sep = 10pt,
column sep=20pt,
matrix of nodes] (m) {
&&d\\
&b\\
a\\
&c\\
&&e\\
};
\draw (m-1-3) -- (m-2-2) -- (m-3-1) -- (m-4-2) -- (m-5-3) -- (m-1-3);
\end{tikzpicture}
\end{document}


or without any matrix:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[node distance=10pt and 20pt]
\node (d) {d};
\node[below left = of d] (b) {b};
\node[below left = of b] (a) {a};
\node[below right = of a] (c) {c};
\node[below right = of c] (e) {e};
\draw (a) -- (b) -- (d) -- (e) -- (c) -- (a);
\end{tikzpicture}
\end{document}


(The result is the same as before).

Alternately to tikz-cd, you can use this code.

\documentclass[10pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (a) {$a$} node (b) at (2,1) {$p$} node (c) at (2,-1) {$d$};
\draw (a) -- (b);
\draw (a) -- (c);
\draw (b) -- (c);
\node[fill=white,text=black] at (1,0.5) {$e$} ;
\node[fill=white,text=black] (v1) at (1,-0.5) {$f$};
\end{tikzpicture}
\end{document}