1 -- removes all `$$` surrounding \begin{align} and \begin{align*} contexts; also 2 -- modifies the space after and before equations by adding \vspace{...} 3 -- 4 -- \begin{align} .. \end{align} adds more vertical space before and after than 5 -- $$ .. $$ (or \[ \] in latex notation) 6 7 local eq_before_space = "-1em" 8 local eq_after_space = "0em" 9 local eq_align_before_space = "-0.75em" 10 local eq_align_after_space = "-0.75em" 11 12 local function vspace(amount) 13 return pandoc.RawBlock('latex', '\\vspace{' .. amount .. '}') 14 end 15 16 function walk_blocks(blocks) 17 local new_blocks = {} 18 for _, blk in ipairs(blocks) do 19 if blk.t == 'Para' then 20 -- Paras can contain inline math elements, so check and replace those as well 21 local new_inlines = {} 22 for _, inline in ipairs(blk.content) do 23 if inline.t == 'Math' and inline.mathtype == 'DisplayMath' then 24 table.insert(new_blocks, vspace(eq_before_space)) 25 if inline.text:match('^\\begin{align%*?}') then 26 -- replace inline Math with RawBlock (convert Para to RawBlock) 27 table.insert(new_blocks, vspace(eq_align_before_space)) 28 table.insert(new_blocks, pandoc.RawBlock('latex', inline.text)) 29 table.insert(new_blocks, vspace(eq_align_after_space)) 30 else 31 table.insert(new_blocks, pandoc.RawBlock('latex', '\\[' .. inline.text .. '\\]')) 32 end 33 table.insert(new_blocks, vspace(eq_after_space)) 34 else 35 table.insert(new_inlines, inline) 36 end 37 end 38 -- Only add the Para if there is still content 39 if #new_inlines > 0 then 40 table.insert(new_blocks, pandoc.Para(new_inlines)) 41 end 42 43 elseif blk.t == 'CodeBlock' or blk.t == 'RawBlock' then 44 -- Just keep them as is 45 table.insert(new_blocks, blk) 46 47 elseif blk.t == 'BlockQuote' or blk.t == 'Div' then 48 -- Recurse on nested blocks 49 table.insert(new_blocks, vspace("0.5em")) 50 blk.content = walk_blocks(blk.content) 51 table.insert(new_blocks, blk) 52 table.insert(new_blocks, vspace("0.5em")) 53 else 54 table.insert(new_blocks, blk) 55 end 56 end 57 return new_blocks 58 end 59 60 function Pandoc(doc) 61 doc.blocks = walk_blocks(doc.blocks) 62 return doc 63 end
