I would like to "shade" the faces of a commutative cube drawn using tikz-cd
.
\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}
&
P
\ar{dl}[swap, sloped, near start]{t}
\ar{rr}{t'}
\ar[]{dd}[near end]{t}
& & R''
\ar{dd}{}
\ar{dl}[swap, sloped, near start]{}
\\
R
\ar[crossing over]{rr}[near start]{}
\ar{dd}[swap]{}
& & S''
\\
&
R'
\ar[near start]{rr}{}
\ar[sloped, near end]{dl}{}
& & S
\ar{dl}
\\
S'
\ar{rr}
& & P'
\ar[crossing over, leftarrow, near start]{uu}{}
\end{tikzcd}
\end{document}
One subtlety is that the vertices of this "cube" are not visible. However, there is a notional location for each vertex and I would like the shading to extend to that notional location. Really I'm just after a way of visually highlighting a face of the cube.
The shading should be quite transparent so that any edges of the cube that lie behind are still visible, and the arrows that lie along the edges of the shaded face are still clear.
Roughly speaking, I would like the effect of Drawing polyhedra using TikZ with semi-transparent and shading effect, but staying as much as possible within tikz-cd
. I have a bunch of existing diagrams that I would like to animate by highlighting various faces in this way.
Is this possible, or would I need to rewrite my diagrams to use Tikz directly?
Best Answer
Yes, it's possible:
The code:
The idea is to place a coordinate at each of the nodes in the diagram and then use those coordinates to do the filling; this is done using the
execute at end picture
key.The internal numbering schema
Each node in a
tikz-cd
diagram has an internal name of the formwhere
<number>
is the number of thetikz-cd
diagram,<row>
and<column>
are the row and column, respectively, of the node. For example, the node labeledR
in the above diagram (assuming it is the firsttikz-cd
diagram) would beHad it been the 13th diagram, the internal name for
R
would've beenFor example, the following shows, for each node, both its internal name and its new user-defined name. This is helpful if you want some guide during the filling process:
If you don't want to keep finding the currect number of your diagram, you can use the associated internal count(er)
\tikz@fig@count
, but then something liketransforms into
and the resulting code is rather cumbersome:
A better approach
Using the
alias
key (and the standard|[...]|
syntax for passing keys to nodes in a matrix), you can place easy-to-remember names to the nodes so you don't have to fiddle with the internal names and can use those simpler names for the shading:Use of backgrounds
For diagrams in which
crossing over
ordescription
are not used, one can refine the answer doing the filling in abackground
layer; in this way, even withopacity=1
the filling doesn't affect labels nor arrows:The result: