为学习阅读的一年级学生着色音节

为学习阅读的一年级学生着色音节

在德国,可能还有其他国家,一些一年级学生使用所谓的“音节法”学习阅读,即每个有两个或多个音节的单词的音节都涂上不同的颜色。因此,所有只有一个音节的单词都是蓝色的,在有三个音节的单词中,第一个音节是蓝色,第二个音节是红色,第三个音节是蓝色,等等。

这在 LuaLaTex 中可以轻松实现吗?

到目前为止,我发现,根据这个问题,获取连字点可能比获取音节更容易: 使用 TeX 将单词拆分成音节
这是可以接受的,但是我不想显示连字符,而是想按照上面描述的方式给单词的各个部分着色。

答案1

作为概念验证,请尝试以下内容,它是showhyphensPatrick Gundlach 的软件包和LuaTeX 成熟LWN 上的文章。

这是modhyphens.sty

\ProvidesPackage{modhyphens}
\RequirePackage{ifluatex,luatexbase}
% Modification of `showhyphens' package, whose license follows:
% License: MIT style license

% Copyright (c) 2011-2016 Patrick Gundlach [email protected]

% Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
% "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
% publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
% so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
% FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

\ifluatex
\directlua{
local show_hyph
local hlist_node = node.id("hlist")
local vlist_node = node.id("vlist")
local disc_node = node.id("disc")
local glyph_node = node.id("glyph")
local glue_node = node.id("glue")

local colortable = {"0.4 0.6 0.8", "0.867 0.800 0.467", "0.267 0.667 0.600", "0.533 0.133 0.333"}
local colortable_len = 4
local colortable_i = 1
local new_word_heuristic = 0

local function round(num)
  return math.floor(num * 10^3 + 0.5) / 10^3
end

show_hyph = function(head)
  for n in node.traverse(head) do
    if n.id == hlist_node or n.id == vlist_node then
      show_hyph(n.list)
    elseif n.id == glue_node then
      new_word_heuristic = 1
    elseif n.id == glyph_node then
      if new_word_heuristic == 1 then
        new_word_heuristic = 0
        colortable_i = 1
        local m = node.new("whatsit",node.subtype("pdf_colorstack"))
        m.data = colortable[colortable_i].." rg"
        node.insert_before(head,n,node.copy(m))
      end
    elseif n.id == disc_node then
      if n.replace and n.replace.id == glyph_node and n.replace.components then
        local wd = round(n.replace.width / 65781)  or 0
        local ht = round(n.replace.height / 65781) + 0.5  or 0
        local r = node.new("whatsit","pdf_literal")
        r.data = "q 0.3 w 0 " .. tostring(ht) .. " m " .. tostring(-wd) .. " " .. tostring(ht) ..  "  l S Q"
        node.insert_after(n.replace,n.replace,r)
      else
        colortable_i = colortable_i+1
        if colortable_i > colortable_len then
          colortable_i = 1
        end
        local m = node.new("whatsit",node.subtype("pdf_colorstack"))
        m.data = colortable[colortable_i].." rg"
        node.insert_before(head,n,node.copy(m))
      end
    end
  end
  return true
end


luatexbase.add_to_callback("post_linebreak_filter",show_hyph,"show_hyph")
}
\else
\errhelp{Please use LuaLaTeX when you require the package 'modhyphens'}
\errmessage{Package modhyphens error: This works only with LuaLaTeX!}
\fi

这是 TeX 文件:

\documentclass{article}

\usepackage{modhyphens}

\begin{document}

In Germany and possibly other countries, some first graders learn to read using the so-called ``syllable method", where the syllables in every word with two or more syllables are colored differently. So, all words with just one syllable are e.g blue, in words with three syllables the first syllable is blue, second is red and third is blue etc.

Would this be doable in LuaLaTex easily?

\end{document}

输出结果如下: 段落在连字符边界处着色

我曾尝试通过查找后跟字形的粘连来检测单词边界,但我不知道这有多可靠。您可以编辑颜色循环:条目是 0 到 1 之间的 RGB 值,您可能需要超过 4 个(尤其是在德语中),或者如果您只想交替使用颜色,只需列出两个。连字符内的连字点的处理方式与 相同showhyphens

但是,您可以看到使用连字符点的弱点:示例中有很大一部分内容为相同的颜色,这是因为 TeX 不会对双音节单词进行连字符连接,正如您链接到的上一个答案中所提到的那样 [编辑:实际上,也许这并不像我想象的那么糟糕,请参阅链接的答案。]

我对 luatex 不是特别精通,因此无法保证该代码的稳健性。

相关内容