marking end of words in luatex - problem with math












8














I'm trying to mark the end of "words" (including punctuation) with luatex callbacks. (At the end I want to inject space chars at this positions, but the example uses rules for the visualization).



With some code based on countwords from the chickenize package it works quite fine. I only have a problem with math: I would like to avoid to get marks there. Trying to do it with an attribute set in everymath doesn't work as can be seen in the code below: It also affects other places like e.g. tabulars where math mode is used internally.



Has someone an idea how to exclude only "real" math from the mark function?



documentclass{article}
usepackage{luacode,amsmath}

newattributemathattr

begin{luacode}

local MATHATTR = luatexbase.registernumber ("mathattr")
local nodetraverseid = node.traverse_id
local nodegetattribute = node.get_attribute
local GLUE = node.id("glue")
local GLYPH = node.id("glyph")

local function insertmark (head,current)
local pdfstring = node.new("whatsit","pdf_literal")
pdfstring.data =
string.format("q 1 0 0 RG 1 0 0 rg 0.4 w 0 %g m 0 %g l S Q",-3,10)
head = node.insert_after(head,current,pdfstring)
return head
end

local markwords = function(head)
for glyph in nodetraverseid(GLYPH,head) do
if glyph.next and (glyph.next.id == GLUE)
and not nodegetattribute(glyph,MATHATTR)
then
insertmark(head,glyph)
end
end
return head
end


luatexbase.add_to_callback("pre_linebreak_filter",markwords,"markwords")

luatexbase.add_to_callback("hpack_filter",markwords,"markwords")

end{luacode}
usepackage{lipsum}
begin{document}


begin{tabular}{l}
xxx xxx
end{tabular}

$a=b int f(x) =0quad text{abc abc abc abc} $

everymath{setattributemathattr{1}}

begin{tabular}{l}
xxx xxx
end{tabular}

$a=b int f(x) =0quad text{abc abc abc abc} $

bigskip

VA VA
lipsum[1]

end{document}


enter image description here










share|improve this question





























    8














    I'm trying to mark the end of "words" (including punctuation) with luatex callbacks. (At the end I want to inject space chars at this positions, but the example uses rules for the visualization).



    With some code based on countwords from the chickenize package it works quite fine. I only have a problem with math: I would like to avoid to get marks there. Trying to do it with an attribute set in everymath doesn't work as can be seen in the code below: It also affects other places like e.g. tabulars where math mode is used internally.



    Has someone an idea how to exclude only "real" math from the mark function?



    documentclass{article}
    usepackage{luacode,amsmath}

    newattributemathattr

    begin{luacode}

    local MATHATTR = luatexbase.registernumber ("mathattr")
    local nodetraverseid = node.traverse_id
    local nodegetattribute = node.get_attribute
    local GLUE = node.id("glue")
    local GLYPH = node.id("glyph")

    local function insertmark (head,current)
    local pdfstring = node.new("whatsit","pdf_literal")
    pdfstring.data =
    string.format("q 1 0 0 RG 1 0 0 rg 0.4 w 0 %g m 0 %g l S Q",-3,10)
    head = node.insert_after(head,current,pdfstring)
    return head
    end

    local markwords = function(head)
    for glyph in nodetraverseid(GLYPH,head) do
    if glyph.next and (glyph.next.id == GLUE)
    and not nodegetattribute(glyph,MATHATTR)
    then
    insertmark(head,glyph)
    end
    end
    return head
    end


    luatexbase.add_to_callback("pre_linebreak_filter",markwords,"markwords")

    luatexbase.add_to_callback("hpack_filter",markwords,"markwords")

    end{luacode}
    usepackage{lipsum}
    begin{document}


    begin{tabular}{l}
    xxx xxx
    end{tabular}

    $a=b int f(x) =0quad text{abc abc abc abc} $

    everymath{setattributemathattr{1}}

    begin{tabular}{l}
    xxx xxx
    end{tabular}

    $a=b int f(x) =0quad text{abc abc abc abc} $

    bigskip

    VA VA
    lipsum[1]

    end{document}


    enter image description here










    share|improve this question



























      8












      8








      8







      I'm trying to mark the end of "words" (including punctuation) with luatex callbacks. (At the end I want to inject space chars at this positions, but the example uses rules for the visualization).



      With some code based on countwords from the chickenize package it works quite fine. I only have a problem with math: I would like to avoid to get marks there. Trying to do it with an attribute set in everymath doesn't work as can be seen in the code below: It also affects other places like e.g. tabulars where math mode is used internally.



      Has someone an idea how to exclude only "real" math from the mark function?



      documentclass{article}
      usepackage{luacode,amsmath}

      newattributemathattr

      begin{luacode}

      local MATHATTR = luatexbase.registernumber ("mathattr")
      local nodetraverseid = node.traverse_id
      local nodegetattribute = node.get_attribute
      local GLUE = node.id("glue")
      local GLYPH = node.id("glyph")

      local function insertmark (head,current)
      local pdfstring = node.new("whatsit","pdf_literal")
      pdfstring.data =
      string.format("q 1 0 0 RG 1 0 0 rg 0.4 w 0 %g m 0 %g l S Q",-3,10)
      head = node.insert_after(head,current,pdfstring)
      return head
      end

      local markwords = function(head)
      for glyph in nodetraverseid(GLYPH,head) do
      if glyph.next and (glyph.next.id == GLUE)
      and not nodegetattribute(glyph,MATHATTR)
      then
      insertmark(head,glyph)
      end
      end
      return head
      end


      luatexbase.add_to_callback("pre_linebreak_filter",markwords,"markwords")

      luatexbase.add_to_callback("hpack_filter",markwords,"markwords")

      end{luacode}
      usepackage{lipsum}
      begin{document}


      begin{tabular}{l}
      xxx xxx
      end{tabular}

      $a=b int f(x) =0quad text{abc abc abc abc} $

      everymath{setattributemathattr{1}}

      begin{tabular}{l}
      xxx xxx
      end{tabular}

      $a=b int f(x) =0quad text{abc abc abc abc} $

      bigskip

      VA VA
      lipsum[1]

      end{document}


      enter image description here










      share|improve this question















      I'm trying to mark the end of "words" (including punctuation) with luatex callbacks. (At the end I want to inject space chars at this positions, but the example uses rules for the visualization).



      With some code based on countwords from the chickenize package it works quite fine. I only have a problem with math: I would like to avoid to get marks there. Trying to do it with an attribute set in everymath doesn't work as can be seen in the code below: It also affects other places like e.g. tabulars where math mode is used internally.



      Has someone an idea how to exclude only "real" math from the mark function?



      documentclass{article}
      usepackage{luacode,amsmath}

      newattributemathattr

      begin{luacode}

      local MATHATTR = luatexbase.registernumber ("mathattr")
      local nodetraverseid = node.traverse_id
      local nodegetattribute = node.get_attribute
      local GLUE = node.id("glue")
      local GLYPH = node.id("glyph")

      local function insertmark (head,current)
      local pdfstring = node.new("whatsit","pdf_literal")
      pdfstring.data =
      string.format("q 1 0 0 RG 1 0 0 rg 0.4 w 0 %g m 0 %g l S Q",-3,10)
      head = node.insert_after(head,current,pdfstring)
      return head
      end

      local markwords = function(head)
      for glyph in nodetraverseid(GLYPH,head) do
      if glyph.next and (glyph.next.id == GLUE)
      and not nodegetattribute(glyph,MATHATTR)
      then
      insertmark(head,glyph)
      end
      end
      return head
      end


      luatexbase.add_to_callback("pre_linebreak_filter",markwords,"markwords")

      luatexbase.add_to_callback("hpack_filter",markwords,"markwords")

      end{luacode}
      usepackage{lipsum}
      begin{document}


      begin{tabular}{l}
      xxx xxx
      end{tabular}

      $a=b int f(x) =0quad text{abc abc abc abc} $

      everymath{setattributemathattr{1}}

      begin{tabular}{l}
      xxx xxx
      end{tabular}

      $a=b int f(x) =0quad text{abc abc abc abc} $

      bigskip

      VA VA
      lipsum[1]

      end{document}


      enter image description here







      math-mode luatex callback






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 3 hours ago

























      asked 4 hours ago









      Ulrike Fischer

      186k7290669




      186k7290669






















          1 Answer
          1






          active

          oldest

          votes


















          5














          The math content is enclosed by math nodes, with subtype 0 marking beginning, and subtype 1 marking closing of the math:



          documentclass{article}
          usepackage{luacode,amsmath}

          newattributemathattr

          begin{luacode}

          local MATHATTR = luatexbase.registernumber ("mathattr")
          local nodetraverseid = node.traverse_id
          local nodetraverse = node.traverse
          local nodegetattribute = node.get_attribute
          local GLUE = node.id("glue")
          local GLYPH = node.id("glyph")
          local MATH = node.id("math")
          local HLIST = node.id("hlist")
          local VLIST = node.id("vlist")


          local function insertmark (head,current)
          local pdfstring = node.new("whatsit","pdf_literal")
          pdfstring.data =
          string.format("q 1 0 0 RG 1 0 0 rg 0.4 w 0 %g m 0 %g l S Q",-3,10)
          head = node.insert_after(head,current,pdfstring)
          return head
          end

          local function markwords(head)
          local inside_math = false
          for n in nodetraverse(head) do
          local id = n.id
          if id == GLYPH then
          local glyph = n
          if glyph.next and (glyph.next.id == GLUE)
          and not (inside_math or nodegetattribute(glyph,MATHATTR))
          then
          insertmark(head,glyph)
          end
          elseif id == MATH then
          inside_math = (n.subtype == 0)
          end
          end
          return head
          end


          luatexbase.add_to_callback("pre_linebreak_filter",markwords,"markwords")

          luatexbase.add_to_callback("hpack_filter",markwords,"markwords")

          end{luacode}
          usepackage{lipsum}
          begin{document}


          begin{tabular}{l}
          xxx xxx
          end{tabular}

          $a=b int f(x) =0quad text{abc abc abc abc} $


          everymath{setattributemathattr{1}}

          begin{tabular}{l}
          xxx xxx
          end{tabular}

          $a=b int f(x) =0quad text{abc abc abc abc} $

          bigskip

          VA VA
          lipsum[1]

          end{document}


          Contrary to your screenshot, the code in your question doesn't put bar behind last word in text{abc abc abc abc} and at the end of a paragraph:



          enter image description here






          share|improve this answer























          • elseif id == MATH then inside_math =(n.subtype == 0) end is a bit shorter
            – Herbert
            1 hour ago










          • @Herbert thanks
            – michal.h21
            1 hour ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "85"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468153%2fmarking-end-of-words-in-luatex-problem-with-math%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          The math content is enclosed by math nodes, with subtype 0 marking beginning, and subtype 1 marking closing of the math:



          documentclass{article}
          usepackage{luacode,amsmath}

          newattributemathattr

          begin{luacode}

          local MATHATTR = luatexbase.registernumber ("mathattr")
          local nodetraverseid = node.traverse_id
          local nodetraverse = node.traverse
          local nodegetattribute = node.get_attribute
          local GLUE = node.id("glue")
          local GLYPH = node.id("glyph")
          local MATH = node.id("math")
          local HLIST = node.id("hlist")
          local VLIST = node.id("vlist")


          local function insertmark (head,current)
          local pdfstring = node.new("whatsit","pdf_literal")
          pdfstring.data =
          string.format("q 1 0 0 RG 1 0 0 rg 0.4 w 0 %g m 0 %g l S Q",-3,10)
          head = node.insert_after(head,current,pdfstring)
          return head
          end

          local function markwords(head)
          local inside_math = false
          for n in nodetraverse(head) do
          local id = n.id
          if id == GLYPH then
          local glyph = n
          if glyph.next and (glyph.next.id == GLUE)
          and not (inside_math or nodegetattribute(glyph,MATHATTR))
          then
          insertmark(head,glyph)
          end
          elseif id == MATH then
          inside_math = (n.subtype == 0)
          end
          end
          return head
          end


          luatexbase.add_to_callback("pre_linebreak_filter",markwords,"markwords")

          luatexbase.add_to_callback("hpack_filter",markwords,"markwords")

          end{luacode}
          usepackage{lipsum}
          begin{document}


          begin{tabular}{l}
          xxx xxx
          end{tabular}

          $a=b int f(x) =0quad text{abc abc abc abc} $


          everymath{setattributemathattr{1}}

          begin{tabular}{l}
          xxx xxx
          end{tabular}

          $a=b int f(x) =0quad text{abc abc abc abc} $

          bigskip

          VA VA
          lipsum[1]

          end{document}


          Contrary to your screenshot, the code in your question doesn't put bar behind last word in text{abc abc abc abc} and at the end of a paragraph:



          enter image description here






          share|improve this answer























          • elseif id == MATH then inside_math =(n.subtype == 0) end is a bit shorter
            – Herbert
            1 hour ago










          • @Herbert thanks
            – michal.h21
            1 hour ago
















          5














          The math content is enclosed by math nodes, with subtype 0 marking beginning, and subtype 1 marking closing of the math:



          documentclass{article}
          usepackage{luacode,amsmath}

          newattributemathattr

          begin{luacode}

          local MATHATTR = luatexbase.registernumber ("mathattr")
          local nodetraverseid = node.traverse_id
          local nodetraverse = node.traverse
          local nodegetattribute = node.get_attribute
          local GLUE = node.id("glue")
          local GLYPH = node.id("glyph")
          local MATH = node.id("math")
          local HLIST = node.id("hlist")
          local VLIST = node.id("vlist")


          local function insertmark (head,current)
          local pdfstring = node.new("whatsit","pdf_literal")
          pdfstring.data =
          string.format("q 1 0 0 RG 1 0 0 rg 0.4 w 0 %g m 0 %g l S Q",-3,10)
          head = node.insert_after(head,current,pdfstring)
          return head
          end

          local function markwords(head)
          local inside_math = false
          for n in nodetraverse(head) do
          local id = n.id
          if id == GLYPH then
          local glyph = n
          if glyph.next and (glyph.next.id == GLUE)
          and not (inside_math or nodegetattribute(glyph,MATHATTR))
          then
          insertmark(head,glyph)
          end
          elseif id == MATH then
          inside_math = (n.subtype == 0)
          end
          end
          return head
          end


          luatexbase.add_to_callback("pre_linebreak_filter",markwords,"markwords")

          luatexbase.add_to_callback("hpack_filter",markwords,"markwords")

          end{luacode}
          usepackage{lipsum}
          begin{document}


          begin{tabular}{l}
          xxx xxx
          end{tabular}

          $a=b int f(x) =0quad text{abc abc abc abc} $


          everymath{setattributemathattr{1}}

          begin{tabular}{l}
          xxx xxx
          end{tabular}

          $a=b int f(x) =0quad text{abc abc abc abc} $

          bigskip

          VA VA
          lipsum[1]

          end{document}


          Contrary to your screenshot, the code in your question doesn't put bar behind last word in text{abc abc abc abc} and at the end of a paragraph:



          enter image description here






          share|improve this answer























          • elseif id == MATH then inside_math =(n.subtype == 0) end is a bit shorter
            – Herbert
            1 hour ago










          • @Herbert thanks
            – michal.h21
            1 hour ago














          5












          5








          5






          The math content is enclosed by math nodes, with subtype 0 marking beginning, and subtype 1 marking closing of the math:



          documentclass{article}
          usepackage{luacode,amsmath}

          newattributemathattr

          begin{luacode}

          local MATHATTR = luatexbase.registernumber ("mathattr")
          local nodetraverseid = node.traverse_id
          local nodetraverse = node.traverse
          local nodegetattribute = node.get_attribute
          local GLUE = node.id("glue")
          local GLYPH = node.id("glyph")
          local MATH = node.id("math")
          local HLIST = node.id("hlist")
          local VLIST = node.id("vlist")


          local function insertmark (head,current)
          local pdfstring = node.new("whatsit","pdf_literal")
          pdfstring.data =
          string.format("q 1 0 0 RG 1 0 0 rg 0.4 w 0 %g m 0 %g l S Q",-3,10)
          head = node.insert_after(head,current,pdfstring)
          return head
          end

          local function markwords(head)
          local inside_math = false
          for n in nodetraverse(head) do
          local id = n.id
          if id == GLYPH then
          local glyph = n
          if glyph.next and (glyph.next.id == GLUE)
          and not (inside_math or nodegetattribute(glyph,MATHATTR))
          then
          insertmark(head,glyph)
          end
          elseif id == MATH then
          inside_math = (n.subtype == 0)
          end
          end
          return head
          end


          luatexbase.add_to_callback("pre_linebreak_filter",markwords,"markwords")

          luatexbase.add_to_callback("hpack_filter",markwords,"markwords")

          end{luacode}
          usepackage{lipsum}
          begin{document}


          begin{tabular}{l}
          xxx xxx
          end{tabular}

          $a=b int f(x) =0quad text{abc abc abc abc} $


          everymath{setattributemathattr{1}}

          begin{tabular}{l}
          xxx xxx
          end{tabular}

          $a=b int f(x) =0quad text{abc abc abc abc} $

          bigskip

          VA VA
          lipsum[1]

          end{document}


          Contrary to your screenshot, the code in your question doesn't put bar behind last word in text{abc abc abc abc} and at the end of a paragraph:



          enter image description here






          share|improve this answer














          The math content is enclosed by math nodes, with subtype 0 marking beginning, and subtype 1 marking closing of the math:



          documentclass{article}
          usepackage{luacode,amsmath}

          newattributemathattr

          begin{luacode}

          local MATHATTR = luatexbase.registernumber ("mathattr")
          local nodetraverseid = node.traverse_id
          local nodetraverse = node.traverse
          local nodegetattribute = node.get_attribute
          local GLUE = node.id("glue")
          local GLYPH = node.id("glyph")
          local MATH = node.id("math")
          local HLIST = node.id("hlist")
          local VLIST = node.id("vlist")


          local function insertmark (head,current)
          local pdfstring = node.new("whatsit","pdf_literal")
          pdfstring.data =
          string.format("q 1 0 0 RG 1 0 0 rg 0.4 w 0 %g m 0 %g l S Q",-3,10)
          head = node.insert_after(head,current,pdfstring)
          return head
          end

          local function markwords(head)
          local inside_math = false
          for n in nodetraverse(head) do
          local id = n.id
          if id == GLYPH then
          local glyph = n
          if glyph.next and (glyph.next.id == GLUE)
          and not (inside_math or nodegetattribute(glyph,MATHATTR))
          then
          insertmark(head,glyph)
          end
          elseif id == MATH then
          inside_math = (n.subtype == 0)
          end
          end
          return head
          end


          luatexbase.add_to_callback("pre_linebreak_filter",markwords,"markwords")

          luatexbase.add_to_callback("hpack_filter",markwords,"markwords")

          end{luacode}
          usepackage{lipsum}
          begin{document}


          begin{tabular}{l}
          xxx xxx
          end{tabular}

          $a=b int f(x) =0quad text{abc abc abc abc} $


          everymath{setattributemathattr{1}}

          begin{tabular}{l}
          xxx xxx
          end{tabular}

          $a=b int f(x) =0quad text{abc abc abc abc} $

          bigskip

          VA VA
          lipsum[1]

          end{document}


          Contrary to your screenshot, the code in your question doesn't put bar behind last word in text{abc abc abc abc} and at the end of a paragraph:



          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 1 hour ago









          michal.h21

          30.3k446104




          30.3k446104












          • elseif id == MATH then inside_math =(n.subtype == 0) end is a bit shorter
            – Herbert
            1 hour ago










          • @Herbert thanks
            – michal.h21
            1 hour ago


















          • elseif id == MATH then inside_math =(n.subtype == 0) end is a bit shorter
            – Herbert
            1 hour ago










          • @Herbert thanks
            – michal.h21
            1 hour ago
















          elseif id == MATH then inside_math =(n.subtype == 0) end is a bit shorter
          – Herbert
          1 hour ago




          elseif id == MATH then inside_math =(n.subtype == 0) end is a bit shorter
          – Herbert
          1 hour ago












          @Herbert thanks
          – michal.h21
          1 hour ago




          @Herbert thanks
          – michal.h21
          1 hour ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468153%2fmarking-end-of-words-in-luatex-problem-with-math%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Understanding the information contained in the Deep Space Network XML data?

          Ross-on-Wye

          Eastern Orthodox Church