ConTeXT で各単語のテキスト サイズをランダム化するにはどうすればよいですか?

ConTeXT で各単語のテキスト サイズをランダム化するにはどうすればよいですか?

次のようなテキストから始まります。

This is a sample passage.

各単語を 3 ~ 4 種類のサイズのリストからランダムに異なるサイズで表示するように設定するにはどうすればよいですか?

たとえば、出力では、「This」は大きい、「is」は小さい、「a」は中くらいのサイズなどになります。

  • ドキュメントがレンダリングされるたびに、サイズが異なる場合があります。

答え1

Aditya と Henri が言及した 2 つのコマンドの他に、ConTeXt は、指定された引数のそれぞれまたはにコマンドを適用するためのコマンド\applytosplitstringwordspacedとコマンドも提供します。\applytosplitstringcharspacedwordcharacter

\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マクロは1回だけ評価されます。これを緩和するには、手動で「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}}

いずれの場合も、リストの後の不要なスペースに注意してください。

関連情報