我希望我的 Latex 模板能够识别以下 Markdown 语法(警告):
!!! note "My Note Title"
This is
a note with
multible lines
Not part of the note.
这些警告注释以 开头,!!!
后面跟着一个关键字,例如,note, warning, danger
它表示背景颜色应该是什么。注释的标题用引号括起来,即“我的注释标题”。
注释内容以三个制表符缩进。只有这几行才应该添加到内容框中。
该代码的样式应与此类似:
我尝试使用 luatex 和 luacode。
\begin{luacode}
function triplebang ( s )
return ( string.gsub ( s , '!!!',
'\\par\\bigskip\\noindent\\textcolor{red}{!!! ' ..
'This is an admonition note.}\\par\\bigskip ' ) )
end
\end{luacode}
\AtBeginDocument{\directlua{luatexbase.add_to_callback (
"process_input_buffer", triplebang, "triplebang" ) }}
我的代码只能识别!!!
但我还需要对缩进的文本进行样式设置。
感谢您的帮助!
法比奥(德国)
上下文:我想使用 admonition 语法将我的 markdown 文件解析为 pdf。我使用带有自定义 latex 模板的 pandoc。
答案1
这是一个基于 David 之前的回答的解决方案,旨在提供精美的注释、危险和警告框。
% !TeX program = lualatex
\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{tcolorbox,marvosym,xstring}
\newcommand\boxcolor{}
\newcommand\mybox[3]{%
\IfStrEq{#1}{note}{\renewcommand\boxcolor{CornflowerBlue}}{%
\IfStrEq{#1}{danger}{\renewcommand\boxcolor{Tomato}}{%
\IfStrEq{#1}{warning}{\renewcommand\boxcolor{Crimson}}{}}}
\begin{tcolorbox}[colback=\boxcolor!15!white,
colframe=\boxcolor,
title=\sffamily\bfseries\Info\quad #2]
\sffamily #3
\end{tcolorbox}}
\usepackage{luacode}
\begin{luacode}
function triplebang (s)
if not (boxedflag) then
if string.find ( s , '^!!!' ) then
boxedflag=true
return string.gsub ( s, '^!!! (%a*) "(.*)"$', '\\mybox{%1}{%2}{' )
else
return s
end
else
if string.find (s , '^XXX' ) then
return string.sub ( s , 4 )
else
boxedflag=false
return '}' .. s
end
end
end
\end{luacode}
\def\startlooking{\directlua{%
boxedflag=false
luatexbase.add_to_callback(
'process_input_buffer', triplebang , 'triplebang' )}}
\def\stoplooking{\directlua{%
luatexbase.remove_from_callback(
'process_input_buffer' , 'triplebang' )}}
\begin{document}
\startlooking
Not part of the note.
!!! note "A Note Title"
XXXThis is
XXXa note with
XXXmultiple lines
XXX
XXXpossibly containing
XXXparagraph breaks.
Not part of the note either.
!!! danger "A Danger Title"
XXXThis is
XXXanother note with
XXXmultiple lines.
Further non-note text.
!!! warning "A Warning Title"
XXXThis is yet
XXXanother note with
XXXmultiple lines.
Still more non-note text.
\stoplooking
\end{document}
答案2
此处不使用X
标签,因为标签在发布到网站后不会保留。
\documentclass{article}
\long\def\mybox#1#2#3{%
\par\fbox{\parbox{5cm}{\fbox{#1 --- #2}\par#3}}\par}
{\catcode`\%=12
\directlua{
function boxed (s)
if not(boxedflag) then
i=string.find(s,'^!!!')
if(i==1) then
boxedflag=true
print('A' ..string.gsub(s,'^!!! (%a*) "(.*)"', '\string\\mybox{%1}{%2}['))
return string.gsub(s,'^!!! (%a*) "(.*)"', '\string\\mybox{%1}{%2}{')
else
return s
end
else
i=string.find(s,'^XXX')
if i==1 then
print('C' .. s)
return string.gsub(s,'^XXX','')
else
print('B' .. s)
boxedflag=false
return '}' .. s
end
end
end}
}
\def\startlooking{%
\directlua{
boxedflag=false
luatexbase.add_to_callback('process_input_buffer',boxed,'find box markup')}}
\def\stoplooking{%
\directlua{luatexbase.remove_from_callback('process_input_buffer','find box markup')}}
\begin{document}
\startlooking
Not part of the note.
!!! note "My Note Title"
XXXThis is
XXXa note with
XXXmultible lines
Not part of the note.
\stoplooking
\end{document}