4

The following code from this example (Plane partition by Jang Soo Kim) has 2 faults, I could not figure out how to overcome them.

Firsty, the value 0 equals to value 2; i.e. value 0 draws 2 cubes.

Secondly, it does not allow for drawing a void cube position, i.e. to generate the second drawing.

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}% to suppresses (hide) navigation symbols bar
\usepackage{tikz}
\usepackage{verbatim}
% Three counters
\newcounter{x}
\newcounter{y}
\newcounter{z}
% The angles of x,y,z-axes
\newcommand\xaxis{210}
\newcommand\yaxis{-30}
\newcommand\zaxis{90}
% The top side of a cube
\newcommand\topside[3]{
  \fill[fill=yellow, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
  shift={(\zaxis:#3)}] (0,0) -- (30:1) -- (0,1) --(150:1)--(0,0);
}
% The left side of a cube
\newcommand\leftside[3]{
  \fill[fill=green, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
  shift={(\zaxis:#3)}] (0,0) -- (0,-1) -- (210:1) --(150:1)--(0,0);
}
% The right side of a cube
\newcommand\rightside[3]{
  \fill[fill=blue, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
  shift={(\zaxis:#3)}] (0,0) -- (30:1) -- (-30:1) --(0,-1)--(0,0);
}
% The cube 
\newcommand\cube[3]{
  \topside{#1}{#2}{#3} \leftside{#1}{#2}{#3} \rightside{#1}{#2}{#3}
}
% Definition of \planepartition
% To draw the following plane partition, just write \planepartition{ {a, b, c}, {d,e} }.
%  a b c
%  d e
\newcommand\planepartition[1]{
 \setcounter{x}{-1}
  \foreach \a in {#1} {
    \addtocounter{x}{1}
    \setcounter{y}{-1}
    \foreach \b in \a {
      \addtocounter{y}{1}
      \setcounter{z}{-1}
      \foreach \c in {1,...,\b} {
        \addtocounter{z}{1}
        \cube{\value{x}}{\value{y}}{\value{z}}
      }
    }
  }
}
\begin{document} 
\begin{tikzpicture}
\planepartition{{2,1,2,1,2,1,2},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{2,1,2,1,2,1,2}}%1st column from back to front{row1,... from left to right}%0 is the same as 2% it does not allow void for 0
\end{tikzpicture}
\end{document} 

enter image description here enter image description here

2
  • 1
    It would be better to indicate in the title that you want to modify the code of the example Plane partition by Jang Soo Kim from "texample.net" so that other users wishing to correct this same code can find your question.
    – AndréC
    Commented Jan 20, 2019 at 5:11
  • @AndréC Thank you for your comment. How can I do it in this post.
    – Hany
    Commented Jan 20, 2019 at 6:00

1 Answer 1

3

Please note that there is a follow-up question here...

There's only one mistake (and not 2 faults) in the \foreach loop. Indeed when it enters the third loop with \b=0

\foreach \c in {1,..., \b} 

the foreach becomes \c in {1,0} which corresponds to 2 iterations.

All you have to do is place a test to prevent iterations when \b = 0.

\newcommand\planepartition[1]{
 \setcounter{x}{-1}
  \foreach \a in {#1} {
        \addtocounter{x}{1}
        \setcounter{y}{-1}
        \foreach \b in \a {
            \addtocounter{y}{1}
            \setcounter{z}{-1}
            \ifnum \b>0
            \foreach \c in {1,...,\b} {
                \addtocounter{z}{1}
                \cube{\value{x}}{\value{y}}{\value{z}}
      }\fi
    }
  }
}

cubes

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}% to suppresses (hide) navigation symbols bar
\usepackage{tikz}
\usepackage{verbatim}
% Three counters
\newcounter{x}
\newcounter{y}
\newcounter{z}
% The angles of x,y,z-axes
\newcommand\xaxis{210}
\newcommand\yaxis{-30}
\newcommand\zaxis{90}
% The top side of a cube
\newcommand\topside[3]{
  \fill[fill=yellow, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
  shift={(\zaxis:#3)}] (0,0) -- (30:1) -- (0,1) --(150:1)--(0,0);
}
% The left side of a cube
\newcommand\leftside[3]{
  \fill[fill=green, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
  shift={(\zaxis:#3)}] (0,0) -- (0,-1) -- (210:1) --(150:1)--(0,0);
}
% The right side of a cube
\newcommand\rightside[3]{
  \fill[fill=blue, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
  shift={(\zaxis:#3)}] (0,0) -- (30:1) -- (-30:1) --(0,-1)--(0,0);
}
% The cube 
\newcommand\cube[3]{
  \topside{#1}{#2}{#3} \leftside{#1}{#2}{#3} \rightside{#1}{#2}{#3}
}
% Definition of \planepartition
% To draw the following plane partition, just write \planepartition{ {a, b, c}, {d,e} }.
%  a b c
%  d e
\newcommand\planepartition[1]{
 \setcounter{x}{-1}
  \foreach \a in {#1} {
        \addtocounter{x}{1}
        \setcounter{y}{-1}
        \foreach \b in \a {
            \addtocounter{y}{1}
            \setcounter{z}{-1}
            \ifnum \b>0
            \foreach \c in {1,...,\b} {
                \addtocounter{z}{1}
                \cube{\value{x}}{\value{y}}{\value{z}}
      }\fi
    }
  }
}
\begin{document} 
\begin{tikzpicture}
\planepartition{{2,1,2,1,2,1,2},{1,0,0,0,0,0,1},{1,0,0,0,0,0,1},{1,0,0,0,0,0,1},{2,1,2,1,2,1,2}}
\end{tikzpicture}
\end{document} 
3
  • 1
    Thank you very much for your answer. I also found that adding , opacity=.7 in the \fill options gives a good effect.
    – Hany
    Commented Jan 20, 2019 at 6:15
  • This creates a transparent effect as if the sides were made of coloured glass. That's nice.
    – AndréC
    Commented Jan 20, 2019 at 6:18
  • Please refer to my question tex.stackexchange.com/questions/470983/…
    – Hany
    Commented Jan 20, 2019 at 6:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .