{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TikzGraphs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This library uses the LaTeX package [pgf/tikz](http://www.ctan.org/pkg/pgf) to produce graphs. It integrates with IJulia, outputting SVG images to the notebook."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Installation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Pkg.add(\"TikzGraphs\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition, you will need to install the following dependencies if you do not already have them on your system.\n",
"* Pdf2svg. This is required by TikzPictures. On Ubuntu, you can get this by running `sudo apt-get install pdf2svg`. On Windows, you can download the binaries from http://www.cityinthesky.co.uk/opensource/pdf2svg/. Be sure to add pdf2svg to your path (and restart).\n",
"* Pgf (version 3.0 or later). Install using your latex package manager (e.g., texlive or miktex).\n",
"\n",
"Once these things are installed, you should be able to run the following:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"using TikzGraphs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"1\\\" [],\\n2/\\\"2\\\" [],\\n3/\\\"3\\\" [],\\n4/\\\"4\\\" [],\\n;\\n1 -> [,] 2;\\n2 -> [,] 3;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using TikzGraphs\n",
"using LightGraphs\n",
"g = DiGraph(4)\n",
"add_edge!(g, 1, 2)\n",
"add_edge!(g, 2, 3)\n",
"TikzGraphs.plot(g)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"1\\\" [],\\n2/\\\"2\\\" [],\\n3/\\\"3\\\" [],\\n4/\\\"4\\\" [],\\n;\\n1 -> [,] 2;\\n1 -> [,] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add_edge!(g, 3, 4)\n",
"add_edge!(g, 1, 4)\n",
"TikzGraphs.plot(g)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can save your graphs to PDF, SVG, and TEX."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"t = TikzGraphs.plot(g)\n",
"using TikzPictures # this is required for saving\n",
"TikzPictures.save(PDF(\"graph\"), t)\n",
"TikzPictures.save(SVG(\"graph\"), t)\n",
"TikzPictures.save(TEX(\"graph\"), t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Labels"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also specify the node labels."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"A\\\" [],\\n2/\\\"B\\\" [],\\n3/\\\"C\\\" [],\\n4/\\\"D\\\" [],\\n;\\n1 -> [,] 2;\\n1 -> [,] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, [\"A\", \"B\", \"C\", \"D\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can even use unicode."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"α\\\" [],\\n2/\\\"β\\\" [],\\n3/\\\"γ\\\" [],\\n4/\\\"δ\\\" [],\\n;\\n1 -> [,] 2;\\n1 -> [,] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, [\"α\", \"β\", \"γ\", \"δ\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also have latex labels."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"\\$\\\\int_0^\\\\infty f(x) dx\\$\\\" [],\\n2/\\\"\\$\\\\sqrt{2}\\$\\\" [],\\n3/\\\"\\$x^2\\$\\\" [],\\n4/\\\"\\$\\\\frac{1}{2}\\$\\\" [],\\n;\\n1 -> [,] 2;\\n1 -> [,] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, [L\"\\int_0^\\infty f(x) dx\", L\"\\sqrt{2}\", L\"x^2\", L\"\\frac{1}{2}\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can have repeated labels."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"α\\\" [],\\n2/\\\"β\\\" [],\\n3/\\\"γ\\\" [],\\n4/\\\"α\\\" [],\\n;\\n1 -> [,] 2;\\n1 -> [,] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, [\"α\", \"β\", \"γ\", \"α\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can specify the style for the nodes."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"α\\\" [draw, rounded corners, fill=blue!10],\\n2/\\\"β\\\" [draw, rounded corners, fill=blue!10],\\n3/\\\"γ\\\" [draw, rounded corners, fill=blue!10],\\n4/\\\"α\\\" [draw, rounded corners, fill=blue!10],\\n;\\n1 -> [,] 2;\\n1 -> [,] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, [\"α\", \"β\", \"γ\", \"α\"], node_style=\"draw, rounded corners, fill=blue!10\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can override the styles of selected nodes using a dictionary."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"α\\\" [draw, rounded corners, fill=blue!10,fill=green!10],\\n2/\\\"β\\\" [draw, rounded corners, fill=blue!10],\\n3/\\\"γ\\\" [draw, rounded corners, fill=blue!10,fill=yellow!10],\\n4/\\\"α\\\" [draw, rounded corners, fill=blue!10],\\n;\\n1 -> [,] 2;\\n1 -> [,] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, [\"α\", \"β\", \"γ\", \"α\"], node_style=\"draw, rounded corners, fill=blue!10\", node_styles=Dict(1=>\"fill=green!10\",3=>\"fill=yellow!10\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Edges"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can set edge labels using a dictionary."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"α\\\" [],\\n2/\\\"β\\\" [],\\n3/\\\"γ\\\" [],\\n4/\\\"α\\\" [],\\n;\\n1 -> [,edge label={x},] 2;\\n1 -> [,edge label={y},] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, [\"α\", \"β\", \"γ\", \"α\"], edge_labels=Dict((1,2)=>\"x\", (1,4)=>\"y\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can set the style for the edges."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"α\\\" [],\\n2/\\\"β\\\" [],\\n3/\\\"γ\\\" [],\\n4/\\\"α\\\" [],\\n;\\n1 -> [green,edge label={x},] 2;\\n1 -> [green,edge label={y},] 4;\\n2 -> [green,] 3;\\n3 -> [green,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, [\"α\", \"β\", \"γ\", \"α\"], edge_labels=Dict((1,2)=>\"x\", (1,4)=>\"y\"), edge_style=\"green\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can overide the style for specific edges as specified by a dictionary."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"1\\\" [],\\n2/\\\"2\\\" [],\\n3/\\\"3\\\" [],\\n4/\\\"4\\\" [],\\n;\\n1 -> [green,edge label={x},blue,] 2;\\n1 -> [green,edge label={y},] 4;\\n2 -> [green,] 3;\\n3 -> [green,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, edge_labels=Dict((1,2)=>\"x\", (1,4)=>\"y\"), edge_style=\"green\", edge_styles=Dict((1,2)=>\"blue\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Layouts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can use different layouts (currently just Layered [default], Spring, and SimpleNecklace are supported)."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [layered layout, ] {\\n1/\\\"1\\\" [],\\n2/\\\"2\\\" [],\\n3/\\\"3\\\" [],\\n4/\\\"4\\\" [],\\n;\\n1 -> [,] 2;\\n1 -> [,] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{layered}\",true,true)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, Layouts.Layered())"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [spring layout, random seed = 42] {\\n1/\\\"1\\\" [],\\n2/\\\"2\\\" [],\\n3/\\\"3\\\" [],\\n4/\\\"4\\\" [],\\n;\\n1 -> [,] 2;\\n1 -> [,] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{force}\",true,true)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, Layouts.Spring())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can set the random seed for the Spring layout."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [spring layout, random seed = 52] {\\n1/\\\"1\\\" [],\\n2/\\\"2\\\" [],\\n3/\\\"3\\\" [],\\n4/\\\"4\\\" [],\\n;\\n1 -> [,] 2;\\n1 -> [,] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{force}\",true,true)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, Layouts.Spring(randomSeed=52))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"TikzPictures.TikzPicture(\"\\\\graph [simple necklace layout, ] {\\n1/\\\"1\\\" [],\\n2/\\\"2\\\" [],\\n3/\\\"3\\\" [],\\n4/\\\"4\\\" [],\\n;\\n1 -> [,] 2;\\n1 -> [,] 4;\\n2 -> [,] 3;\\n3 -> [,] 4;\\n};\\n\",\"\",\"\\\\usepackage{fontspec}\\r\\n\\\\setmainfont{Latin Modern Math}\\r\\n\\\\usetikzlibrary{graphs}\\r\\n\\\\usetikzlibrary{graphdrawing}\\r\\n\\n\\\\usegdlibrary{circular}\",true,true)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TikzGraphs.plot(g, Layouts.SimpleNecklace())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Future Plans"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Gradually, more functionality from pgf/tikz will be migrated into this package."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.5.0",
"language": "julia",
"name": "julia-0.5"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}