从这样的一段文字开始:
This is a sample passage.
我怎样才能从 3-4 种可能大小的列表中将每个单词随机设置为不同的大小?
例如,在输出中,“This”可能很大,“is”可能很小,“a”可能是中等大小,等等。
- 每次呈现文档时,它们的大小可能会有所不同。
答案1
除了 Aditya 和 Henri 提到的两个命令之外,ConTeXt 还提供了命令\applytosplitstringwordspaced
和\applytosplitstringcharspaced
将命令应用于每个命令word
或character
给定的参数。
\startluacode
function userdata.randomwordsize(str)
local sizes = { "10pt", "12pt", "14pt", "16pt", "18pt", "20pt" }
local size = sizes[math.random(1,6)]
context.startfont{ string.formatters["Serif at %s"](size) }
context(str)
context.stopfont()
end
\stopluacode
\starttext
\defineexpandable[1]\RandomWordSize
{\ctxlua{userdata.randomwordsize("#1")}}
\applytosplitstringwordspaced\RandomWordSize
{The fraction of fossil olfactory receptor genes is significantly
higher in all species with full color vision. This suggests that
the evolution of trichromatic vision --- which allows these
primates to detect food, mates, and danger with visual cues ---
has reduced their reliance on the sense of smell.}
\blank
\applytosplitstringcharspaced\RandomWordSize
{The fraction of fossil olfactory receptor genes is significantly
higher in all species with full color vision. This suggests that
the evolution of trichromatic vision --- which allows these
primates to detect food, mates, and danger with visual cues ---
has reduced their reliance on the sense of smell.}
\stoptext
答案2
困难的部分是从段落中分离出单词,并对每个单词应用任意宏。ConTeXt 提供了一个\processwords
执行此操作的命令。要使用此命令,您首先必须定义一个\processword
将作用于特定单词的宏。
举个例子,假设你想在每个单词周围画一个框。然后定义:
\def\processword{\inframed}
\starttext
Normal text \processwords{This is a simple passage.} Normal text
\stoptext
这使
现在,下一步是创建一个宏,选择一个随机字体大小并应用该大小。
答案3
Aditya 的答案原则上是正确的,但随机化字体大小的简单宏不起作用。这是因为默认情况下\processwords
使用“旧呼唤”每个单词都放在一个框中,并且\processword
宏只被评估一次。为了缓解这种情况,我们必须手动切换到“MetaFun 呼叫”并且也不需要调用Lua。
\def\processword#1{{\switchtobodyfont[\randomnumber{5}{15}pt]#1}}
\starttext
Normal text \processwords{This is a simple passage.} Normal text
\unprotect
\def\processwords#1%
{\syst_helpers_process_word#1 \_e_o_w_}% no \unskip
\protect
Normal text \processwords{This is a simple passage.}Normal text
\stoptext
或者,您可以使用\processseparatedlist
允许您使用任意分隔符:
\processseparatedlist
[This is a simple passage.]
[ ]
{\groupedcommand{\bgroup\switchtobodyfont[\randomnumber{5}{15}pt]}{\space\egroup}}
在所有情况下,请留意列表后的虚假空格。