7

I am trying to define a command that changes the color of every letter in the whole document to a specified color. I already tried different approaches from this thread, but I was not able to color every letter with any of those.

My best approach looks like this:

\documentclass{article}
\usepackage{xparse,l3regex}
\usepackage{xcolor}

\definecolor{color1}{HTML}{FF0000} %for A and a
\definecolor{color2}{HTML}{00FF00} %for B and b

\ExplSyntaxOn
\NewDocumentCommand{\colorsyn}{ m }
{
\colorsyn:nn { #1 }
}

%defining a new tocken list
\tl_new:N \l_syn_tl 
\cs_new_protected:Npn \colorsyn:nn #1
{
  %setting l_syn_tl to the string-argument
  \tl_set:Nn \l_syn_tl { #1 } 
  %Coloring A and a by replacing them in the string and overriding l_syn_tl with the new string
  \regex_replace_all:nnN { ([A+a]) } { \c{textcolor}\cB\{color1\cE\}\cB\{\1\cE\} } \l_syn_tl
  %Coloring B and b and overriding l_syn_tl again
  \regex_replace_all:nnN { ([B+b]) } { \c{textcolor}\cB\{color2\cE\}\cB\{\1\cE\} } \l_syn_tl
  %Using \l_syn_tl as output
  \tl_use:N \l_syn_tl
}
\ExplSyntaxOff

\begin{document}
\colorsyn{A Baby}
\end{document}

It works unless I try to change the color of more than two letter pairs, by simply adding:

\regex_replace_all:nnN { ([C+c]) } { \c{textcolor}\cB\{color3\cE\}\cB\{\1\cE\} } \l_syn_tl

to the code.

I hope anyone of you guys can help me with that problem or give me an new approach to work with.

Btw, that is what it should look like: Here is what it should look like in the end.

  • 2
    This looks like a job for LuaTeX. BTW, the problem comes from replacing c, which also replaces \textcolor{color1} to \textcolor{\textcolor{color3}{c}olor}. If you define \cs_new_protected_nopar:Npn \__color_three:n { \textcolor{color3} } then it would probably work with the regex replace \c{__color_three:n}\cB\{\1\cE\}. – Manuel Nov 22 '14 at 14:46
  • 1
    If you use LuaTeX you can probably use the chickenize package. – Juri Robl Nov 22 '14 at 15:26
  • 1
    Those colors are all wrong... – Joe Nov 22 '14 at 21:10
  • 1
    There is no "wrong" color when it comes to synesthesia, cause it is an individual phenomenon… if it's that what you meant. – AstronAUT Nov 24 '14 at 20:30
13

enter image description here

the regexp for a and A is [Aa] not [A+a] but also as you were adding commands that had letters like later regexp were altering the commands already added. Simpler just to match all letters with one regex and add a command that adds a colour depending on its argument:

\documentclass{article}
\usepackage{xparse,l3regex}
\usepackage{xcolor}

\definecolor{colorA}{HTML}{FF0000} 
\definecolor{colorB}{HTML}{00FF00} 
\definecolor{colora}{HTML}{00F350} 
\definecolor{colorb}{HTML}{9000DD} 
\definecolor{colory}{HTML}{0011AA} 
% more one for each letter

\def\zz#1{\textcolor{color#1}{#1}}

\ExplSyntaxOn
\NewDocumentCommand{\colorsyn}{ m }
{
\colorsyn:nn { #1 }
}

%defining a new tocken list
\tl_new:N \l_syn_tl 
\cs_new_protected:Npn \colorsyn:nn #1
{
  %setting l_syn_tl to the string-argument
  \tl_set:Nn \l_syn_tl { #1 } 
  %Coloring A and a by replacing them in the string and overriding l_syn_tl with the new string
  \regex_replace_all:nnN { ([a-zA-Z]) } { \c{zz}\cB\{\1\cE\} } \l_syn_tl
   %Using \l_syn_tl as output
  \tl_use:N \l_syn_tl
}
\ExplSyntaxOff

\begin{document}
\colorsyn{A Baby}
\end{document}
  • 1
    @AstronAUT In case this answers your question don't forget to accept it (clicking on the green tick), and upvoting (in case you think the answer deserves it). – Manuel Nov 22 '14 at 15:27
  • Great solutio, unfortunately it doen't work with other characters like , ä ö ü or - -- . Is there a way to use it with those characters and numerals? – jlk Jun 12 '15 at 15:19
  • @harryhaller a-z doesn't match letters with diacritics you should be able to match ä but details may depend on whether you are using xe/luatex (where it is a single token) or utf-8 with inputenc (when it isn't) texdoc l3regex should have the details, but off the top of my head I'm not sure what Bruno did:-) – David Carlisle Jun 12 '15 at 15:36
  • I would be great full, if someone has an idea to solve it in xelatex. – jlk Jun 12 '15 at 15:38
  • 1
    @gschenk paragraphs would be allowed in colorsyn if you add + as in \NewDocumentCommand{\colorsyn}{ +m } but really it's not designed for large chunks such as the whole document – David Carlisle Feb 14 '17 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.