After reading a bit on random coloring in LaTeX, I had the idea to use randomly colored leaders in a document. I came up with the following piece of code, which was stolen and modified from the \dotfill implementation.

\documentclass{article}

\usepackage{tikz}

\makeatletter
\pgfmathsetseed{1}
\def\qfill{
\hss
\pgfmathparse{rnd}
\textcolor[hsb]{\pgfmathresult, 1.0, 1.0}{?}
}\hfill
\kern \z@
}
\makeatother

\begin{document}

\qfill What will be the answer?\qfill

\end{document}


However, it seems that, for one call of qfill, all the question marks will have the same color, with a new color being randomly selected once per call of qfill, and not once for every ?-character (as intended).

What do I need to change in order to have randomly colored leaders?

• You can not use \leaders you would have to position each box within the macro, \leaders repeats the same box multiple times, which is not configurable from tex macros – David Carlisle Nov 4 '14 at 15:52
• note you are missing % from ends of lines in your definition it will add space to the output as well as the leader dots. – David Carlisle Nov 4 '14 at 15:54

\documentclass{article}

\usepackage{tikz}

\makeatletter
\pgfmathsetseed{1}
\def\qfill#1{{%
\sbox0{#1}%
\dimen0=1em
\count0=\numexpr\dimexpr(\textwidth-\wd0)/2\relax/\numexpr\dimen0\relax-1\relax
\showthe\count0
\noindent
{\loop
\ifnum\count0>0
\hfil
\hbox to 1em{%
\hss
\pgfmathparse{rnd}
\textcolor[hsb]{\pgfmathresult, 1.0, 1.0}{?}
}\hfil
\repeat}%
\box0
{\loop
\ifnum\count0>0
\hfil
\hbox to 1em{%
\hss
\pgfmathparse{rnd}
\textcolor[hsb]{\pgfmathresult, 1.0, 1.0}{?}
}\hfil