13

I wrote a snippet of code to get a beautiful cuboid but it looks a little distorted. I'd be glad if someone can infuse beauty into the output.

(I am sorry if the question is too localised; feel free to (vote to) close if you consider so.)

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}
\newcommand{\Z}{\mathbb Z}
\begin{document}
\begin{tikzpicture}
 \matrix (m) [matrix of math nodes, row sep=0.5em,
              column sep=2em]{
      &\Z/30\Z& \\
\Z/15\Z&&\\
       &\Z/10\Z& \\
       &&\Z/6\Z \\
\Z/5\Z&& \\
      &\Z/3\Z& \\
      &&\Z/2\Z \\
      &\{1\}& \\};
 \path[-]
    (m-1-2) edge (m-2-1) 
    edge (m-3-2)
    edge (m-4-3)
    (m-3-2) edge (m-5-1)
    (m-2-1) edge [-,line width=6pt,draw=white](m-6-2)
    edge (m-6-2)
    (m-2-1) edge (m-5-1)
    (m-8-2) edge (m-5-1)
    edge (m-6-2)
    edge (m-7-3)
    (m-7-3) edge (m-4-3)
    (m-3-2) edge (m-7-3)
    (m-4-3) edge [-,line width=6pt,draw=white](m-6-2)
    edge (m-6-2);
\end{tikzpicture}
\end{document}

enter image description here

2
  • 2
    I think this will point you in the right direction: texample.net/tikz/examples/commutative-diagram
    – Martin H
    Jun 27, 2012 at 12:41
  • @Martin I am not sure, it does, now. That is where I learnt to do what I have done. There are two other parameters apart from the two in the code to tweak. My intuition suggests no combination of the four would solve the problem as the parallel edges are not of the same length.
    – kan
    Jun 27, 2012 at 12:50

5 Answers 5

11

It is often overlooked that while TikZ doesn't do “true” three-dimensional graphics, it does have an xyz coordinate system that comes in very handy for drawing cubes. Just tweak the coordinate unit options to adjust the view to your liking.

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{amssymb}
\newcommand{\Z}{\mathbb Z}

\begin{document}

\begin{tikzpicture}[
        x={(4em,4em)},
        y={(-6em,8em)},
        z={(0,5em)}]
    \node (b1) at (0,0,0) {$\{1\}$};
    \node (b2) at (1,0,0) {$\Z/2\Z$};
    \node (b3) at (1,1,0) {$\Z/10\Z$};
    \node (b4) at (0,1,0) {$\Z/5\Z$};
    \node (t1) at (0,0,1) {$\Z/3\Z$};
    \node (t2) at (1,0,1) {$\Z/6\Z$};
    \node (t3) at (1,1,1) {$\Z/30\Z$};
    \node (t4) at (0,1,1) {$\Z/15\Z$};

    \draw (b1) -- (b2) -- (b3) -- (b4) -- (b1);
    \draw[preaction={draw,white,line width=5pt}] (t1) -- (t2) -- (t3) -- (t4) -- (t1);
    \foreach \x in {1,...,4} {
        \draw (b\x) -- (t\x);
    }
\end{tikzpicture}
\end{document}

result

6
  • 2
    Haha, that's neat.
    – percusse
    Jun 27, 2012 at 15:54
  • Another simple and elegant answer! +1.
    – kan
    Jun 27, 2012 at 16:14
  • Ah, this is really nice! @KannappanSampath: Maybe it would be a good idea to accept this answer instead of mine, so others coming across this question can recognise more easily that this way is actually much better than using a matrix?
    – Jake
    Jun 27, 2012 at 16:22
  • @Jake Done. But, I should once again thank you for I learnt quite a bot from your answer as well.
    – kan
    Jun 27, 2012 at 16:38
  • 1
    Sorry for my hastely edit. It's due to the fact that the (b#) are not coordinates but nodes so every line is a independent path hence cycle actually closes but by drawing the last line twice. If you add .center to all node names then it works but I missed it before I edit.
    – percusse
    Jun 27, 2012 at 17:25
11

