# Continuously changing size of text

I am trying to do some Alice in Wonderland style text size changes to irritate the reader. I am having two basic troubles with the code below. Firstly, I would like to retain the spaces. Secondly, for some completely inexplicable reason the exponential increase function seems to break down after a certain number of characters. Any advice appreciated.

Most of the code would be in my preamble, but I have included it in the body for demonstration.

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.8}
\usepackage{graphicx}
\begin{document}

\newcounter{numcharacters}
\def\changesize#1{\setcounter{numcharacters}{0}\changesizeinternal{#1}}
\def\changesizeinternal#1{\ifx#1\relax\else\changesizechar#1\endchangesize\fi}
\def\scaleString#1#2{\pgfmathparse{6/(#2+2)}\scalebox{\pgfmathresult}{#1}}

\changesize{3.1415926535897932384626}%

\changesize{Shrinking away until there is nothing left}%

\def\scaleString#1#2{\pgfmathparse{0.5+(#2/12)}\scalebox{\pgfmathresult}{#1}}
\changesize{This is a linear increase in size 123456789 123456789}%

\def\scaleString#1#2{\pgfmathparse{0.5*(1.05^#2)}\scalebox{\pgfmathresult}{#1}}%exponential grows 5pcnt per character
\changesize{This one is an exponential increase in size 123456789 rabbit}%

\def\scaleString#1#2{\pgfmathparse{1.0*pow(1.08,(#2-1))}\scalebox{\pgfmathresult}{#1}}%exponential grows 8pcnt per character
\changesize{This is a different expontial increase in size 1234567890}%

\end{document}


• Like these tex.stackexchange.com/questions/208426/… ? – percusse Jun 1 '15 at 21:17
• These are great. Though I would like to fix the existing code to get spacing and avoid the \ifx error, and avoid use of another package beyond PGF just for this. @david-carlisle answer noted re the cause of the bug, but I still can't figure out how to fix it. – Aubrey Blumsohn Jun 2 '15 at 8:53
• You have pgfplotstable loaded so PGF/TikZ,pgfplots,graphicx are all loaded already by it already ;) – percusse Jun 2 '15 at 9:26
• Yes, know that -- what trying to avoid use of package scalerel and to use only PGF – Aubrey Blumsohn Jun 2 '15 at 9:56
• The breakdown occurs when the roundoff produces a length of 0, which then ends up in a denominator somewhere. – Steven B. Segletes Jun 2 '15 at 18:48

As you can see if you try

\changesize{3.333333333333333333}%


The macro is terminated whenever two adjacent tokens are equal.

\def\changesizeinternal#1{\ifx#1\relax\


the \ifx tests if the first two tokens in #1 have equal meaning, it does not just test if #1 is \relax (although that would also test true as then the first tokens would be \ix\relax\relax).

In order not to lose spaces you need to lookahead with \futurelet not a macro argument, which also gives you a safe token to test with \ifx.

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.8}
\usepackage{graphicx}
\begin{document}

\makeatletter
\newcount\numcharacters
\def\changesize#1{\numcharacters=0 \changesizeinternal#1\relax}
\def\changesizeinternal{\futurelet\tmp\xchangesizeinternal}
\def\xchangesizeinternal{%
\ifx\tmp\relax
\else
\ifx\tmp\@sptoken\space\fi
\expandafter\changesizechar
\fi}
\def\changesizechar#1{%
\scaleString{#1}{\numcharacters}%
\changesizeinternal}
\def\scaleString#1#2{\pgfmathparse{6/(#2+2)}\scalebox{\pgfmathresult}{#1}}

\raggedright

\changesize{3.1415926535897932384626}%

\changesize{Shrinking away until there is nothing left}%

\def\scaleString#1#2{\pgfmathparse{0.5+(#2/12)}\scalebox{\pgfmathresult}{#1}}
\changesize{This is a linear increase in size 123456789 123456789}%

\def\scaleString#1#2{\pgfmathparse{0.5*(1.05^#2)}\scalebox{\pgfmathresult}{#1}}%exponential grows 5pcnt per character
\changesize{This one is an exponential increase in size 123456789 rabbit}%

\def\scaleString#1#2{\pgfmathparse{1.0*pow(1.08,(#2-1))}\scalebox{\pgfmathresult}{#1}}%exponential grows 8pcnt per character
\changesize{This is a different expontial increase in size 1234567890}%

\end{document}

• Understand (sort of) the \ifx error and the circumstances of the code bug, but no idea how to fix it or allow spacing to be included. – Aubrey Blumsohn Jun 2 '15 at 17:54
• @AubreyBlumsohn updated answer – David Carlisle Jun 2 '15 at 18:46

I adapted my answer at How do I display pi in LaTeX like Don? to both work in non-math mode and to permit the use of spaces.

EDITED so that spaces no longer need quoting with a backslash.

\documentclass{article}
\usepackage{scalerel}
\newlength\curht
\def\defaultdimfrac{.98}
\def\defaultstartht{\baselineskip}
\newcommand\diminish[2][\defaultdimfrac]{%
\curht=\defaultstartht\relax
\def\dimfrac{#1}%
\diminishhelpA{#2}}
\newcommand\diminishhelpA[1]{%
\expandafter\diminishhelpB#1 \relax\relax}
\def\diminishhelpB#1 #2\relax{%
\diminishhelpC#1\relax\relax%
\ifx\relax#2\relax\else%
\curht=\dimfrac\curht\relax\scaleto{\strut\ }{\curht}\allowbreak%
\diminishhelpB#2\relax\fi}
\def\diminishhelpC#1#2\relax{%
\scaleto{$\strut#1$}{\curht}%
\curht=\dimfrac\curht\relax%
\ifx\relax#2\relax\else\diminishhelpC#2\relax\fi}
\begin{document}
\def\defaultstartht{14pt}
\def\increasetext{This is a linear increase in size. This is a linear increase in size.}
\def\decreasetext{This is a linear decrease in size}
\diminish{\decreasetext}\par\diminish[1.03]{\increasetext}\par
\diminish[0.94]{\decreasetext}\par\def\defaultstartht{38pt}\diminish[0.92]{\decreasetext}
\end{document}
`