The strategy here is to add a character with each loop, stuff the current string into a box and measure it. Then, use the fractional measure of current length to fade-length to subtract intensity from the color of that character. The routine \prefahelper
I use to determine whether I am at the end of a word and need to remember a space. The routine \fahelper
is the workhorse, called upon recursively by \fahelp
.
Thanks to percusse at Dividing dimensions to get a count for showing how to divide lengths using counters (a trick he learned from egreg, if I recall).
EDITED to multiply denominator by 0.01, rather than multiply numerator by 100, in order to avoid counter overflows. Multiline example shown.
EDITED to correct bug with one-letter words.
\documentclass{article}
\usepackage{xcolor,ifthen}
\newcounter{tmpcounter}
\newlength\cumlength
\newlength\critlength
\newlength\tmplength
\newcount\mynum
\newcount\myden
\makeatletter
\newcommand\FadeAfter[2]{\critlength=#1\relax\cumlength=0pt\relax%
\def\cumstring{}\fahelp{#1}{#2}}
\newcommand\fahelp[2]{\prefahelper#2 \relax\fahelper#2\relax}
\def\prefahelper#1#2 #3\relax{\gdef\wordremaining{#1#2}}
\def\fahelper#1#2\relax{%
\global\protected@edef\cumstring{\cumstring#1}%
\ifthenelse{\equal{#1}{\wordremaining}}{%
\global\protected@edef\cumstring{\cumstring\ }}{}%
\setbox0=\hbox{\cumstring}%
\tmplength=.01\critlength\relax%
\mynum=\wd0\relax%
\myden=\tmplength\relax%
\divide\mynum by\myden%
\setcounter{tmpcounter}{\numexpr100-\the\mynum}%
\ifnum\thetmpcounter<0\setcounter{tmpcounter}{0}\fi%
\textcolor{black!\thetmpcounter}{#1}%
\ifthenelse{\equal{#1}{\wordremaining}}{\ }{}%
\ifdim\wd0<\critlength%
\ifx\relax#2\relax\else\fahelp{\critlength}{#2}\fi%
\fi%
}
\makeatother
\begin{document}
\FadeAfter{.5in}{testing whether this fades away}%
\FadeAfter{1in}{testing whether this fades away}%
\FadeAfter{1.5in}{testing whether this fades away}%
\FadeAfter{2.5in}{testing whether this fades away}
\FadeAfter{.5in}{testing whether this fades away}\par
\FadeAfter{1in}{testing whether this fades away}\par
\FadeAfter{1.5in}{testing whether this fades away}\par
\FadeAfter{2.5in}{testing whether this fades away}\par
\FadeAfter{7in}{This is a very long multi-line test
This is a very long test This is a very long test
This is a very long test This is a very long test}
\end{document}

#1
and stop fading at.east
, at which point it will be completely transparent. Pure-white works, too, but I'll always wonder if there's a way to do it with just transparency :) – Sean Allred Oct 9 '14 at 2:35