Graphics, Figures & TablesTikZ | Configuration of Example

Information and discussion about graphics, figures & tables in LaTeX documents.
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

TikZ | Configuration of Example

Post by svend_tveskaeg »

@Stefan: I would have if I had seen the answer here before Clemens posted it himself on {TeX} SX. ;)

@Thorsten: You are right. I forgot to link to {TeX} SX; I only remembered to do it the other way around.

@Clemens: Thank you for the solution. It is very nice!

Consider the following MWE:

Code: Select all

\documentclass{article}

\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}{330}
\newcommand{\zaxis}{90}
% Color relativt to layer
\newcommand*{\cubecolors}[1]{%
  \ifcase#1\relax
  \or\colorlet{cubecolor}{green}%
  \or\colorlet{cubecolor}{cyan}%
  \or\colorlet{cubecolor}{yellow}%
  \or\colorlet{cubecolor}{red}%
  \or\colorlet{cubecolor}{purple}%
  \or\colorlet{cubecolor}{blue}%
  \else
     \colorlet{cubecolor}{white}%
  \fi
}
% 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 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{\c}
      \ifthenelse{\c=0}{\setcounter{z}{-1},\addtocounter{y}{0}}{
        \cube{\value{x}}{\value{y}}{\value{z}}}
      }
    }
  }
}

\begin{document} 

\begin{figure}
 \centering
  \begin{tikzpicture}[scale=0.6]
   \planepartition{1}
  \end{tikzpicture}
 \hfill
  \begin{tikzpicture}[scale=0.6]
   \planepartition{{1,1,1},{1,2,1},{1,1,1}}
  \end{tikzpicture}
 \hfill
  \begin{tikzpicture}[scale=0.6]
   \planepartition{{1,1,1,1,1},{1,2,2,2,1},{1,2,3,2,1},{1,2,2,2,1},{1,1,1,1,1}}
  \end{tikzpicture}
\end{figure}

\end{document}
It would be nice if the colours in the three figures were the same in each layer from the top down instead of from the bottom up. I would much appreciate it, how you (or someone else, of course) would show me how to do this.

I tried to change

Code: Select all

\foreach \c in {0,...,\b}
to

Code: Select all

\foreach \c in {\b,...,0}
but that (of course, I now see) just gives all the layers the same colour as the bottom layer.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

TikZ | Configuration of Example

Post by cgnieder »

Not so easy here since the layers are drawn from bottom to top where each layer corresponds to a value. 1 for the first, 2 for the second and so on. Equal numbers mean equal color right now. In order to achieve your goal one would need that the number of the top layer always corresponds to the same color, i.e., 5=green if there are five layers, 3=green if there are 3 layers, 6=green if there are six layers...

I'll try to think of something but won't promise anything.

Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: TikZ | Configuration of Example

Post by svend_tveskaeg »

@Clemens: Thank you very much for giving it a try.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

TikZ | Configuration of Example

Post by cgnieder »

Here's a solution that's working as long as there are no more than 6 layers:

Code: Select all

\documentclass{article}

\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}{330}
\newcommand{\zaxis}{90}
% Color relativt to layer
\newcommand*{\cubecolors}[1]{%
  \ifcase\numexpr#1\relax
  \or\colorlet{cubecolor}{green}%  1
  \or\colorlet{cubecolor}{cyan}%   2
  \or\colorlet{cubecolor}{yellow}% 3
  \or\colorlet{cubecolor}{red}%    4
  \or\colorlet{cubecolor}{purple}% 5
  \or\colorlet{cubecolor}{blue}%   6
  \else
     \colorlet{cubecolor}{white}%  7--
  \fi
}
% 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 of \planepartition
\newcommand{\planepartition}[1]{
  \getheight{#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{6-\top+\c}
        \ifthenelse{\c=0}{\setcounter{z}{-1},\addtocounter{y}{0}}{
            \cube{\value{x}}{\value{y}}{\value{z}}}
      }
    }
  }
}
\makeatletter
\newcount\@top
\newcommand{\getheight}[1]{%
  \gdef\top{0}%
  \foreach \a in {#1} {%
    \pgfmathparse{ max(\a) }\@top=\pgfmathresult
    \ifnum\@top>\top\relax
      \xdef\top{\the\@top}%
    \fi
  }%
}
\makeatother
\begin{document}

\begin{figure}
  \centering
  \begin{tikzpicture}[scale=0.6]
    \planepartition{1}
  \end{tikzpicture}
  \hfill
  \begin{tikzpicture}[scale=0.6]
    \planepartition{{1,1,1},{1,2,1},{1,1,1}}
  \end{tikzpicture}
  \hfill
  \begin{tikzpicture}[scale=0.6]
    \planepartition{{1,1,1,1,1},{1,2,2,2,1},{1,2,3,2,1},{1,2,2,2,1},{1,1,1,1,1}}
  \end{tikzpicture}
\end{figure}

\end{document}
With more than 6 layers one needs to add colors to \cubecolors and adjust the line

Code: Select all

\cubecolors{6-\top+\c}
accordingly.

Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: TikZ | Configuration of Example

Post by svend_tveskaeg »

Very nice.

Thank you so much for helping me!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply