1

I want my latex template to recognize the following Markdown syntax (Admonition):

!!! note "My Note Title"
    This is
    a note with
    multible lines

Not part of the note.

These Admonition notes start with !!! followed by a keyword like note, warning, danger which indicates what the background color should be. The Title of the note is in quotation-marks "My Note Title".

The content of the note is indented by three tabs. Only these lines should be added to the content-box.

This code should be styled to look simular to this:

Admonition note styling

I tried using luatex and 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" ) }}

My code only recognizes the !!! but I also need the indented text to be styled.

Thanks for your help!

Fabio (Germany)

Context: I want to parse my markdowns files with admonition syntax to pdf. I use pandoc with a custom latex template.

2

Here's a solution that builds on David's earlier answer to providing fancy note, danger, and warning boxes.

enter image description here

% !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}
| improve this answer | |
  • oh no, you stole my tick, happy new year:-) – David Carlisle Jan 5 '17 at 17:31
  • @DavidCarlisle - Sorry 'bout that. Happy New Year to you too. – Mico Jan 5 '17 at 19:23
3

Using X not tab here as tabs don't survive posting to the site.

enter image description here

\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}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.