3

Initial Question

I may be asking the wrong question, or phrasing it incorrectly, but here is my goal: I would like to have the color of a tikz rectangle alternate between 2 different colors. The ultimate goal is to use 6 colors, but I tried to remove all the complications from my example. Every time I draw the frame, I want the color to change. To get that result, I have the following 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}

This does not work, and I'm pretty sure it has to do with expansion, but I do not know how to fix the code. Note that I did refer to Macro inside TikZ node properties as a possible solution (which is how I ended up with this MWE).

Final solution

The chosen answer led me to a final solution that is much simpler than my initial attempt (my original attempt involved nested macros). This allows me to use text frames throughout my document without worrying about specifying the color, as the color is automatically incremented with every use.

\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}

Text frames with alternating colors

groffl
  • 33

2 Answers2

1

A simpler way is to just define the number of styles

\tikzset{Style 1/.style={draw = c1}}
\tikzset{Style 2/.style={draw = c2}}
etc...

you need and use them:

\node[Style \arabic{Color}, My Rectangle] (r) {TEXT};%

enter image description here

Code:

\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}

Peter Grill
  • 223,288
  • Part of my goal was to make it so I didn't have to specify the color each time (these frames are to be scattered throughout a document), but I didn't specify well. This wasn't the solution for me, but thank you! – groffl Feb 04 '22 at 12:37
1

Welcome to TeX.SE!

I guess, that you looking for something like this:

\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}

enter image description here

Zarko
  • 296,517
  • Thank you! This led me to my chosen solution (simpler than my original though), that I'll add to my question. – groffl Feb 04 '22 at 12:36
  • @groffl, you are welcome. BTW, according to site policy, you can express your thankfulness by upvoting (both) answers . – Zarko Feb 04 '22 at 12:46
  • good to know, and I have done so - thanks again! – groffl Feb 04 '22 at 13:39