CB100

Notas, resueltos y tps de la materia Algoritmos y Estructuras de Datos
Index Commits Files Refs README
guias/complejidad_algoritmica/fix-math.lua (2916B)
   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 
  54         elseif blk.t == 'BulletList' or blk.t == 'OrderedList' then
  55             -- process each item recursively
  56             for i, item in ipairs(blk.content) do
  57                 blk.content[i] = walk_blocks(item)
  58             end
  59             table.insert(new_blocks, blk)
  60 
  61         elseif blk.t == 'Item' then
  62             blk.content = walk_blocks(blk.content)
  63             table.insert(new_blocks, blk)
  64         else
  65             table.insert(new_blocks, blk)
  66         end
  67     end
  68     return new_blocks
  69 end
  70 
  71 function Pandoc(doc)
  72     doc.blocks = walk_blocks(doc.blocks)
  73     return doc
  74 end