
我正在使用下面的程式碼,它創建不同的顏色層。很難獲得裝飾以使其在三個軸的每一層同時工作。請在這裡提供一些幫助。我希望使用 3 種顏色 a、b、c 透過顏色 a+b+c mod 3 為位置 (a, b, c) 的盒子著色。
程式碼
% Plane partition
% Author: Jang Soo Kim
\documentclass{standalone}
\usepackage[danish]{babel}
\usepackage{ifthenx}
\usepackage{verbatim}
\usepackage{tikz}
% 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=cubecolor, 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=cubecolor, 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=cubecolor, 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 cubecolors
\newcommand*\cubecolors[1]{%
\ifcase#1\relax
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{blue}%
\or\colorlet{cubecolor}{red}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{blue}%
\or\colorlet{cubecolor}{red}%
\else
\colorlet{cubecolor}{white}%
\fi
}
% Definition of \planepartition
\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 {0,...,\b} {
\addtocounter{z}{1}
\cubecolors{\a}
\ifthenelse{\c=0}{\setcounter{z}{-1},\addtocounter{y}{0}}{
\cube{\value{x}}{\value{y}}{\value{z}}}
}
}
}
}
\begin{document}
\begin{tikzpicture}
\planepartition{{5,3,2,2},{4,2,2,1},{3,2,1},{2,1},{1}}
\end{tikzpicture}
\end{document}
答案1
我認為必須有一種方法可以使用 a 進行著色,mod
但我現在不確定如何(在我看來,有點晚了)。但同時,我可以為您提供一種替代方案,允許您擺脫計數器,這很有用,但它仍然是額外的代碼以及包ifthenx
和\ifthenelse
命令,由\ifnum### ... \else ... \fi
.
因此,您可以將\planepartition
命令替換為以下內容:
\newcommand\planepartition[1]{
\foreach \a [count=\x starting from -1] in {#1} {
\foreach \b [count=\y starting from -1] in \a {
\foreach \c [count=\z starting from -1] in {0,...,\b} {
\cubecolors{\a}
\ifnum\c=0
\else
\cube{\x}{\y}{\z}
\fi
}
}
}
}