Category Theory
Zulip Server
Archive

You're reading the public-facing archive of the Category Theory Zulip server.
To join the server you need an invite. Anybody can get an invite by contacting Matteo Capucci at name dot surname at gmail dot com.
For all things related to this archive refer to the same person.


Stream: practice: software

Topic: tikz examples


view this post on Zulip Nathaniel Virgo (May 14 2020 at 11:54):

I'm wondering if anyone either knows of a tikz library, or just has some nice code examples, for drawing relatively simple string diagrams? I realise there are some more heavyweight options available, but if I can use the tool I already sort-of know I'll save myself some time. I mostly want to draw the kinds of thing that appear in the graphical linear algebra papers.

view this post on Zulip sarahzrf (May 14 2020 at 14:09):

this isn't what you asked for, but it's possible that you might like to use https://tikzit.github.io/

view this post on Zulip sarahzrf (May 14 2020 at 14:11):

it's a really pleasant & pretty straightforward gui for building diagrams with nodes and edges and exporting to tikz code

view this post on Zulip sarahzrf (May 14 2020 at 14:11):

and it Actually Works Well and has a Pretty Pleasant Interface and Produces Nice Results unlike some tools i've seen

view this post on Zulip sarahzrf (May 14 2020 at 14:11):

(sorry if this is already something you know of and you were specifically looking for code for a good reason :persevere:)

view this post on Zulip Daniel Geisler (May 14 2020 at 14:20):

Thanks @sarahzrf , I'm totally checking out tikzit.github.io.

view this post on Zulip Nathaniel Virgo (May 14 2020 at 14:35):

It turned out to be not so hard to get started just using tikz. First set up a few macros:

\documentclass[a4paper]{article}

% first set up a few macros ....

\usepackage{tikz}
\newlength{\xscale}
\newlength{\yscale}
\setlength{\xscale}{0.5cm}
\setlength{\yscale}{0.3cm}

