Pandoc Lua过滤器替换tex宏 [英] pandoc lua filter to replace tex macro
问题描述
我正在尝试编写pandoc lua过滤器以替换乳胶软件包mhchem中的\ ce {}命令.
这是我在\ce{NO3-}
示例中尝试过的方法,但是它不起作用,并且在rtf输出文件中呈现空白:
return {
{
Str = function (elem)
if elem.text == "\\ce%{%NO3-%}%" then
return {pandoc.Str "NO3"}
else
return elem
end
end,
}
}
我的pandoc命令是:
pandoc -s myfile.tex --lua-filter myfilter.lua -o myfile.rtf
这里的主要问题是pandoc如何处理这些mhchem片段:pandoc默认情况下会丢弃它无法解析的所有LaTeX代码.示例:
$ printf 'Nitrate (\\ce{NO3-})' | pandoc --from latex -t native
[Para [Str "Nitrate",Space,Str "()"]]
我们要保留这些片段,可以使用raw_tex
扩展名:
$ printf 'Nitrate (\\ce{NO3-})' | pandoc --from latex+raw_tex -t native
[Para [Str "Nitrate",Space,Str "(",RawInline (Format "latex") "\\ce{NO3-}",Str ")"]]
现在我们有机会匹配此文本.如我们所见,我们需要匹配RawInline元素而不是Str:
return {
{
RawInline = function (raw)
local formula = raw.text:match '\\ce{([^ ]+)}'
if raw.format == 'latex' and formula then
return pandoc.Str(formula)
end
end
}
}
这将删除tex命令并在其中呈现原始代码. 要匹配最初的示例:
return {
{
RawInline = function (raw)
local formula = raw.text:match '\\ce{NO3%-}'
if raw.format == 'latex' and formula then
return pandoc.Str('NO3')
end
end
}
}
最后,pandoc命令为:
pandoc --from latex+raw_tex -s myfile.tex --lua-filter myfilter.lua -o myfile.rtf
用于匹配的模式不太正确,如@PaulKulchenko指出的那样.请参见Lua参考手册的模式"部分. /p>
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
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.
这篇关于Pandoc Lua过滤器替换tex宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!