可变字体格式

可变字体格式

我想创建一个 LaTeX 函数:(1)将字符串作为参数(2)计算字符串的长度,(3)将长度除以 3,(3)对字符串的前 1/3 应用一种格式,对字符串的中间 1/3 应用另一种格式,然后对字符串的剩余部分应用另一种格式。

有没有办法做到这一点?

答案1

以下是使用fp计算包和xstring字符串拆分包的方法。格式化命令是硬编码的,在本例中为粗体、蓝色和大字体。

代码:

\documentclass{article}
\usepackage{xstring}
\usepackage{fp}
\usepackage{xcolor}
\newcommand{\divprint}[1]{%
\StrLen{#1}[\mylen]% get length of string
\FPeval\x{clip(round(\mylen/3,0))}% divide by 3, round on 0 decimal places, clip any trailing zeroes
\StrSplit{#1}{\x}{\strA}{\strBC}% split string on the calculated position
\StrSplit{\strBC}{\x}{\strB}{\strC}% split remaining string
\textbf{\strA}\textcolor{blue}{\strB}\Large\strC\normalsize% print formatted string
}
\begin{document}
\divprint{fourfourfour}

\divprint{random string}
\end{document}

结果:

在此处输入图片描述

相关内容