\newcommand{\spath}[1][1]{\draw[rounded corners=#1\yscale]}
% optional argument changes the corner rounding, in y scale units

\newcommand{\whitedot}[1]{\draw[fill=white] (#1) circle (0.25\yscale);}
\newcommand{\blackdot}[1]{\draw[fill=black] (#1) circle (0.25\yscale);}

\newcommand{\stringdiagram}[1]{
\begin{tikzpicture}[x=\xscale, y=\yscale, baseline={([yshift=-0.6ex]current bounding box.center)}]
#1
\end{tikzpicture}
}

\begin{document}

% .... then let's draw some diagrams

\begin{equation}
%
\stringdiagram{
\spath (0,0) -- (1,0);
\spath (1,0) -- (1,1) -- (2,1);
\spath (1,0) -- (1,-1) -- (2,-1);
\whitedot{1,0};
}
%
\quad = \quad
%
\stringdiagram{
%\draw[step=1.0,lightgray] (0,-3) grid (5,3);  % a coordinate grid is helpful while coding it
\spath (0,1) -- (1,1);
\blackdot{0,1};
\blackdot{1,1};
\spath (1,1) -- (1,2) -- (3,2) -- (3,1);
\spath (1,-1) -- (1,0) -- (3,0) -- (3,1);
\spath (3,1) -- (4,1) -- (4,2);
\whitedot{3,1};
\spath (-0.5,3) -- (4,3) -- (4,2);
\spath (4,2) -- (5,2);
\blackdot{4,2};
\blackdot{5,2};
\spath (0,-1) -- (1,-1);
\blackdot{0,-1};
\blackdot{1,-1};
\spath[2] (1,1) -- (1,-1) -- (5.5,-1);
\spath[1.5] (1,-1) -- (1,-2.5) -- (5.5,-2.5);
}
\end{equation}

\end{document}

Here's the result:

image.png

view this post on Zulip Nathaniel Virgo (May 14 2020 at 14:36):

The only thing I don't like is that the code for the more complicated diagram became hard to organise, becase you have to draw the white dots after the wires that connect to them, otherwise the wires get drawn over them. I'm not sure how to fix that.

view this post on Zulip Nathaniel Virgo (May 14 2020 at 14:38):

I will definitely check out tikzit some time, if I want to do something more complex than this. I have a (bad?) habit of liking to keep everything in a single file as much as possible, so I like being able to do things in code when I can.

view this post on Zulip sarahzrf (May 14 2020 at 14:39):

yeah, i relate to that :)

view this post on Zulip sarahzrf (May 14 2020 at 14:39):

but the organizational thing is kind of killer x_x

view this post on Zulip sarahzrf (May 14 2020 at 14:39):

...in the "kills you" sense, not the "killer app" sense

view this post on Zulip sarahzrf (May 14 2020 at 14:40):

well, ok, i have pretty much no tikz experience, so maybe there are solutions :upside_down:

view this post on Zulip sarahzrf (May 14 2020 at 14:40):

the solution i like is to use tikzit.

view this post on Zulip Jules Hedges (May 14 2020 at 14:45):

I also use raw TikZ instead of TikZit (only because my laptop won't run it, I really wish I could use it) - but I always name my nodes, which helps for organisation

view this post on Zulip sarahzrf (May 14 2020 at 14:45):

o.O why wont it run it?

view this post on Zulip Jules Hedges (May 14 2020 at 14:46):

I use \node and \draw as my main 2 commands, not \path

view this post on Zulip Jules Hedges (May 14 2020 at 14:47):

sarahzrf said:

o.O why wont it run it?

osx is too out of date, and I very long time ago ran out of enough hard disc space to update it

view this post on Zulip Nathaniel Virgo (May 14 2020 at 14:47):

Mine's using \draw as well - \spath is a macro, just to avoid writing "rounded corners = \yscale" all the time. (I suppose I could have called it \string instead.) (Except that's already a LaTeX command.)

view this post on Zulip Jules Hedges (May 14 2020 at 14:48):

It can run tikzit1, which runs for about 20 seconds before crashing

view this post on Zulip Nathaniel Virgo (May 14 2020 at 15:03):

I figured out how to use layers in tikz, so with these updated macros the white dots will always be drawn on top of the wires, no matter where they appear in the code.

\documentclass[a4paper]{article}

% first set up a few macros ....

\usepackage{tikz}
\newlength{\xscale}
\newlength{\yscale}
\setlength{\xscale}{0.5cm}
\setlength{\yscale}{0.3cm}

%\newcommand{\wire}[1][1]{\begin{pgfonlayer}{wire layer}\draw[rounded corners=#1\yscale];\end{pgfonlayer}}
\newcommand{\wire}[1][1]{\draw[rounded corners=#1\yscale]}
% optional argument changes the corner rounding, in y scale units

\newcommand{\fg}[1]{\begin{pgfonlayer}{foreground}#1\end{pgfonlayer}}

\newcommand{\whitedot}[1]{\fg{\draw[fill=white] (#1) circle (0.25\yscale);}}
\newcommand{\blackdot}[1]{\fg{\draw[fill=black] (#1) circle (0.25\yscale);}}

\newcommand{\stringdiagram}[1]{
\begin{tikzpicture}[x=\xscale, y=\yscale, baseline={([yshift=-0.6ex]current bounding box.center)}]
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}
#1
\end{tikzpicture}
}

\begin{document}

% .... then let's draw some diagrams

\begin{equation}
%
\stringdiagram{
\whitedot{1,0};
\wire (0,0) -- (1,0);
\wire (1,0) -- (1,1) -- (2,1);
\wire (1,0) -- (1,-1) -- (2,-1);
}
%
\quad = \quad
%
\stringdiagram{
%\draw[step=1.0,lightgray] (0,-3) grid (5,3);  % a coordinate grid is helpful while coding it
\wire (0,1) -- (1,1);
\blackdot{0,1};
\blackdot{1,1};
\wire (1,1) -- (1,2) -- (3,2) -- (3,1);
\wire (1,-1) -- (1,0) -- (3,0) -- (3,1);
\wire (3,1) -- (4,1) -- (4,2);
\whitedot{3,1};
\wire (-0.5,3) -- (4,3) -- (4,2);
\wire (4,2) -- (5,2);
\blackdot{4,2};
\blackdot{5,2};
\wire (0,-1) -- (1,-1);
\blackdot{0,-1};
\blackdot{1,-1};
\wire[2] (1,1) -- (1,-1) -- (5.5,-1);
\wire[1.5] (1,-1) -- (1,-2.5) -- (5.5,-2.5);
}
\end{equation}

\end{document}

view this post on Zulip Eduardo Ochs (May 14 2020 at 19:43):

@Nathaniel Virgo, I am not sure how obvious this is, but anyway... do you know that you can download the source code of most papers on Arxiv? Here I have sort of automated this to be able to download, unpack and compile sources with few keystrokes, and my local copy of the source of Fong and Spivak's "Seven Sketches" - https://arxiv.org/abs/1803.05316 - is stored at ~/snarf/https/arxiv.org/e-print/1803.05316.tar, and to unpack it I do this:

rm -Rfv ~/usrc/arxiv-sevensketches/
mkdir   ~/usrc/arxiv-sevensketches/
cd      ~/usrc/arxiv-sevensketches/
tar -xvf ~/snarf/https/arxiv.org/e-print/1803.05316.tar

view this post on Zulip Nathaniel Virgo (May 14 2020 at 20:21):

Ah, thanks - I looked at the source of a couple of papers with diagrams I like, but found the diagrams were just pdf files. I didn't think to have a look at Seven Sketches - that's a good source of nice examples.

view this post on Zulip Thomas Read (May 15 2020 at 06:54):

I think Seven Sketches uses the tikz library here: https://github.com/appliedcategorytheory/TikZWD