-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawtex.lua
59 lines (51 loc) · 1.91 KB
/
rawtex.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function CodeBlock(c)
if (c.classes[1] == "language-rawtex" or c.classes[1] == "rawtex") then
local t = c.text:gsub("\nCopy" .. "$", "")
return {pandoc.RawBlock("latex", t)}
end
end
-- Function to split a string by a delimiter
local function split(inputstr, sep)
if sep == nil then sep = "%s" end
local t = {}
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
table.insert(t, str)
end
return t
end
function CodeBlockMathPlugin(el)
-- Check if the code block has a class "math"
if el.classes:includes("math") then
-- Split the text into lines
local lines = split(el.text, "\n")
-- Remove the first two lines
table.remove(lines, 1) -- Remove first line
table.remove(lines, 1) -- Remove second line
-- Add leading & to each line for proper left alignment
for i, line in ipairs(lines) do
if line ~= "" then
lines[i] = "& " .. line
print(line)
else
lines[i] = "\newline" -- Preserve blank lines as empty strings
end
end
-- Combine the remaining lines with `\\` for LaTeX flalign environment, ensuring the equations are left-aligned
local combined_lines = "\\begin{flalign*}\n" ..
table.concat(lines, " & \\\\\n") ..
"\n\\end{flalign*}"
-- Return the combined lines as a single Math block
return pandoc.Para {pandoc.RawInline('latex', combined_lines)}
end
end
-- Add the split function to the string table for ease of use
string.split = split
function Code(c)
local cb = "\\begin{align*}\n\\chessboard\n\\end{align*}"
if c.text:sub(1, 11) == "\\chessboard" then
return {pandoc.RawInline("latex", cb)}
end
if c.text:sub(1, 1) == "\\" then
return {pandoc.RawInline("latex", c.text)}
end
end