1

I've been struggling in find the solution for the following problem.

If I want to change the color of each letter in a sentence in latex, how can I do it? Let's say that I want to write "Merry Christmas" using the colors red and green and alternating between them.

I want to define a command and use a forloop to change the color of each letter, but I don't know:

  1. How can I find the number of letters in a sentence (excluding spaces) to give the upper limit for the forloop?

  2. How can I define a string with the two colors to use in the forloop?

So far my silly "solution" is to do it manually, like:

\documentclass[a4paper,12pt]{article}
  \usepackage[utf8x]{inputenc}
  \usepackage{xcolor}

\begin{document}

\textcolor{red}{M}\textcolor{green}{e}\textcolor{red}{r}
\textcolor{green}{r}\textcolor{red}{y}\textcolor{green}{C}
\textcolor{red}{h}\textcolor{green}{r}\textcolor{red}{i}
\textcolor{green}{s}\textcolor{red}{t}\textcolor{green}{m}
\textcolor{red}{a}\textcolor{green}{s}\textcolor{red}{!}

\end{document}

From what I saw I can define an environment and use a loop to alternate between the colors, I don't have a sensilble solution, but it would be something like:

\newenvironment{redgreen}{
  \forloop{WORDNUMBER}{1}{\VALUE{WORDNUMBER} < \LENGHT{WORDNUMBER}}{
    \textcolor{red}{WORDNUMBER}
    \textcolor{green}{WORDNUMBER + 1}
    WORDNUMBER = WORDNUMBER + 2
  }
}

The parts with capital letters I know won't work, I just want to give an example of what I'm willing to do. I want to find a way to get the number of letters in the sentence that I would put inside the redgreen block and somehow use the forloop to change the color of each letter in the sentence alternating between red and green. I'm not sure how the forloop works in latex, so everything in the example above may be trash.

aiaragomes
  • 11
  • 3
  • This question should probably be moved to tex.SE. Don't worry, you don't have to do anything. – Martin Thoma Feb 25 '15 at 13:19
  • You should probably give a minimum example what you did so far and where you are stuck. See https://en.wikibooks.org/wiki/LaTeX/Colors for example. – Martin Thoma Feb 25 '15 at 13:20
  • I tried to explain a bit better where I'm stuck edditing the post. My problem is basically about how I can use the for loop in latex and how to find the number of letters in a sentence. – aiaragomes Feb 25 '15 at 14:28
  • Possible duplicate of http://tex.stackexchange.com/questions/285968/color-coding-a-text-string-with-alternating-colors – samcarter_is_at_topanswers.xyz Dec 21 '16 at 00:46

0 Answers0