Skip to content

Commit da85a5e

Browse files
Improve support for LaTeX multi-line formulas
## Details As a follow up from: #6, when LaTeX formulas are multi-line we end up showing the formula on the first line and not overwriting the actual contents, which does not look great. Rather than attempting to overlay the formula as before, use virtual lines above the text itself. This is different from the standard rendering experience of an overlay but I think is generally a nicer experience. This will have some issues if the inline LaTeX is surrounded by other text, which seems like an okay trade off.
1 parent 38f7cbc commit da85a5e

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

demo/demo.gif

8.84 KB
Loading

demo/sample.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@ if __name__ == "__main__":
4040
| Row 3 Item 1 | Row 3 Item 2 |
4141

4242
$\sqrt{3x-1}+(1+x)^2$
43+
44+
$$
45+
f(x,y) = x + \sqrt{y}
46+
f(x,y) = \sqrt{y} + \frac{x^2}{4y}
47+
$$

justfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
demo:
1+
default_zoom := '10'
2+
3+
demo zoom=default_zoom:
24
rm -f demo/demo.gif
35
python demo/record.py \
4-
--zoom 10 \
6+
--zoom {{zoom}} \
57
--file demo/sample.md \
68
--cast demo.cast
79
# https://docs.asciinema.org/manual/agg/usage/

lua/render-markdown/ui.lua

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,18 @@ M.latex = function(root)
121121
end
122122

123123
local latex = vim.treesitter.get_node_text(root, 0)
124-
local expression = vim.trim(vim.fn.system('latex2text', latex))
125-
local extra_space = vim.fn.strdisplaywidth(latex) - vim.fn.strdisplaywidth(expression)
126-
if extra_space < 0 then
127-
return
128-
end
124+
local raw_expression = vim.fn.system('latex2text', latex)
125+
local expressions = vim.split(vim.trim(raw_expression), '\n', { plain = true })
129126

130127
local start_row, start_col, end_row, end_col = root:range()
131-
local virt_text = { expression .. string.rep(' ', extra_space), state.config.highlights.latex }
128+
local virt_lines = vim.tbl_map(function(expression)
129+
return { { vim.trim(expression), state.config.highlights.latex } }
130+
end, expressions)
132131
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, start_col, {
133132
end_row = end_row,
134133
end_col = end_col,
135-
virt_text = { virt_text },
136-
virt_text_pos = 'overlay',
134+
virt_lines = virt_lines,
135+
virt_lines_above = true,
137136
})
138137
end
139138

0 commit comments

Comments
 (0)