pandoc lua filter to replace tex macro

帅比萌擦擦* 提交于 2019-12-24 04:04:10

问题


I am trying to write a pandoc lua filter to replace the \ce{} command from the latex package mhchem.

This is what I tried with the example of \ce{NO3-}, but it doesn't work and renders a blank in the rtf output file:

return {
  {
    Str = function (elem)
      if elem.text == "\\ce%{%NO3-%}%" then
        return {pandoc.Str "NO3"}
      else
        return elem
      end
    end,
  }
}

My pandoc command is:

pandoc -s myfile.tex --lua-filter myfilter.lua -o myfile.rtf

回答1:


The main issue here is how pandoc handles these mhchem snippets: pandoc, by default, drops all LaTeX code which it cannot parse. Example:

$ printf 'Nitrate (\\ce{NO3-})' | pandoc --from latex -t native
[Para [Str "Nitrate",Space,Str "()"]]

We want to keep those snippets, which we can by using the raw_tex extension:

$ printf 'Nitrate (\\ce{NO3-})' | pandoc --from latex+raw_tex -t native
[Para [Str "Nitrate",Space,Str "(",RawInline (Format "latex") "\\ce{NO3-}",Str ")"]]

Now we have a chance of matching this text. As we can see, we need to match on RawInline elements instead of Str:

return {
  {
    RawInline = function (raw)
      local formula = raw.text:match '\\ce{([^ ]+)}'
      if raw.format == 'latex' and formula then
        return pandoc.Str(formula)
      end
    end
  }
}

Which will remove the tex command and render the raw code inside. To match the initial example:

return {
  {
    RawInline = function (raw)
      local formula = raw.text:match '\\ce{NO3%-}'
      if raw.format == 'latex' and formula then
        return pandoc.Str('NO3')
      end
    end
  }
}

Finally, the pandoc command is:

pandoc --from latex+raw_tex -s myfile.tex --lua-filter myfilter.lua -o myfile.rtf

The pattern used for matching wasn't quite correct, as @PaulKulchenko noted. See "Patterns" section of the Lua reference manual.




回答2:


You only need to escape special characters before them rather than before and after and you also need to escape -, but don't need to escape {}, so "\\ce%{%NO3-%}%" should probably be "\\ce{NO3%-}". You can see the list of "magic characters" that need escaping in the Patterns chapter.



来源:https://stackoverflow.com/questions/56387990/pandoc-lua-filter-to-replace-tex-macro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!