You'll need to add nodes to the empty cells to get the spacing right.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\def\Z{Z}
\begin{tikzpicture}
 \matrix (m) [execute at empty cell={\node{\phantom{\Z}};},
    matrix of math nodes, nodes={inner xsep=0pt},row sep=0.5em,
              column sep=3em]{
        &   \Z/30\Z & \\
        &           & \\
\Z/15\Z &           &\\
        &\Z/10\Z    & \\
        &           &\Z/6\Z \\
\Z/5\Z  &           & \\
        &\Z/3\Z     & \\
        &           &\Z/2\Z \\
        &           & \\
        &\{1\}      & \\};
 \path[-]
    (m-1-2) edge (m-3-1) 
    edge (m-4-2)
    edge (m-5-3)
    (m-4-2) edge (m-6-1)
    (m-3-1) edge [-,line width=6pt,draw=white](m-7-2)
    edge (m-7-2)
    (m-3-1) edge (m-6-1)
    (m-10-2) edge (m-6-1)
    edge (m-7-2)
    edge (m-8-3)
    (m-8-3) edge (m-5-3)
    (m-4-2) edge (m-8-3)
    (m-5-3) edge [-,line width=6pt,draw=white](m-7-2)
    edge (m-7-2);
\end{tikzpicture}

\end{document}
3
  • 1
    I am happy with your result. But, the purpose of putting Z/5Z below Z/6Z is that 6>5. Would it be possible to keep the hierarchy and still get a good cuboid? It's fine if I can't. And, thank you for the help. +1.
    – kan
    Jun 27, 2012 at 13:24
  • @KannappanSampath: Ah, I see. I've edited my answer.
    – Jake
    Jun 27, 2012 at 14:00
  • Thank you so much. Looks much better! Thank you once again.
    – kan
    Jun 27, 2012 at 14:08
6

To get a slightly manual but still easy to maintain code, you can define a lower and upper rectangles and place nodes around. The copy/paste makes it easier to handle the code. Of course there is always parts to improve.

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{amsmath}
\usepackage{amssymb}
\newcommand{\Z}{\mathbb Z}
\begin{document}

\begin{tikzpicture}[every node/.style={fill=white}]
%This is to be copied in a scope for the upper layer
\coordinate (b) at (3,2);
\coordinate (c) at (-3,3);
\draw (0,0) -- ++(b) -- ++(c) --++($-1*(b)$)--cycle;
\node (n1) at (0,0)                    {$\{1\}$};
\node (n2) at (b)                      {$\Z/2\Z$};
\node (n3) at ($(b)+(c)$)              {$\Z/10\Z$};
\node (n4) at ($(n3)-(b)$)             {$\Z/5\Z$};
%until here

\begin{scope}[yshift=2cm]
\coordinate (b) at (3,2);
\coordinate (c) at (-3,3);
\draw[preaction={draw,white,line width=5pt}] (0,0) -- ++(b) -- ++(c) -- ++($-1*(b)$)--cycle;
\node (n5) at (0,0)                    {$\Z/3\Z$};
\node (n6) at (b)                      {$\Z/6\Z$};
\node (n7) at ($(b)+(c)$)              {$\Z/30\Z$};
\node (n8) at ($(n7)-(b)$)             {$\Z/15\Z$};
\end{scope}
%The verticals
\foreach \x [count=\xi from 5] in {1,...,4} {
\draw (n\x) -- (n\xi);
}

\end{tikzpicture}
\end{document}

enter image description here

0
6

It's not an answer at the question but another way to draw a graph like a cuboid. I had already some examples in 3D with tkz-graph.

