7

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\changesizechar#1#2\endchangesize{\addtocounter{numcharacters}{1}\scaleString{#1}{\thenumcharacters}\changesizeinternal{#2}}
\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}

enter image description here

2 Answers2

5

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.

enter image description here

\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{%
\advance\numcharacters 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}
David Carlisle
  • 757,742
2

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}

enter image description here