修正“texample.net”中 Jang Soo Kim 的平面分区示例代码

修正“texample.net”中 Jang Soo Kim 的平面分区示例代码

以下代码来自这个例子 (平面隔断,作者:Jang Soo Kim)有 2 个缺点,我不知道如何克服它们。

首先,值 0 等于值 2;即值 0 绘制 2 个立方体。

其次,它不允许绘制空白立方体的位置,即生成第二个图形。

\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} 

在此处输入图片描述 在此处输入图片描述

答案1

请注意,有一个后续问题这里...

只有一个错误(而不是 2 个错误)\foreach循环中。实际上,当它进入第三个循环时 \b=0

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

foreach 变成\c in {1,0}对应于 2 次迭代。

您所要做的就是进行测试以防止迭代\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
    }
  }
}

立方体

\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} 

相关内容