最初的问题
我可能问错了问题,或者表述不正确,但这是我的目标:我希望 tikz 矩形的颜色在两种不同颜色之间交替。最终目标是使用 6 种颜色,但我试图从我的示例中消除所有复杂性。每次绘制框架时,我都希望颜色发生变化。为了获得该结果,我有以下 MWE:
\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{xcolor}
\newcounter{Color}
\setcounter{Color}{1}
\definecolor{c1}{RGB}{58, 116, 182}
\definecolor{c2}{RGB}{83, 166, 219}
\tikzset{
check Color count/.code={
\ifthenelse{\value{\theColor}=1}{\tikzset{draw = c1}}{\tikzset{draw = c2}}
}
}
\newcommand{\textframe}{%
\begin{tikzpicture}%
\node[check Color count, rectangle, line width = 5pt] (r) {TEXT};%
\end{tikzpicture}%
}
\begin{document}
\textframe
\setcounter{Color}{2}
\textframe
\end{document}
这不起作用,我很确定这与扩展有关,但我不知道如何修复代码。请注意,我确实参考了TikZ 节点属性内的宏作为一种可能的解决方案(这就是我最终得到这个 MWE 的原因)。
最终解决方案
所选答案让我找到了最终解决方案,它比我最初的尝试简单得多(我最初的尝试涉及嵌套宏)。这使我可以在整个文档中使用文本框架,而不必担心指定颜色,因为每次使用时颜色都会自动递增。
\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{xcolor}
\newcounter{Color}
\setcounter{Color}{1}
\definecolor{c1}{RGB}{58, 116, 182}
\definecolor{c2}{RGB}{83, 166, 219}
\definecolor{c3}{RGB}{152, 95, 85}
\definecolor{c4}{RGB}{220, 80, 65}
\definecolor{c5}{RGB}{175, 49, 52}
\definecolor{c6}{RGB}{232, 152, 63}
\newcommand{\nextColor}{%
\stepcounter{Color}%
\ifthenelse{\theColor>6}%
{\setcounter{Color}{1}}%
{}%
}
\tikzset{%
get Color/.code={%
\tikzset{draw = c\theColor}%
}%
}
\newcommand{\textframe}[1]{%
\begin{tikzpicture}%
\node[get Color, rectangle, line width = 5pt] (r) {#1};%
\end{tikzpicture}%
\nextColor%
}
\begin{document}
\textframe{TEXT COLOR c\theColor}\par
\textframe{TEXT COLOR c\theColor}\par
\textframe{TEXT COLOR c\theColor}\par
\textframe{TEXT COLOR c\theColor}\par
\textframe{TEXT COLOR c\theColor}\par
\textframe{TEXT COLOR c\theColor}\par
\end{document}
答案1
欢迎来到 TeX.SE!
我猜你正在寻找这样的东西:
\documentclass{article}
\usepackage{tikz}
\definecolor{c1}{RGB}{58, 116, 182}
\definecolor{c2}{RGB}{83, 166, 219}
% more color definitions
\newcommand{\textframe}[2]{%
\begin{tikzpicture}[baseline=-0.7ex]%
\node (n1) [draw=#1, very thick, node contents={#2}];
\end{tikzpicture}%
}
\newcounter{CLR}
\setcounter{CLR}{1}
\begin{document}
\textframe{c\theCLR}{TEXT 1}
\setcounter{CLR}{2}
\textframe{c\theCLR}{TEXT 2}
\bigskip
\foreach \i [count=\j] in {word 1, word 2}
{
\textframe{c\j}{\i}
}
\end{document}
答案2
一种更简单的方法是仅定义样式的数量
\tikzset{Style 1/.style={draw = c1}}
\tikzset{Style 2/.style={draw = c2}}
etc...
您需要并使用它们:
\node[Style \arabic{Color}, My Rectangle] (r) {TEXT};%
代码:
\documentclass{article}
\usepackage{tikz}
\newcounter{Color}
\setcounter{Color}{1}
\definecolor{c1}{RGB}{58, 116, 182}
\definecolor{c2}{RGB}{83, 166, 219}
\tikzset{My Rectangle/.style={rectangle, line width = 5pt}}
\tikzset{Style 1/.style={draw = c1}}
\tikzset{Style 2/.style={draw = c2}}
\tikzset{Style 3/.style={draw = red}}
\tikzset{Style 4/.style={draw = blue}}
\tikzset{Style 5/.style={draw = orange}}
\tikzset{Style 6/.style={draw = magenta}}
\newcommand{\textframe}{%
\begin{tikzpicture}%
\node[Style \arabic{Color}, My Rectangle] (r) {TEXT};%
\end{tikzpicture}%
}
\begin{document}
\foreach \x in {1,...,6} {%
\setcounter{Color}{\x}%
\textframe\space
}
\end{document}