(source: bankfotek.pl)

I have chapter and on end of each line I would write counter of chars, and before text line write a line sized of counts of chars.

Please solution in LuaTeX. My LaTeX code


This is a proof of concept:


function countchars(head)
  while head do
    if head.id == 0 then
      -- a hbox
      local c = 0
      local first_font
      for n in node.traverse_id(37,head.head) do
        c = c + 1
        first_font = first_font or n.font

      local glue = node.new("glue")
      glue.spec = node.new("glue_spec")
      glue.spec.stretch = 2^16
      glue.spec.stretch_order = 3
      local rule = node.new("rule")
      rule.width = 1 * c * 2^16
      rule.height = 2 * 2^16
      while c > 0 do
        local digit = math.mod(c,10)
        local glyf = node.new("glyph")
        glyf.font = first_font
        glyf.char = 48 + digit
        c = c - digit
        c = c / 10
      local llap_glue = node.new("glue")
      llap_glue.spec = node.new("glue_spec")
      llap_glue.spec.stretch = 2^16
      llap_glue.spec.stretch_order = 2
      llap_glue.spec.shrink = 2^16
      llap_glue.spec.shrink_order = 2
      node.insert_after(llap_glue, llap_glue, rule)
      llap_hbox = node.hpack(llap_glue,0,"exactly")
      local marker = node.insert_before(glue,glue,llap_hbox)
      local h = node.hpack(marker,head.width + 14*2^16,"exactly")
      h.width = 0
      head.head = node.insert_before(head.head,head.head,h)
    head = head.next
  return true


A \emph{wonderful} serenity has taken {\large possession} of my entire soul, like these
\textbf{mornings} of spring which I enjoy with my whole heart. I am alone, and feel the
charm of existence in this spot, \textbf{which} was created for the bliss of souls like
mine. I am so happy, my dear friend, so absorbed in the exquisite sense of
mere tranquil existence, that I neglect my talents. I should be incapable of
drawing a single stroke at the present moment; and yet I feel that I never was
a greater artist than now.


enter image description here

  • it looks like that you do not count characters of words in a \mbox{foo}. node.traverse doesn't work recursively – user2478 May 1 '13 at 11:11
  • @Herbert no, I don't. There are several more flaws in the above code. It's a proof of concept :) – topskip May 1 '13 at 11:13
  • 3
    Just to name a few flaws: ligatures are counted as 1 character, font at the right hand side depends on the first glyph of that row, hidden characters inside \mbox{...}/\hbox{...} are not counted, hyphens, punctuation marks, bullet points and other non-text are counted as one character, math mode is not tested, \accented letters are counted more than once probably, ... A good solution will always make things more complicated. – topskip May 1 '13 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.