Closed tobiasBora closed 3 years ago
Sorry for the late reply, this got lost somehow.
There is no built-in method for this. For non-math, one can use org-mode #+MACRO
statements, but those don't work in math contexts.
One possible solution would be to use a Lua filter to replace certain commands in math contexts. E.g.:
function Math (m)
m.text = m.text:gsub('\\water', '{H_2O}')
return m
end
With a bit of work, this could be extended to build a full macro system for math.
Please reach out on the pandoc-discuss mailing list for more help to get this working.
Thanks for your answer. Lua filters could be an idea, but they have three issues:
But this solution also has some advantages, notably I guess it's more portable than just pasting the macros at the beginning of the html/tex files.
But it's better than nothing, thanks for the trick!
Well, pandoc already includes a LaTeX parser, and we can use it from within Lua filters. So we could just use that!
Below is a (hacky!) filter which add the definitions from the header to each math element, parses the resulting string as LaTeX, and returns the resulting math.
local latex_headers
function get_latex(metaval)
return table.concat(
pandoc.List.map(
metaval,
function (v) return v.t == 'RawInline' and v.text or '' end
),
'\n'
)
end
function Meta (meta)
latex_headers = meta['header-includes']:map(get_latex)
end
function Math (m)
local delim = m.mathtype == 'DisplayMath' and '$$' or '$'
local latex = table.concat(latex_headers, '\n')
.. '\n' .. delim .. m.text .. delim .. '\n'
local blocks = pandoc.read(latex, 'latex').blocks
return pandoc.utils.blocks_to_inlines(blocks)
end
return {{Meta = Meta}, {Math = Math}}
This should give you the desired result. Probably requires some refinement, but should get you started.
LaTeX macros are used very frequently. Unfortunately, I can't find any better way than doing (in
org-mode
):The first
#+LaTeX_HEADER
is added to the.tex
header, while the second$\def\water{H_2O}$
is of interest formathjax
, that way both these commands work:Unfortunately, if I try to create an epub, for example using:
I get errors two times:
\def\water{H_2O}
: the website don't know how to proceed it\water
: the website does not know where this macro is definedSo would it be possible to provide a more unified way of defining that some
tex
code correspond to macros, and provide this code automatically towebtex
?Thanks!