6

I sometimes find myself typesetting commutative cubes using TikZ. Here is my code.

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{matrix}

\tikzset
{
  over/.style={preaction={draw=white,-,line width=6pt}},
}

\begin{document}

\begin{tikzpicture}
  \matrix[matrix of math nodes,column sep={2.5em},row sep={2.5em},
    text height=1.5ex,text depth=.25ex]
  {
      |(e)|  X X X X & & |(0)|   X_0     \\
    & |(1)|  X_1     & & |(01)|  X_{01}  \\
      |(2)|  X_2     & & |(02)|  X_{02}  \\
    & |(12)| X_{12}  & & |(012)| X_{012} \\
  };

  \draw[->] (e)  to (2);
  \draw[->] (0)  to (02);
  \draw[->] (e)  to (0);
  \draw[->] (2)  to (02);

  \draw[->,over] (1)  to (12);
  \draw[->]      (01) to (012);
  \draw[->,over] (1)  to (01);
  \draw[->]      (12) to (012);

  \draw[->] (e)  to (1);
  \draw[->] (0)  to (01);
  \draw[->] (2)  to (12);
  \draw[->] (02) to (012);
\end{tikzpicture}

\end{document}

The problem that I often encounter is that if one of the nodes is larger than the others, then my cube is a little distorted.

cube0

My ad hoc fix is to adjust the width of one of the colums, for example by changing the first line in the matrix to

|(e)|  X X X X & & |(0)|   X_0     &[1em] \\

Then the result is pretty good:

cube1

Another thing I tried is changing the column sep and row sep options to:

column sep={5em,between origins},row sep={5em,between origins}

but then the edges are still not quite parallel:

cube2

I'm quite satisfied with the look of the second picture, but this method has a disadvantage that I have to guess a good offset value which is usually cumbersome. Is there a way of making TikZ figure out this value on its own? Or maybe there is a better way of using between origins or something else entirely?

  • 1
    Imagine you would like to anchor the 120 degree angle of node 12 if you write 12.120 TikZ wil think that this is a number and you will spend lots of time thinking what might be the problem. Hence, don't do numbers in node names. – percusse Sep 14 '16 at 13:02
  • @percusse Your advice is appreciated but it doesn't apply here. It is customary to index vertices of an $(n+1)$-cube by subsets of the set ${0, 1, \ldots, n}$. I find it really helpful for the node names to follow this convention and if I am typesetting a cube it is very unlikely that I will have to modify any angles. – Karol Szumiło Sep 14 '16 at 13:24
  • Please take a look on my cubes here: http://tex.stackexchange.com/a/234151 – LaRiFaRi Sep 14 '16 at 14:32
  • Why do you think that the edges of the last solution are not parallel? In fact, they are. – LaRiFaRi Sep 14 '16 at 14:35
  • 1
    Well you have been warned. – percusse Sep 14 '16 at 14:38
  • @LaRiFaRi I have seen this post. The edges in the last picture are not parallel, I checked it with a ruler. – Karol Szumiło Sep 14 '16 at 14:53

2 Answers2

3

The option between origins is the right one. The problem is that this option is taken into account only in the first row, and there you are specifying three columns, not four. The effect can be amplified:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset
{
  over/.style={preaction={draw=white,-,line width=6pt}},
}

\begin{document}
\begin{tikzpicture}
  \matrix[matrix of math nodes,column sep={1.5em,between origins},row sep={1em,between origins},nodes={inner sep=0.2pt}]
  {
      |(e)|  X & & |(0)|   X  \\
    & |(1)|  X & & |(01)|  X  \\
      |(2)|  X & & |(02)|  X  \\
    & |(12)| X & & |(012)| X \\
  };

  \draw[->] (e)  to (2);
  \draw[->] (0)  to (02);
  \draw[->] (e)  to (0);
  \draw[->] (2)  to (02);

  \draw[->,over] (1)  to (12);
  \draw[->]      (01) to (012);
  \draw[->,over] (1)  to (01);
  \draw[->]      (12) to (012);

  \draw[->] (e)  to (1);
  \draw[->] (0)  to (01);
  \draw[->] (2)  to (12);
  \draw[->] (02) to (012);
\end{tikzpicture}

\end{document}

original

If the four column is added to the first row:

  \matrix[matrix of math nodes,column sep={1.5em,between origins},row sep={1em,between origins},nodes={inner sep=0.2pt}]
  {
      |(e)|  X & & |(0)|   X & \\
    & |(1)|  X & & |(01)|  X  \\
      |(2)|  X & & |(02)|  X  \\
    & |(12)| X & & |(012)| X \\
  };

Then the result is the expected one:

enter image description here

1

You should use tikz-cd for such diagramms. You might find it visually more pleasing if you tweak around with the indices a bit. Have a look on the right side where I have smashed all the indices in order to get a more even look on the left side.

% arara: pdflatex

\documentclass{article}
\usepackage{tikz-cd}
\usepackage{mathtools}

\begin{document}
\begin{tikzcd}[%
    ,column sep={5em,between origins}
    ,row sep={5em,between origins}
    ]
    XXXX \arrow{rr}\arrow{dd}\arrow{dr} & & X_{\mathrlap{0}} \arrow{dd}\arrow{dr} & \\
    & X_1 \arrow[crossing over]{rr} & & X_{\mathrlap{01}} \arrow{dd} \\
    X_2 \arrow{rr}\arrow{dr} & & X_{\mathrlap{02}} \arrow{dr} & \\
    & X_{12} \arrow{rr}\arrow[<-,crossing over]{uu} & & X_{\mathrlap{012}}
\end{tikzcd}
\end{document}

enter image description here

LaRiFaRi
  • 43,807
  • 1
    This appears to work, but I don't want to use tikz-cd. The main reason I like typesetting diagrams in TikZ is that I can specify objects and arrows separately. Doing it tikz-cd style defeats the purpose in my mind. However, explicitly specifying the last column with the ampersand in the last row fixes the problem. – Karol Szumiło Sep 14 '16 at 16:50