astoff / tikz-cd

Commutative diagrams with TikZ
46 stars 3 forks source link

feature request: allow placing arrows on layers #10

Closed rynchn closed 3 years ago

rynchn commented 4 years ago

In order to simplify creation and maintenance of complicated three-dimensional diagrams, it would help if it were possible to place arrows on different pgf layers, rather than having to move arrows to the target cell and reverse the direction each time. Example:

\documentclass[tikz]{standalone}

\usetikzlibrary{cd}

\begin{document}

\pgfdeclarelayer{front}
\pgfsetlayers{main,front}

\begin{tikzcd}
000 \ar[dd, Rightarrow] \ar[rr] \ar[dr] &&
001 \ar[dd] \ar[dr, bend left]
\\ &
100 \ar[dd, crossing over, on layer=front] \ar[rr, crossing over, on layer=front] &&
101 \ar[dd]
\\
010 \ar[rr] \ar[dr] &&
011 \ar[dr]
\\ &
110 \ar[rr] &&
111
\end{tikzcd}

\end{document}

The following quick hack enables this (there may be a better way to do it):

--- tikzlibrarycd.code.tex  2020-07-27 22:16:37.798447201 -0500
+++ tikzlibrarycd.code.tex  2020-07-27 22:23:00.845406568 -0500
@@ -53,6 +53,9 @@
       /tikz/draw=\pgfkeysvalueof{/tikz/commutative diagrams/background color},
       /tikz/arrows=-,
       /tikz/line width=\pgfkeysvalueof{/tikz/commutative diagrams/crossing over clearance}}},
+  on layer/.style={
+    /tikz/execute at begin to={\pgfonlayer{#1}},
+    /tikz/execute at end to={\endpgfonlayer}},
   cramped/.code={\tikzcdset{
     every matrix/.append style={inner sep=+-0.3em},
     every cell/.append style={inner sep=+0.3em}}},
@@ -180,7 +183,9 @@
 \def\tikzcd@ar@new[#1]{% new syntax
   \pgfutil@g@addto@macro\tikzcd@savedpaths{%
     \path[/tikz/commutative diagrams/.cd,every arrow,#1]%
-    (\tikzcd@ar@start\tikzcd@startanchor) to (\tikzcd@ar@target\tikzcd@endanchor); }}
+    (\tikzcd@ar@start\tikzcd@startanchor)%
+    edge[/tikz/commutative diagrams/.cd,every arrow,#1]%
+    (\tikzcd@ar@target\tikzcd@endanchor); }}

 \def\tikzcd@ar@old[#1]#2{% old syntax
   \pgfutil@ifnextchar[%

The change from to to edge was needed, so that TikZ executes the code in execute at begin to before the auxiliary \path command implementing the edge. The options do need to be passed to both \path and edge: the former for execute at begin to, the latter for all other styling options.

astoff commented 3 years ago

Sorry for taking several months to reply :-)

I agree the feature you describe here would be very desirable. I also don't see a better way to implement it than your patch.

However, this implementation is not acceptable, since running the keys twice could lead to unexpected behavior. Keys can run arbitrary code and there's no guarantee they are free of side effects.

If you are still interested in this, I suggest bringing this up with the PGF maintainers. It might be possible to include a clean implementation in the topaths library. Then everyone would benefit, not only tikz-cd users.