Skip to content

Instantly share code, notes, and snippets.

@vermiculus
Created October 9, 2014 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vermiculus/ed425d3319f5a3912353 to your computer and use it in GitHub Desktop.
Save vermiculus/ed425d3319f5a3912353 to your computer and use it in GitHub Desktop.
\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\relax{\gdef\wordremaining{#1}}
\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
\usepackage[breakall]{truncate}
\begin{document}
\FadeAfter{.5in}
{\truncate[]{3in}{Hello world, you would never believe how hard it~is to get a string that show's off the breaking}}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment