# Drawing a cube with TikZ-cd

I would like to draw a cube with TikZ-cd. The entries have different sizes, which causes the same problems as in my former question, because I want the arrows to be parallel.

 \begin{tikzcd}[row sep=25,column sep=0,nodes={inner sep=2pt}, cells={nodes={align=center,text width=\widthof{X_2 \times_{S_2} Y_2}}}] & X_1 \times_{S_1} Y_1 \ar{rr} \ar{dd} \ar{dl} & & Y_1 \ar{dd} \ar{dl} \\ X_1 \ar[crossing over]{rr} \ar{dd} & & S_1 \\ & X_2 \times_{S_2} Y_2 \ar{rr} \ar{dl} & & Y_2 \ar{dl} \\ X_2 \ar{rr} && S_2 \ar[from=uu,crossing over] \end{tikzcd}


How to achieve that the arrows really end and start at the nodes? Of course they do start and end at the nodes formally, but I mean for example that the upper arrow really ends in the near of Y_1.

• This is a complete duplicate. You already got three answers over there. What do you want to achieve? Please clarify! If you just use \begin{tikzcd}[row sep=25, column sep=25] It looks nice and symmetric. Commented Mar 20, 2015 at 9:05
• It is not a duplicate. I want the parallel arrows to be parallel. This doesn't happen without further adjustments, and sep=25 is not enough. Commented Mar 20, 2015 at 9:19
• The between origins approach is the “right“ one for both this question and your previous question about triangles. I suggest that you accept LaRiFaRi's answer. Commented Apr 2, 2015 at 14:30
• Yes, I agree. Meanwhile I have also used this in my file. Commented Apr 2, 2015 at 17:06

If you separate your columns not with a certain width between borders, but with a width between origins (centre of nodes), it should look better.

% arara: lualatex

\documentclass{article}
\usepackage{tikz-cd}
\usepackage{lua-visual-debug} % just for proove of symmetry. Without that, you may compile with pdfLaTeX

\begin{document}
$\begin{tikzcd}[row sep={40,between origins}, column sep={40,between origins}] & X_1 \times_{S_1} Y_1 \ar{rr}\ar{dd}\ar{dl} & & Y_1\vphantom{\times_{S_1}} \ar{dd}\ar{dl} \\ X_1 \ar[crossing over]{rr} \ar{dd} & & S_1 \\ & X_2 \times_{S_2} Y_2 \ar{rr} \ar{dl} & & Y_2\vphantom{\times_{S_1}} \ar{dl} \\ X_2 \ar{rr} && S_2 \ar[from=uu,crossing over] \end{tikzcd}$
\end{document}


If you want to have it really realistic, I would recommend to add some perspective tweaking:

% arara: pdflatex

\documentclass{article}
\usepackage{tikz-cd}
\newlength{\perspective}