A) Natural version

 \documentclass[11pt]{article}
 \usepackage{tkz-berge}
 \begin{document}
    \usetikzlibrary{calc,3d}
    \newcommand{\setxyz}[1]{%
    \pgfmathsetmacro{\xone}{cos(180+#1)}%
    \pgfmathsetmacro{\yone}{sin(180+#1)}%
    \pgfmathsetmacro{\xtwo}{cos(360-#1)}%
    \pgfmathsetmacro{\ytwo}{sin(360-#1)}%
    }
    \setxyz{17} 

     \begin{tikzpicture}%
    [x = {(\xone cm,\yone cm)},
     y = {(\xtwo cm,\ytwo cm)},
     z = {(0cm,1cm)}] 

    \GraphInit[vstyle=Empty]
        \tikzset{VertexStyle/.style ={shape          = rectangle,
                                      minimum width  = 48pt, 
                                      minimum height = 15pt,
                                      }}   
    \SetVertexNoLabel
    \begin{scope}[canvas is xy plane at z=-5]
        \grCycle[prefix=a,rotation=60,RA=5]{4}  
    \end{scope}
    \begin{scope}[canvas is xy plane at z=0]
        \grEmptyCycle[prefix=b,rotation=60,RA=5]{4} 
    \end{scope}
     \EdgeIdentity*{a}{b}{0,...,3}
     \EdgeInGraphLoop{b}{4}
     \AssignVertexLabel[color = blue,size = \small]{a}{ \{1\},Z/2Z,Z/10Z,Z/5Z}
     \AssignVertexLabel[color = blue,size = \small]{b}{Z/3Z,Z/6Z,Z/30Z,Z/15Z} 
    \end{tikzpicture} 
 \end{document}  

enter image description here
B) With some decorations

 \documentclass[11pt]{article}
 \usepackage{tkz-berge}
 \begin{document}
    \usetikzlibrary{calc,3d}
    \newcommand{\setxyz}[1]{%
    \pgfmathsetmacro{\xone}{cos(180+#1)}%
    \pgfmathsetmacro{\yone}{sin(180+#1)}%
    \pgfmathsetmacro{\xtwo}{cos(360-#1)}%
    \pgfmathsetmacro{\ytwo}{sin(360-#1)}%
    }
    \setxyz{17} 

    \begin{tikzpicture}%
    [x = {(\xone cm,\yone cm)},
     y = {(\xtwo cm,\ytwo cm)},
     z = {(0cm,1cm)}] 

    \GraphInit[vstyle=Shade]
        \tikzset{VertexStyle/.style ={shape        = circle,
                                      shading      = ball, 
                                      ball color   = blue!20,% 
                                      minimum size = 48pt,
                                      draw}}   
    \SetVertexNoLabel
    \begin{scope}[canvas is xy plane at z=-5]
        \grCycle[prefix=a,rotation=60,RA=5]{4}  
    \end{scope}
    \begin{scope}[canvas is xy plane at z=0]
        \grEmptyCycle[prefix=b,rotation=60,RA=5]{4} 
    \end{scope}
     \EdgeIdentity*{a}{b}{0,...,3}
     \EdgeInGraphLoop{b}{4}
     \AssignVertexLabel[color = blue,size = \small]{a}{ \{1\},Z/2Z,Z/10Z,Z/5Z}
     \AssignVertexLabel[color = blue,size = \small]{b}{Z/3Z,Z/6Z,Z/30Z,Z/15Z} 
    \end{tikzpicture}

 \end{document}  

enter image description here

3

run it with latex->dvips->ps2pdf. You can change the viewpoint (x y z) for a different view.

\documentclass{article}
\usepackage{amssymb}
\usepackage{pst-gr3d}
\newcommand\Z{\mathbb{Z}}
\begin{document}

\PstGridThreeD[unit=4cm,viewpoint=0.4 -0.4 0.6,GridThreeDNodes,
  PstPicture=](1,1,1)
\rput*(Gr3dNode111){$\Z/6\Z$}
\rput*(Gr3dNode011){$\Z/30\Z$}
\rput*(Gr3dNode001){$\Z/15\Z$}
\rput*(Gr3dNode101){$\Z/3\Z$}
\rput*(Gr3dNode110){$\Z/2\Z$}
\rput*(Gr3dNode100){$\{1\}$}
\rput*(Gr3dNode010){$\Z/10\Z$}
\rput*(Gr3dNode000){$\Z/5\Z$}

\end{document}

enter image description here

4
  • +1; I appreciate your effort; I am sorry to disappoint you -- please read the comment I left under Jake's answer. (I should have added it in the question; my bad.)
    – kan
    Jun 27, 2012 at 13:34
  • 1
    Nice. I would like to ask you if you are the only one who are responsible for the manuals and the collection of pst-x packages. The reason I ask is that I often spend a lot of time to find the general collection of these packages. And due to my ignorance I tend to crash them when used together. Is there any attempt or a location where these are handled together?
    – percusse
    Jun 27, 2012 at 13:35
  • On this note, I should say that I enjoy tweaking the viewing co-ordinates and guess how it's going to look. Good game for me!
    – kan
    Jun 27, 2012 at 14:09
  • 1
    Among the other available answers, this one used the most minimal numbers of keystrokes. Jun 28, 2012 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.