0

I want to draw a stack of boxes in TikZ where the boxes get their color from a numbered list of colors. This question explains how to do a numbered list of colors with different colored text. I tried to use it in my solution, but I get the error from xcolor "undefined color". The colors in the list are defined and work fine outside of the script, it's the calling of the function that doesn't work.

The end goal here is to make the boxes like a stair, if you are wondering about some other remnants in the example. (full disclosure, I keep most of this in my own document class in different places so I am not entirely sure I copy-pasted all of the relevant code but it should be close to a MWE).

Minimum example (that currently doesn't work):

\documentclass{report}

\usepackage{xparse,xpatch,xcolor,tikz}
\usetikzlibrary{decorations.text, arrows.meta,calc,shadows.blur,shadings}
\usetikzlibrary{shapes.geometric, arrows}

\ExplSyntaxOn
\prop_new:N \g_aloui_colors_prop
\NewDocumentCommand{\setdocumentcolors}{m}
{
    \prop_gset_from_keyval:Nn \g_aloui_colors_prop { #1 }
}
\NewDocumentCommand{\ovc}{m}
{
    \prop_item:Nn \g_aloui_colors_prop { #1 }
}
\ExplSyntaxOff

\setdocumentcolors{
    1=yellow,
    2=orange,
    3=red,
}

\begin{document}

\newcommand{\ovstair}[2]
{
    \begin{tikzpicture}[
        box/.style={
            draw,
            minimum height=1.3cm,
            minimum width=\textwidth/#1,
            text width=13cm
        }
        ]
        % Draw baseline
        \node [minimum height=0cm, minimum width=\textwidth,fill=white] (boxName0) {};
        % Draw steps
        \foreach \bbt [count=\bbn from 1] in {#2}
        {
            \pgfmathsetmacro{\prevbox}{int(\bbn-1)};
            \node [box,fill=\ovc{\bbn},above of= boxName\prevbox] (boxName\bbn) {\bbt};
        }
    \end{tikzpicture}
}

\ovstair{3}{
    One,
    Two,
    Three,
}

\end{document}

2 Answers 2

2

Maybe something like this.

coloured stairs

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{ifthen}

\begin{document}

    \newcommand{\stair}[1]
        {
        \begin{tikzpicture}[
            staircase/.style={
                above left,
                fill=\col,
                minimum width=\y cm,
                minimum height=1cm,
                text width=\y cm,
                align=left,
                text=white,
                inner xsep=5pt}]
                
            \foreach \stairword [count=\y from 1] in {#1}
                {
                \pgfmathtruncatemacro{\colnum}{int(Mod(\y,5))}

                \ifthenelse{\colnum=0}{\def\col{olive}}{}
                \ifthenelse{\colnum=1}{\def\col{pink}}{} % first one on top
                \ifthenelse{\colnum=2}{\def\col{violet}}{}
                \ifthenelse{\colnum=3}{\def\col{orange}}{}
                \ifthenelse{\colnum=4}{\def\col{purple}}{}

                \node[staircase] at (0,-\y cm) {\stairword};
                }
        \end{tikzpicture}
        }


        
    \stair{%
        One,
        Two,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Eleven}

\end{document}

I used here something I made for a coloured pyramid of cubes. The interesting part is that you don't have to worry about the number of staircases, since you only have 5 looping colours, thanks to the int(Mod(\y,5)) command. You can of course change that, use more or less coulours and change their order.

EDIT

In order to align the boxes to the right, you just have to modifiy the staircase style, replacing above left by above right. I edited the code and the image.

3
  • Thanks, but I already posted an ifthenelse-solution and said it is not what I am looking for. Also, align=right doesn't work to align boxes on the right side. Cool trick with using varable from the for-loop in the style though, I didn't know that was possible. That trick helps a bit to clean up code. Apr 3, 2021 at 20:53
  • OK, even if it was not that clear that you didn't want something based on ifthen. I edited my answer to let you a solution with the boxes aligned right, which is what you didn't manage to achieve in the first place. I'll try and understand what you want exactly, even if I don't understand why it's so important, when you already have something working ;)
    – SebGlav
    Apr 3, 2021 at 21:46
  • My question is about color selection from a numbered list. Ifthenelse-statements are not numbered lists. My reason for being particular about the method is that I expect to be able to reuse this in other types of numbered lists, once I figure out how to do them. But thanks for the alignment solution. :) Apr 6, 2021 at 6:28
0

Well, I worked out a way to do it that is a lot uglier than what I set out to do, but it works. So I am posting it as a solution but I am not accepting it as an answer.

\documentclass{report}

\usepackage{xparse,xpatch,xcolor,tikz}
\usepackage{pgfmath}
\usepackage{xifthen}
\usetikzlibrary{decorations.text, arrows.meta,calc,shadows.blur,shadings}
\usetikzlibrary{shapes.geometric, arrows, positioning}

\definecolor{ovgreen}{RGB}{168, 173, 0}
% Mörkgrön
\definecolor{ovdarkgreen}{RGB}{106, 119, 42}
% Gul
\definecolor{ovyellow}{RGB}{234, 170, 0}
% Orange
\definecolor{ovorange}{RGB}{234, 118, 0}
% Blå
\definecolor{ovblue}{RGB}{0, 76, 151}
% Lila
\definecolor{ovpurple}{RGB}{122, 33, 130}
% Röd
\definecolor{ovred}{RGB}{186, 12, 47}
% Mörkröd
\definecolor{ovdarkred}{RGB}{154, 25, 21}
% Turkos
\definecolor{ovturqoise}{RGB}{10, 148, 164}
% Brun
\definecolor{ovbrown}{RGB}{155, 144, 123}


\ExplSyntaxOn
\prop_new:N \g_aloui_colors_prop
\NewDocumentCommand{\setdocumentcolors}{m}
{
    \prop_gset_from_keyval:Nn \g_aloui_colors_prop { #1 }
}
\NewDocumentCommand{\ovc}{m}
{
    \prop_item:Nn \g_aloui_colors_prop { #1 }
}
\ExplSyntaxOff

\setdocumentcolors{
    1=yellow,
    2=orange,
    3=red,
}

\begin{document}

\newcommand{\ovcolnum}[1]
{
    \ifthenelse{#1 = 1}
    {\colorlet{boxcolor}{ovdarkred}}
    {%false
        \ifthenelse{#1 = 2}
        {\colorlet{boxcolor}{ovred}}
        {%false
            \ifthenelse{#1 = 3}
            {\colorlet{boxcolor}{ovorange}}
            {%false
                \ifthenelse{#1 = 4}
                {\colorlet{boxcolor}{ovyellow}}
                {%false
                    \ifthenelse{#1 = 5}
                    {\colorlet{boxcolor}{ovbrown}}
                    {%false
                        \ifthenelse{#1 = 6}
                        {\colorlet{boxcolor}{ovdarkgreen}}
                        {%false
                            \ifthenelse{#1 = 7}
                            {\colorlet{boxcolor}{ovgreen}}
                            {%false
                                \ifthenelse{#1 = 8}
                                {\colorlet{boxcolor}{ovturqoise}}
                                {%false
                                    \ifthenelse{#1 = 9}
                                    {\colorlet{boxcolor}{ovblue}}
                                    {%false
                                        \colorlet{boxcolor}{ovpurple}
                                    }   
                                }   
                            }   
                        }   
                    }   
                }   
            }   
        }
    }
}

\newcommand{\ovstair}[2]
{
    \begin{tikzpicture}[
        box/.style={
            draw,
            minimum height=1.3cm,
            node distance = 0.65cm
        }
        ]
        % Draw baseline
        \node [minimum height=0cm, minimum width=\textwidth,text width=\textwidth,fill=white] (boxName0) {};
        % Draw steps
        \foreach \bbt [count=\bbn from 1] in {#2}
        {
            \pgfmathsetmacro{\prevbox}{int(\bbn-1)};
            \ovcolnum{\bbn};
            \pgfmathsetmacro{\boxwidth}{\textwidth/#1*\bbn};
            \node [box,fill=boxcolor,text width=\boxwidth,below of= boxName\prevbox,right=\boxwidth/2 cm of boxName\prevbox,anchor=north east] (boxName\bbn) {{\LARGE \textcolor{white}{\bbt}}};
        }
    \end{tikzpicture}
}

\ovstair{3}{
    One,
    Two,
    Three
}

\ovstair{10}{
    One,
    Two,
    Three,
    Four, Five, Six, Seven, Eight, Nine, Ten
}

\end{document}

Like I said, I intended to make a stairway, and it looks like this picture below when I run the code above. For the life of me I cannot get the right edges of the boxes to align so if anyone stumbles on this and know how to right align boxes give me a hint please.

Colored staircase, 3 or 10 steps

2
  • What's wrong with the provided and updated answer? Apr 4, 2021 at 4:34
  • It isn't using a numbered list, like I asked for how to do. Apr 6, 2021 at 6:46

You must log in to answer this question.

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