Skip to content

Instantly share code, notes, and snippets.

@seungwonpark
Created November 19, 2018 16:06
Show Gist options
  • Save seungwonpark/14c803186083cdde60bcdcaf3a60680f to your computer and use it in GitHub Desktop.
Save seungwonpark/14c803186083cdde60bcdcaf3a60680f to your computer and use it in GitHub Desktop.
Automatic graph drawing with TikZ
% All you have to do is replacing `{a/1/2, b/3/4, c/2/1, d/3/2}` and `{a/b, b/c, c/d}`.
% With given example, node (a) will be drawn at coordinate (1,2), (b) in at (3,4), ...,
% and edges a-b, b-c, c-d will be drawn.
% Within `scope` environment, all nodes will be surrounded with circle.
\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath, amssymb}
\usepackage{pgffor}
\begin{document}
\begin{tikzpicture}
\begin{scope}[every node/.style={draw,circle}]
% draw nodes
\foreach \name/\xx/\yy in {a/1/2, b/3/4, c/2/1, d/3/2}{
\node (\name) at (\xx, \yy) {\name};
}
% draw edges
\foreach \edgefrom/\edgeto in {a/b, b/c, c/d}{
\draw (\edgefrom) -- (\edgeto);
}
\end{scope}
\end{tikzpicture}
\end{document}
@seungwonpark
Copy link
Author

Preview:
screen shot 2018-11-20 at 1 10 58 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment