不断改变文本的大小

不断改变文本的大小

我正在尝试做一些爱丽丝梦游仙境风格的文本大小更改来激怒读者。下面的代码有两个基本问题。首先,我想保留空格。其次,由于某种完全无法解释的原因,指数增加函数似乎在一定数量的字符后失效。任何建议都值得赞赏。

大部分代码都在我的序言中,但我将其包含在正文中以供演示。

\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}

在此处输入图片描述

答案1

正如你所见如果你尝试

\changesize{3.333333333333333333}%

只要两个相邻的标记相等,宏就会终止。

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

测试\ifx中的前两个标记是否#1具有相同的含义,它不仅仅测试是否#1\relax(尽管这也会测试为真,因为那么第一个标记将是\ix\relax\relax)。


为了不丢失空格,您需要使用\futurelet非宏参数进行前瞻,这也为您提供了一个安全的标记来使用“\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{%
\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}

答案2

我修改了我的答案我怎样才能像 Don 一样在 LaTeX 中显示 pi?既可以在非数学模式下工作,又允许使用空格。

已编辑,因此空格不再需要用反斜杠引用。

\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}

在此处输入图片描述

相关内容