\begin{document}
\setlength{\perspective}{2pt}
$\begin{tikzcd}[row sep={40,between origins}, column sep={40,between origins}] &[-\perspective] X_1 \times_{S_1} Y_1 \ar{rr}\ar{dd}\ar{dl} &[\perspective] &[-\perspective] Y_1\vphantom{\times_{S_1}} \ar{dd}\ar{dl} \\[-\perspective] X_1 \ar[crossing over]{rr} \ar{dd} & & S_1 \\[\perspective] & X_2 \times_{S_2} Y_2 \ar{rr} \ar{dl} & & Y_2\vphantom{\times_{S_1}} \ar{dl} \\[-\perspective] X_2 \ar{rr} && S_2 \ar[from=uu,crossing over] \end{tikzcd}$
\setlength{\perspective}{5pt}
$\begin{tikzcd}[row sep={40,between origins}, column sep={40,between origins}] &[-\perspective] X_1 \times_{S_1} Y_1 \ar{rr}\ar{dd}\ar{dl} &[\perspective] &[-\perspective] Y_1\vphantom{\times_{S_1}} \ar{dd}\ar{dl} \\[-\perspective] X_1 \ar[crossing over]{rr} \ar{dd} & & S_1 \\[\perspective] & X_2 \times_{S_2} Y_2 \ar{rr} \ar{dl} & & Y_2\vphantom{\times_{S_1}} \ar{dl} \\[-\perspective] X_2 \ar{rr} && S_2 \ar[from=uu,crossing over] \end{tikzcd}$
\setlength{\perspective}{8pt}
$\begin{tikzcd}[row sep={40,between origins}, column sep={40,between origins}] &[-\perspective] X_1 \times_{S_1} Y_1 \ar{rr}\ar{dd}\ar{dl} &[\perspective] &[-\perspective] Y_1\vphantom{\times_{S_1}} \ar{dd}\ar{dl} \\[-\perspective] X_1 \ar[crossing over]{rr} \ar{dd} & & S_1 \\[\perspective] & X_2 \times_{S_2} Y_2 \ar{rr} \ar{dl} & & Y_2\vphantom{\times_{S_1}} \ar{dl} \\[-\perspective] X_2 \ar{rr} && S_2 \ar[from=uu,crossing over] \end{tikzcd}$
\setlength{\perspective}{11pt}
$\begin{tikzcd}[row sep={40,between origins}, column sep={40,between origins}] &[-\perspective] X_1 \times_{S_1} Y_1 \ar{rr}\ar{dd}\ar{dl} &[\perspective] &[-\perspective] Y_1\vphantom{\times_{S_1}} \ar{dd}\ar{dl} \\[-\perspective] X_1 \ar[crossing over]{rr} \ar{dd} & & S_1 \\[\perspective] & X_2 \times_{S_2} Y_2 \ar{rr} \ar{dl} & & Y_2\vphantom{\times_{S_1}} \ar{dl} \\[-\perspective] X_2 \ar{rr} && S_2 \ar[from=uu,crossing over] \end{tikzcd}$
\end{document}


Actually, you should even rotate it a bit into isometric view. I guess, there are other solutions for TikZ around. But just to be complete:

% arara: pdflatex

\documentclass[twocolumn]{article}
\usepackage{tikz-cd}
\newlength{\perspective}

\begin{document}
\setlength{\perspective}{12pt}
\def\isofactor{0.5}
$\begin{tikzcd}[row sep={38,between origins}, column sep={38,between origins}] &[-\perspective] X_1 \times_{S_1} Y_1 \ar{rrd}\ar{dddd}\ar{ddl} &[\perspective] &[-\perspective] \\[-38+\isofactor\perspective] & & & Y_1\vphantom{\times_{S_1}} \ar{dddd}\ar{ddl} & \\[-\perspective-\isofactor\perspective] X_1 \ar[crossing over]{rrd} \ar{dddd} & & & \\[-38+\isofactor\perspective] & & S_1 \\[\perspective-\isofactor\perspective] & X_2 \times_{S_2} Y_2 \ar{rrd} \ar{ddl} & & \\[-38+\isofactor\perspective] & & & Y_2\vphantom{\times_{S_1}} \ar{ddl} \\[-\perspective-\isofactor\perspective] X_2 \ar{rrd} & & \\[-38+\isofactor\perspective] & & S_2 \ar[from=uuuu,crossing over] & \end{tikzcd}$
\setlength{\perspective}{10pt}
\def\isofactor{1}
$\begin{tikzcd}[row sep={38,between origins}, column sep={38,between origins}] &[-\perspective] X_1 \times_{S_1} Y_1 \ar{rrd}\ar{dddd}\ar{ddl} &[\perspective] &[-\perspective] \\[-38+\isofactor\perspective] & & & Y_1\vphantom{\times_{S_1}} \ar{dddd}\ar{ddl} & \\[-\perspective-\isofactor\perspective] X_1 \ar[crossing over]{rrd} \ar{dddd} & & & \\[-38+\isofactor\perspective] & & S_1 \\[\perspective-\isofactor\perspective] & X_2 \times_{S_2} Y_2 \ar{rrd} \ar{ddl} & & \\[-38+\isofactor\perspective] & & & Y_2\vphantom{\times_{S_1}} \ar{ddl} \\[-\perspective-\isofactor\perspective] X_2 \ar{rrd} & & \\[-38+\isofactor\perspective] & & S_2 \ar[from=uuuu,crossing over] & \end{tikzcd}$
\vfill\break
\setlength{\perspective}{8pt}
\def\isofactor{1.5}
$\begin{tikzcd}[row sep={38,between origins}, column sep={38,between origins}] &[-\perspective] X_1 \times_{S_1} Y_1 \ar{rrd}\ar{dddd}\ar{ddl} &[\perspective] &[-\perspective] \\[-38+\isofactor\perspective] & & & Y_1\vphantom{\times_{S_1}} \ar{dddd}\ar{ddl} & \\[-\perspective-\isofactor\perspective] X_1 \ar[crossing over]{rrd} \ar{dddd} & & & \\[-38+\isofactor\perspective] & & S_1 \\[\perspective-\isofactor\perspective] & X_2 \times_{S_2} Y_2 \ar{rrd} \ar{ddl} & & \\[-38+\isofactor\perspective] & & & Y_2\vphantom{\times_{S_1}} \ar{ddl} \\[-\perspective-\isofactor\perspective] X_2 \ar{rrd} & & \\[-38+\isofactor\perspective] & & S_2 \ar[from=uuuu,crossing over] & \end{tikzcd}$
\setlength{\perspective}{6pt}
\def\isofactor{2}
$\begin{tikzcd}[row sep={38,between origins}, column sep={38,between origins}] &[-\perspective] X_1 \times_{S_1} Y_1 \ar{rrd}\ar{dddd}\ar{ddl} &[\perspective] &[-\perspective] \\[-38+\isofactor\perspective] & & & Y_1\vphantom{\times_{S_1}} \ar{dddd}\ar{ddl} & \\[-\perspective-\isofactor\perspective] X_1 \ar[crossing over]{rrd} \ar{dddd} & & & \\[-38+\isofactor\perspective] & & S_1 \\[\perspective-\isofactor\perspective] & X_2 \times_{S_2} Y_2 \ar{rrd} \ar{ddl} & & \\[-38+\isofactor\perspective] & & & Y_2\vphantom{\times_{S_1}} \ar{ddl} \\[-\perspective-\isofactor\perspective] X_2 \ar{rrd} & & \\[-40+\isofactor\perspective] & & S_2 \ar[from=uuuu,crossing over] & \end{tikzcd}$
\end{document}


