hmemcpy / milewski-ctfp-pdf

Bartosz Milewski's 'Category Theory for Programmers' unofficial PDF and LaTeX source
https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/
Other
10.84k stars 574 forks source link

Drawing commutative diagrams with TikZ #214

Closed platipo closed 4 years ago

platipo commented 4 years ago

The images are quite nice but sometimes they feel kinda cheap. It would be nice if they could be redrawn with TikZ. This would give a touch of class to a book that is already beautiful.

Example: scanned diagram

Simple solution: TikZ-diagram-simple

\begin{tikzcd}[commutative diagrams/every, column sep=small, cells={nodes={inner color=blue!20!white,outer color=white}}]
    & & Arr\otimes Arr \arrow[dr] \arrow[dl] \arrow[d, phantom, "\vee", very near start,font=\Large] & & \\
    & Arr \arrow[dl, swap, "dom"] \arrow[dr, swap, "cod"] & & Arr \arrow[dl, swap, "dom"] \arrow[dr, swap, "cod"] & \\
    Ob & & Ob & & Ob \\
\end{tikzcd}

And a more complex one: TikZ-diagram-complex

\begin{tikzpicture}[commutative diagrams/every, column sep=small, cells={nodes={inner color=blue!20!white,outer color=white}}]
    \def\colspace{1.2}
    \def\rowspace{1.5}
    \node[inner color=purple!50!blue!20!white,outer color=white] (P0) at (2*\rowspace, 2*\colspace) {$Arr\otimes Arr$};
    \node[inner color=purple!50!blue!20!white,outer color=white] (P1) at (1*\rowspace, 1*\colspace) {$Arr$};
    \node[inner color=purple!50!blue!20!white,outer color=white] (P2) at (3*\rowspace, 1*\colspace) {$Arr$};
    \node[inner color=blue!30!white,outer color=white] (P3) at (0*\rowspace, 0*\colspace) {$Ob$};
    \node[inner color=blue!30!white,outer color=white] (P4) at (2*\rowspace, 0*\colspace) {$Ob$};
    \node[inner color=blue!30!white,outer color=white] (P5) at (4*\rowspace, 0*\colspace) {$Ob$};
    \node (P0b) at (2*\rowspace, 1.7*\colspace) {\rotatebox[origin=c]{180}{ $\mathbf{\widehat{}}$}};

    \path[commutative diagrams/.cd,every arrow, every label]
        (P0) edge node {} (P1)
        (P0) edge node {} (P2)
        (P1) edge node[swap] {$dom$} (P3)
        (P1) edge node[swap] {$cod$} (P4)
        (P2) edge node[swap] {$dom$} (P4)
        (P2) edge node[swap] {$cod$} (P5)
\end{tikzpicture}
BartoszMilewski commented 4 years ago

In principle, I like TikZ. In this case I would get rid of the coloring and try to come up with a better symbol for pullback. In normal orientation, a pullback can be written like this: \begin{tikzcd} a \arrow[r, "f"] \arrow[d, "g"'] \arrow[rd, phantom, "\lrcorner", very near start] & b \arrow[d, "k"] \ c \arrow[r, "j"] & d \end{tikzcd} but I don't know how to rotate it 45 deg.

mseri commented 4 years ago

FWIW at the time of doing the other diagrams in Tikz, there was a discussion that ended with an agreement to leave all the diagrams with hand drawn colora out to preserve their unique feel and style.

XVilka commented 4 years ago

The second variant is clearly better.

platipo commented 4 years ago

In this case I would get rid of the coloring and try to come up with a better symbol for pullback.

In the more complex case I resolved the issue via \rotatebox[origin=c]{-45}{$\lrcorner$}

fixed-TikZ-diagram-complex

FWIW at the time of doing the other diagrams in Tikz, there was a discussion that ended with an agreement to leave all the diagrams with hand drawn colora out to preserve their unique feel and style.

I would gladly do it, there are only 35 diagrams in the whole book!

BartoszMilewski commented 4 years ago

Esthetically, it doesn't look as good as the original. I can't put my finger on it though.

platipo commented 4 years ago

What could be done to improve the diagram? What is it missing from the original one?

platipo commented 4 years ago

Removed the colors this is how it appears in the book:

TikZ-page

and this is the original:

original-page

XVilka commented 4 years ago

This way it looks more professional, indeed. But the point of highlighting the Arr is lost. Maybe use the bold font for this?

platipo commented 4 years ago

I tried some alternatives:

BartoszMilewski commented 4 years ago

I like the simplest one (no color, no bold) best. Go ahead and make the change.

platipo commented 4 years ago

Do you prefer to embed \tikzpictures instead of \includegraphics or to write external \tikzpictures and insert them with \input?

BartoszMilewski commented 4 years ago

Igal, what do you think?

platipo commented 4 years ago

Using the \input seems more organized to me, but since all the chapters are included with \subfile relative imports don't work indeed you have to type \input{content/N.M/images/image.tizk} in file content/N.M/chapter.tex.

So I'll insert the \tikzpictures directly into chapters.

platipo commented 4 years ago

The work has started, but just to see if we all agree I made a list of all possible images to redraw:

BartoszMilewski commented 4 years ago

Some of these diagrams are just standard diagrams and can be redrawn. But a lot of them don't fit in the mold. The rule of thumb should be that the ones that look 3-dimensional, with shading and little clouds, should be left alone.

platipo commented 4 years ago

Thank you @BartoszMilewski!

I've opened a draft pull request with 17 diagrams and I'll upload others shortly.