我想在 TikZ 中绘制一堆盒子,其中盒子的颜色来自编号的颜色列表。这个问题解释如何使用不同颜色的文本制作编号的颜色列表。我尝试在我的解决方案中使用它,但我收到 xcolor 的错误“未定义颜色”。列表中的颜色已定义,并且在脚本之外可以正常工作,但函数调用不起作用。
如果您对示例中的其他一些残余部分感到好奇,那么这里的最终目标是使盒子像楼梯一样。(全面披露,我将其中大部分内容保存在不同位置的我自己的文档类中,因此我不完全确定我复制粘贴了所有相关代码,但它应该接近 MWE)。
最小示例(目前不起作用):
\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}
答案1
也许是这样的。
\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}
我在这里用了我为彩色立方体金字塔制作的东西。有趣的是,您不必担心楼梯的数量,因为借助命令,您只有 5 种循环颜色int(Mod(\y,5))
。当然,您可以更改它,使用更多或更少的颜色并更改它们的顺序。
编辑
为了将框对齐到右侧,您只需修改楼梯样式,将其替换above left
为above right
。我编辑了代码和图像。
答案2
好吧,我想出了一个比我计划的丑陋得多的方法,但它有效。所以我把它作为解决方案发布,但我不接受它作为答案。
\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}
就像我说的,我打算做一个楼梯,当我运行上面的代码时,它看起来就像下面的图片一样。我无论如何都无法让盒子的右边缘对齐,所以如果有人偶然发现这一点并知道如何右对齐盒子,请给我提示。