• Thank you! This is a nice alternative. What does 'between origins' mean? Seems to be quite important in this solution, since otherwise the cube becomes non-parallel shaped again. Commented Mar 20, 2015 at 9:41
• @MartinBrandenburg You are welcome. The between origins set the row sep or column sep between the very centres of the nodes/cells. Like this, they will behave like corners of a real cube. The default is to set the separator in between the borders. Just plot A & BBBBBBBBB & C & D and you will see the difference for between borders and between origins. Commented Mar 20, 2015 at 9:53
• Ah, that is an interesting option! Commented Mar 20, 2015 at 10:41
• Sorry for necro-posting but I'm very interested in understanding how to make these isometric diagrams. Is there any resource I can read to understand the code used? I'm trying to fiddle with it but I don't understand the role of [-38 +\isofactor\perspective] and similar... Commented May 3, 2022 at 2:20
• Hi @Riccardo, it is just a bit of geometry. Both values are numbers which are used as distances. So for example I substract the default distance between two rows (which was 38 in my example above) and instead add up my perspective value times the isofactor value. Like this, I was able to redefine my perspective and isofactor for each cube and to play around with it until it cosmetically fits my needs... Just take one of my cubes, redefine one of those factors and you will see how the cube gets rotated around X or Z axis. Commented May 9, 2022 at 7:43

You can play with start anchor and end anchor.

MWE:

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[row sep=25,column sep=0,nodes={inner sep=2pt}, cells={nodes={align=center,text width=\widthof{X_2 \times_{S_2} Y_2}}}] & X_1 \times_{S_1} Y_1 \ar[end anchor={[xshift=15pt]}]{rr} \ar{dd} \ar{dl} & & Y_1 \ar{dd} \ar{dl} \\ X_1 \ar[crossing over,start anchor={[xshift=-15pt]},end anchor={[xshift=15pt]}]{rr} \ar{dd} & & S_1 \\ & X_2 \times_{S_2} Y_2 \ar[end anchor={[xshift=15pt]}]{rr} \ar{dl} & & Y_2 \ar{dl} \\ X_2 \ar[start anchor={[xshift=-15pt]},end anchor={[xshift=15pt]}]{rr} && S_2 \ar[from=uu,crossing over] \end{tikzcd}
\end{document}


• @MartinBrandenburg You're welcome Commented Mar 20, 2015 at 9:23