How to calculate number of symbols in chapter heading?

Is there a simple way to calculate number of symbols in headings?

I'm planning to change color and size of every even to red 16pt and every odd to blue 10pt.

Like this one made in word:

Or maybe there is another way to do it?

As I see now, I need to use some kind of for loop to work my problem out, but how it should be done using "word" as array of symbols I do not know. For a loop I would need some condition to test whether the loop should terminate. In object oriented programming this would be something like

for (i <= word.length) do
alternate color and size


But I have no idea if there is a way in TeX to get something like word.length.

• I am not sure what you mean. Do you just want to know, if the current page is even or odd? – Johannes_B Feb 11 '14 at 18:21
• could you provide a mock-up of your desired output? – cmhughes Feb 11 '14 at 18:37
• Please, edit your question accordingly. You ask about "number of symbols" in the text, but you obviously want to colour the text in some strange way. There's no calcularion involved, only letter manipulation. If you make the question clear, the [on hold] label can be retracted. – yo' Feb 11 '14 at 19:46
• You don't need to know # of characters to alternate font and color. Iterate - yes. Calculate total - no. – ajeh Feb 11 '14 at 19:58
• I voted for reopening, but I'm asking myself what use can be made of this weird output. – egreg Feb 11 '14 at 21:15

2 Answers

Here it is. Please don't use it except for advertising (and think at least twice before doing it).

\documentclass{article}
\usepackage{lmodern}
\usepackage{color}
\makeatletter
\newcommand\weird[1]{%
\@tempswatrue
\@tfor\next:=#1\do{%
\if@tempswa
\@tempswafalse
{\fontsize{16}{0}\selectfont\color{red}\next}%
\else
\@tempswatrue
{\color{blue}\next}%
\fi
}%
}
\makeatother
\begin{document}
\weird{INTRODUCTION}
\end{document}


This is my solution:

\documentclass[12pt,a4paper]{article}
\usepackage[T2A,T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian,swedish,french]{babel}
\usepackage{fix-cm}
\usepackage{xstring}
\usepackage{ifthen}
\usepackage{xcolor}
\usepackage{pgfkeys}
\newcommand{\setvalue}[1]{\pgfkeys{/variables, #1}}
\newcommand{\getvalue}[1]{\pgfkeysvalueof{/variables/#1}}
\newcommand{\declare}[1]{%
\pgfkeys{
/variables/#1.is family,
/variables/#1.unknown/.style = {\pgfkeyscurrentpath/\pgfkeyscurrentname/.initial = ##1}
}%
}

\declare{}

\usepackage{pgffor}
\begin{document}
\setvalue{var = THANKS TO EVERYBODY...}
\StrLen{\getvalue{var}}[\varL]

\foreach \m in {1,...,\varL} {\ifthenelse{\isodd{\m}}
{\fontsize{20pt}{0}\selectfont\textcolor{red}{\StrChar{\getvalue{var}}{\m}}}   {\fontsize{16pt}{0}\selectfont\textcolor{blue}{\StrChar{\getvalue{var}}{\m}}}}
\end{document}