# Script for dualizing diagrams in tikzcd by mirroring

My question is very similar to this one but in some cases I would prefer my diagrams to be mirrored instead of rotated. Are anyone able to write a script like this in LaTeX? As an example I have this diagram for the product:

\begin{tikzcd}
C \arrow[rr] \arrow[drr, "\phi_x"] \arrow[ddrr]
\arrow[dd, "\exists! \Phi", dashed] && \vdots \\
&& F(x) \\
\prod F \arrow[uurr] \arrow[urr, "\pi_x", swap] \arrow[rr] && \vdots
\end{tikzcd}


and its dual, the coproduct:

\begin{tikzcd}
\vdots \arrow[rr] \arrow[ddrr] && C \\
F(x) \arrow[urr, "\phi_x"] \arrow[drr, "\mu_x", swap] \\
\vdots \arrow[uurr]\arrow[rr] && \coprod F \arrow[uu, "\exists ! \Phi", dashed]
\end{tikzcd}


I had an attempt at modifying the script in the accepted answer of the question linked, but I failed to produce a solution. Thanks!

• There is a bounty for you. Please if the work of the great user for you is very good, check and put also the green v below the score. Thank you. – Sebastiano Sep 14 '19 at 15:15

For simple diagrams like this you only need to set the column sep to a negative value, swap the labels, and revert the arrows. The negative value here was found by looking at the manual. Significantly more complex, where complex means varying column widths, reverted arrows and so on, diagrams may not be dualized that easily. (One can still write styles that revert all relevant original keys to their "dualized" values but the effort for this may not be justified.)

\documentclass{article}
\usepackage{tikz-cd}
\tikzcdset{dual/.style={column sep=#1,every label/.append style={swap},
arrows=leftarrow},
dual/.default=-5.5em}
\begin{document}
Original diagram:
$\begin{tikzcd} C \arrow[rr] \arrow[drr, "\phi_x"] \arrow[ddrr] \arrow[dd, "\exists! \Phi", dashed] && \vdots \\ && F(x) \\ \prod F \arrow[uurr] \arrow[urr, "\pi_x", swap] \arrow[rr] && \vdots \end{tikzcd}$
Dual constructed by hand"
$\begin{tikzcd} \vdots \arrow[rr] \arrow[ddrr] && C \\ F(x) \arrow[urr, "\phi_x"] \arrow[drr, "\mu_x", swap] \\ \vdots \arrow[uurr]\arrow[rr] && \coprod F \arrow[uu, "\exists ! \Phi", dashed] \end{tikzcd}$
Dual constructed by adding the pgf key \texttt{dual} to the original diagram.
$\begin{tikzcd}[dual] C \arrow[rr] \arrow[drr, "\phi_x"] \arrow[ddrr] \arrow[dd, "\exists! \Phi", dashed] && \vdots \\ && F(x) \\ \prod F \arrow[uurr] \arrow[urr, "\pi_x", swap] \arrow[rr] && \vdots \end{tikzcd}$
\end{document}


How could make this things easier? You could just store the original diagram in a macro (using an appropriate ampersand replacement) and then dualize it reusing the macro.

\documentclass{article}
\usepackage{tikz-cd}
\tikzcdset{dual/.style={column sep=#1,every label/.append style={swap},
arrows=leftarrow},dual/.default=-5.5em,ampersand replacement=\&}
\begin{document}
\def\OriginalDiagram{C \arrow[rr] \arrow[drr, "\phi_x"] \arrow[ddrr]
\arrow[dd, "\exists! \Phi", dashed] \&\& \vdots \\
\&\& F(x) \\
\prod F \arrow[uurr] \arrow[urr, "\pi_x", swap] \arrow[rr] \&\& \vdots
}
Original diagram:
$\begin{tikzcd} \OriginalDiagram \end{tikzcd}$
Dual constructed by adding the pgf key \texttt{dual} to the original diagram.
$\begin{tikzcd}[dual] \OriginalDiagram \end{tikzcd}$
\end{document}


Needless to say that this duality does not turn a \pi into a \mu and a \prod into a \coprod.

• In the meantime, thank you very much for helping the user. I'm sorry I can't vote now but I finished all the votes. – Sebastiano Sep 12 '19 at 22:25
• I hope that the user not forgot to put the green tick as approved. For me it is a form of justice for your great work. – Sebastiano Sep 14 '19 at 20:17
• @Sebastiano Thanks for the bounty! (Whether this is precisely what the user wants we still need to find out...) – Schrödinger's cat Sep 14 '19 at 20:18
• I'm not interested in the votes :-) and I'm very happy to have assigned it to a competent user like you. – Sebastiano Sep 14 '19 at 20:28
• Thank you for your great answer and additional insight into the topic! This was exactly what I was looking for and more. Sorry if I was slow to accept, but I did not have time to read and try out your solution earlier. – Fredrik Bakke Sep 15 '19 at 14:24

I figured I could add something to the conversation by noting that you can similarly to the accepted answer set row sep to a negative value to mirror the diagram horizontally. Though the original question described vertical mirroring.

\documentclass{article}
\usepackage{tikz-cd}
\tikzcdset{vdual/.style={column sep=#1,every label/.append style={swap},
arrows=leftarrow},
vdual/.default=-5.5em, ampersand replacement=\&}
\tikzcdset{hdual/.style={row sep=#1,every label/.append style={swap},
arrows=leftarrow},
hdual/.default=-5.5em, ampersand replacement=\&}

\begin{document}
\def\OriginalDiagram{C \arrow[rr] \arrow[drr, "\phi_x"] \arrow[ddrr]
\arrow[dd, "\exists! \Phi", dashed] \&\& \vdots \\
\&\& F(x) \\
\prod F \arrow[uurr] \arrow[urr, "\pi_x", swap] \arrow[rr] \&\& \vdots
}
Original diagram:
$\begin{tikzcd} \OriginalDiagram \end{tikzcd}$

Dual constructed by adding the pgf key \texttt{vdual} to the original diagram.
$\begin{tikzcd}[vdual] \OriginalDiagram \end{tikzcd}$

Dual constructed by adding the pgf key \texttt{hdual} to the original diagram.
$\begin{tikzcd}[hdual] \OriginalDiagram \end{tikzcd}$
\end{document}


Apologies if I'm not following etiquette.

• Also your answer is good. – Sebastiano Sep 15 '19 at 19:45