3

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?

All I found out so far is that it might be easier to get hyphenation points instead of syllables, based on this question: Using TeX to split words into syllables
That would be acceptable, but then instead of showing hyphens, I would like to colorize the parts of the word as described above.

1 Answer 1

4

As a proof of concept, try the following, which is a mashup of the showhyphens package of Patrick Gundlach and the LuaTeX comes of age article on LWN.

This is 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

This is the TeX file:

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

and this is the output: paragraph colourised at hyphenation boundaries

I've tried to detect word boundaries by looking for glue followed by a glyph, but I don't know how reliable this is. You can edit the colour cycle: the entries are RGB values between 0 and 1, and you might want more than 4 of them (especially in German) or if you just want to alternate colours, just list two. Hyphenation points within ligatures are dealt with the same way as in showhyphens.

You can see the weakness of using hyphenation points, though: there are fairly large stretches of the example in the same colour, which is because TeX does not hyphenate two-syllable words, as mentioned in the previous answer you linked to [edit: actually, perhaps this is not as bad as I thought, see the linked answer.]

I'm not especially proficient in luatex, so can't vouch for the robustness of this code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .