# Script for dualizing diagrams in tikzcd

I'm very new to Tex, and recently realized that I will be needing to use TikZCD a fair amount. I began to draw many diagrams and realized that it would be very convenient if there was a quick way of dualizing a diagram (perhaps this means rotating everything by π and then flipping all the arrows). As a simple example

\begin{tikzcd}
T
\arrow[drr, bend left,]
\arrow[ddr, bend right,]
\arrow[dr, dashed] & & \\
& R/IJ \arrow[r] \arrow[d]
& R/I \arrow[d, "f"] \\
& R/J \arrow[r, "g"]
& R/(I+J)
\end{tikzcd}


is an example of a pullback diagram, its dual being a pushout diagram:

\begin{tikzcd}
Z \ar[d,"f",swap] \ar[r,"g"] & Y \ar[d,"g'"] \ar[ddr,bend left,swap,"k"]\\
X \ar[r,"f'",swap] \ar[drr,bend right,"j"] & W \ar[dr,dashed]\\
& & V
\end{tikzcd}


What I would like is a script that could take the first diagram and return something like the second. It would be great if anyone knew of a script or something that was capable of doing such a thing. Thanks!

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. In particular, add a minimal working example (MWE) of what you have tried so far – CarLaTeX Aug 3 '17 at 6:23
• What does "dualizing" mean exactly? Can you describe it more clearly? – Torbjørn T. Aug 3 '17 at 12:26
• Rotating everything and mirror images are easy, but they will affect the text as well. – John Kormylo Aug 3 '17 at 14:03

Here's some basic code that reverses arrows and rotates everything 180 degrees:

\documentclass{article}
\usepackage{tikz-cd}
\usepackage{etoolbox}
\usepackage{rotating}

\makeatletter
\pgfkeysgetvalue{/tikz/commutative diagrams/.unknown/.@cmd}{\dualizetikzcdunknownkeyhandler}
\patchcmd\dualizetikzcdunknownkeyhandler{\tikzcd@ar@target}{\tikzcd@ar@start}{}{\error}

\newenvironment{dualtikzcd}{
\begin{turn}{180}
\pgfkeyssetvalue{/tikz/commutative diagrams/.unknown/.@cmd}{\dualizetikzcdunknownkeyhandler}
\tikzcdset{
bend left/.code = {\tikzset{bend right=##1}},
bend right/.code = {\tikzset{bend left=##1}},
every label/.append style=swap,
to/.code={\tikzcd@setarrowend\tikzcd@ar@start{##1}},
from/.code={\tikzcd@setarrowend\tikzcd@ar@target{##1}},
/tikz/every node/.style={rotate=180,}
}
\begin{tikzcd}
}{\end{tikzcd}\end{turn}}
\makeatother

\begin{document}

\begin{tikzcd}
T
\arrow[drr, bend left,]
\arrow[ddr, bend right,]
\arrow[dr, dashed] & & \\
& R/IJ \arrow[r] \arrow[d]
& R/I \arrow[d, "f"] \\
& R/J \arrow[r, "g"]
& R/(I+J)
\end{tikzcd}

\begin{dualtikzcd}
T
\arrow[drr, bend left]
\arrow[ddr, bend right,]
\arrow[dr, dashed] & & \\
& R/IJ \arrow[r] \arrow[d]
& R/I \arrow[d, "f"] \\
& R/J \arrow[r, "g"]
& R/(I+J)
\end{dualtikzcd}

\end{document}


And the output: