Skip to content

Instantly share code, notes, and snippets.

@AyeGill
Created March 15, 2020 10:55
Show Gist options
  • Save AyeGill/527e542ab54c8a9e4575a9eb811754db to your computer and use it in GitHub Desktop.
Save AyeGill/527e542ab54c8a9e4575a9eb811754db to your computer and use it in GitHub Desktop.
How to make tikz graphs easily

How to make graphs for tikz easily

Necessary utilities: graphviz, dot2tex, xsel. Install these:

$ sudo apt install graphviz
$ sudo apt install xsel
$ pip3 install dot2tex

(May differ on your system - xsel probably already installed).

Now simply do dot2tex --figonly | xsel -i -b Type in the dot code for your graph, e.g.

graph foo {
  a -- b;
  b -- c;
}

then hit CTRL-D for EOF, and the code will be added to your clipboard!

\begin{tikzpicture}[>=latex,line join=bevel,]
  \pgfsetlinewidth{1bp}
%%
\pgfsetcolor{black}
  % Edge: a -- b
  \draw [] (27.0bp,143.83bp) .. controls (27.0bp,133.0bp) and (27.0bp,119.29bp)  .. (27.0bp,108.41bp);
  % Edge: b -- c
  \draw [] (27.0bp,71.831bp) .. controls (27.0bp,61.0bp) and (27.0bp,47.288bp)  .. (27.0bp,36.413bp);
  % Node: a
\begin{scope}
  \definecolor{strokecol}{rgb}{0.0,0.0,0.0};
  \pgfsetstrokecolor{strokecol}
  \draw (27.0bp,162.0bp) ellipse (27.0bp and 18.0bp);
  \draw (27.0bp,162.0bp) node {a};
\end{scope}
  % Node: b
\begin{scope}
  \definecolor{strokecol}{rgb}{0.0,0.0,0.0};
  \pgfsetstrokecolor{strokecol}
  \draw (27.0bp,90.0bp) ellipse (27.0bp and 18.0bp);
  \draw (27.0bp,90.0bp) node {b};
\end{scope}
  % Node: c
\begin{scope}
  \definecolor{strokecol}{rgb}{0.0,0.0,0.0};
  \pgfsetstrokecolor{strokecol}
  \draw (27.0bp,18.0bp) ellipse (27.0bp and 18.0bp);
  \draw (27.0bp,18.0bp) node {c};
\end{scope}
%
\end{tikzpicture}

Tips

  • For a left-right oriented graph, add the line rankdir="LR"; at the beginning of your graph (after the graph foo{).
  • For a different graph layout, add the option --prog neato to dot2tex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment