0

I'm looking for something like this, where a string of text can be put into a command that alternates the text between colors (preferably three), but the color is per word.

eg, \alternatecolors{blah blah blah} would result in three blahs, the first in red, second green, third blue; if there are more then it repeats from red→green→blue etc.

I am currently using the simple solution of just coloring each word independently but I have a lot of instances of this and am in the process of rewriting the entire document :), so I'm interested if this is actually feasible before I rewrite more.

Thank you!

1
  • What about punctuation? Should it share the color of the word it's attached to?
    – egreg
    Commented Jan 8, 2020 at 22:26

1 Answer 1

4

Welcome! You only need xcolor.

\documentclass{article}
\usepackage{xcolor}
\makeatletter
\def\alternatecolors#1{\colorlet{oldcolor}{.}\my@color%
    \@alternatecolors#1 \@empty
    \color{oldcolor}
}
\def\@alternatecolors#1 #2{%
   #1\my@color\space
   \ifx #2\@empty\else
    \expandafter\@alternatecolors
   \fi
   #2%
}
\newcounter{pft@col}
\newcommand{\my@color}{\ifcase\value{pft@col}%
 \color{red}%
 \stepcounter{pft@col}%
\or
 \color{green}%
 \stepcounter{pft@col}%
\or
 \color{blue}%
 \setcounter{pft@col}{0}%
\fi}
\newcommand{\ResetColor}{\setcounter{pft@col}{0}%
}
\makeatother
\begin{document}
\alternatecolors{blah blah blah} : % 
\alternatecolors{blah blub pft pfft} : %
\alternatecolors{blah blub pft pfft} : %
\alternatecolors{blah blub pft pfft} : %
\alternatecolors{blah blub pft pfft} : \ResetColor%
\alternatecolors{blah blub pft pfft} 
\end{document}

enter image description here

The color cycle gets continued unless one resets it with \ResetColor. Of course, one may also reset it by default if that's needed.

ORIGINAL ANSWER: With soul and xcolor you could do

\documentclass{article}
\usepackage{soul}
\usepackage{xcolor}
\makeatletter
\DeclareRobustCommand*{\alternatecolors}{%
  \SOUL@setup
  \def\SOUL@preamble{\colorlet{oldcolor}{.}\my@color}%
  \def\SOUL@postamble{\color{oldcolor}}%
  \def\SOUL@everyspace##1{##1\my@color\space}%
  \def\SOUL@everytoken{\the\SOUL@token}%
  \SOUL@
}
\newcounter{pft@col}
\newcommand{\my@color}{\ifcase\value{pft@col}%
 \color{red}%
 \stepcounter{pft@col}%
\or
 \color{green}%
 \stepcounter{pft@col}%
\or
 \color{blue}%
 \setcounter{pft@col}{0}%
\fi}
\newcommand{\ResetColor}{\setcounter{pft@col}{0}%
}
\makeatother
\begin{document}
\alternatecolors{blah blah blah} : % 
\alternatecolors{blah blub pft pfft} : %
\alternatecolors{blah blub pft pfft} : %
\alternatecolors{blah blub pft pfft} : %
\alternatecolors{blah blub pft pfft} : \ResetColor%
\alternatecolors{blah blub pft pfft} 
\end{document}
2
  • Hi! Thank you! Is there any way to make this command act like a {\itshape blah} kind of command? I'm using expex and it only takes those kind of commands for some things I think? Also how would one make it automatically reset after the command? I'm kinda new to this kind of depth to commands >.<
    – Maxine
    Commented Jan 30, 2020 at 0:17
  • @Maxine Most likely there is. Yet you have never bothered to accept this answer, which, as I believe, answers the original question. I'd kindly like to ask you to accept this answer if you think it solves your original problem, and ask a new question for your new problem.
    – user194703
    Commented Jan 30, 2020 at 1:02

You must log in to answer this question.

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