![帮助使用 Lua 过滤器通过 Pandoc 将 markdown 转换为 LaTeX](https://linux22.com/image/462896/%E5%B8%AE%E5%8A%A9%E4%BD%BF%E7%94%A8%20Lua%20%E8%BF%87%E6%BB%A4%E5%99%A8%E9%80%9A%E8%BF%87%20Pandoc%20%E5%B0%86%20markdown%20%E8%BD%AC%E6%8D%A2%E4%B8%BA%20LaTeX.png)
在 markdown(用 Obsidian.md 编写)中,我使用插件/主题,以便*==text==*
将其呈现为红色而不是斜体并突出显示。
我使用这个 lua-filter 将常规转换==highlighting==
为 LaTeX \hl{highlighting}
,效果很好:(我不知何故需要这两个文件,我不记得为什么)
突出显示.lua:
--[[
Add support for a custom inline syntax.
This pandoc Lua filter allows to add a custom markup syntax
extension. It is designed to be adjustable; it should not be
necessary to modify the code below the separator line.
The example here allows to add highlighted text by enclosing the
text with `==` on each side. Pandoc supports this for HTML output
out of the box. Other outputs will need additional filters.
Copyright: © 2022 Albert Krewinkel
License: MIT
]]
-- Lua pattern matching the opening markup string.
local opening = [[==]]
-- Lua pattern matching the closing markup string.
local closing = [[==]]
-- Toggle whether the opening markup may be followed by whitespace.
local nospace = true
-- Function converting the enclosed inlines to their internal pandoc
-- representation.
local function markup_inlines (inlines)
return pandoc.Span(inlines, {class="mark"})
end
------------------------------------------------------------------------
local function is_space (inline)
return inline and
(inline.t == 'Space' or
inline.t == 'LineBreak' or
inline.t == 'SoftBreak')
end
function Inlines (inlines)
local result = pandoc.Inlines{}
local markup = nil
local start = nil
for i, inline in ipairs(inlines) do
if inline.tag == 'Str' then
if not markup then
local first = inline.text:match('^' .. opening .. '(.*)')
if first then
start = inline -- keep element around in case the
-- markup is not closed. Check if the
-- closing pattern is already in this
-- string.
local selfclosing = first:match('(.*)' .. closing .. '$')
if selfclosing then
result:insert(markup_inlines{pandoc.Str(selfclosing)})
elseif nospace and first == '' and is_space(inlines[i+1]) then
-- the opening pattern is followed by a space, but the
-- config disallows this.
result:insert(inline)
else
markup = pandoc.Inlines{pandoc.Str(first)}
end
else
result:insert(inline)
end
else
local last = inline.text:match('(.*)' .. closing .. '$')
if last then
markup:insert(pandoc.Str(last))
result:insert(markup_inlines(markup))
markup = nil
else
markup:insert(inline)
end
end
else
local acc = markup or result
acc:insert(inline)
end
end
-- keep unterminated markup
if markup then
markup:remove(1) -- the stripped-down first element
result:insert(start)
result:extend(markup)
end
return result
end
local function markup_inlines (inlines) return {pandoc.RawInline('tex', '\\hl{')} .. inlines .. {pandoc.RawInline('tex', '}')} end
跨度:
function Span (span)
if span.classes:includes 'mark' then
return {pandoc.RawInline('latex', '\\hl{')} ..
span.content ..
{pandoc.RawInline('latex', '}')}
end
end
这很好用,我在这里得到了它:https://gist.github.com/tarleb/a0646da1834318d4f71a780edaf9f870
然而,对于我的其他用例,我希望它能够呈现*==this kind of highlighting==*
为\colorbox{lightred}{this new kind}
我尝试用 替换\\hl{
但\colorbox{declared-color}{
没有效果。
我想知道这是否与课程有关?但将该课程从 重命名mark
为mark-red
也不起作用。
这是我的Latex 分子动力学:
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[a4paper, 12pt, oneside]{article}
\usepackage[a4paper, portrait, margin=1in]{geometry}
\begin{document}
Lorem or whatever.
$body$
\end{document}
我使用这个作为模板将 Markdown 转换为 LaTeX,并用来$body$
自我的 Markdown 文件的文本替换。
我如何转换*==this==*
为\colorbox{lightred}{this}
?
有什么建议或想法吗?谢谢!