3

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{
  \leavevmode \cleaders \hb@xt@ 1em{%
    \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?

2
  • 1
    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
  • 1
    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
3

enter image description here

\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
\advance\count0 -1
\repeat}%
\box0
{\loop
\ifnum\count0>0
\hfil
\hbox to 1em{%
    \hss
    \pgfmathparse{rnd}
    \textcolor[hsb]{\pgfmathresult, 1.0, 1.0}{?}
  }\hfil
\advance\count0 -1
\repeat}%
\par}}
\makeatother

\begin{document}

\qfill{What will be the answer?}

\end{document}
1
  • This is very cool (and also insane). Thanks! – carsten Nov 4 '14 at 16:33

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.