How can I 'fade out' text after a certain length?
Here is a possibility which draws white color over the text:
\documentclass{article}
\usepackage{tikz,lipsum}
\usetikzlibrary{fadings}
\makeatletter
\tikzfading[name=fade left,
left color=transparent!100,
right color=transparent!0]
\newcommand{\FadeAfter}[2]{%
\par\noindent\begin{tikzpicture}
\node[inner sep=0pt,inner ysep=2pt,outer sep=0pt,clip] (A) {\makebox[\linewidth][l]{#2}};
\fill[white,path fading=fade left] ([xshift={#1}]A.south west) rectangle ([xshift={1pt}]A.north east);
\end{tikzpicture}\par%
}
\makeatother
\begin{document}
\FadeAfter{0pt}{\lipsum[1]}
\FadeAfter{.25\linewidth}{\lipsum[1]}
\FadeAfter{.5\linewidth}{\lipsum[1]}
\FadeAfter{.75\linewidth}{\lipsum[1]}
\medskip
\lipsum[1]
\end{document}
I have removed the indent, but the macro could be adapted, if you want to indent the text.
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}