Skip to content

Commit 138a796

Browse files
Add basic level of support for showing rendered latex using pylatexenc library
1 parent f5917d2 commit 138a796

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ Plugin to improve viewing Markdown files in Neovim
1212
- Highlights code blocks to better stand out
1313
- Replaces whichever style bullet point is being used with provided character
1414
- Updates table borders with better border characters, does NOT automatically align
15+
- Basic support for `LaTeX` if `pylatexenc` is installed on system
1516

1617
# Dependencies
1718

1819
- [markdown](https://github.com/tree-sitter-grammars/tree-sitter-markdown) parser for
1920
[treesitter](https://github.com/nvim-treesitter/nvim-treesitter): Used to parse
2021
`markdown` files
22+
- [pylatexenc](https://pypi.org/project/pylatexenc/) to transform `LaTeX` strings to
23+
appropriate unicode using `latex2text`, not a mandatory dependency
2124

2225
# Install
2326

@@ -96,6 +99,7 @@ require('render-markdown').setup({
9699
-- Used when displaying non header rows in a markdown table
97100
row = 'Normal',
98101
},
102+
latex = 'Special',
99103
},
100104
})
101105
```

demo/sample.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ if __name__ == "__main__":
3535
| Row 1 Item 1 | Row 1 Item 2 |
3636
| Row 2 Item 1 | Row 2 Item 2 |
3737
| Row 3 Item 1 | Row 3 Item 2 |
38+
39+
$\sqrt{3x-1}+(1+x)^2$

lua/render-markdown/init.lua

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ local M = {}
1616
---@field public code? string
1717
---@field public bullet? string
1818
---@field public table? UserTableHighlights
19+
---@field public latex? string
1920

2021
---@class UserConfig
2122
---@field public markdown_query? string
@@ -71,6 +72,7 @@ function M.setup(opts)
7172
head = '@markup.heading',
7273
row = 'Normal',
7374
},
75+
latex = 'Special',
7476
},
7577
}
7678
state.enabled = true
@@ -134,16 +136,18 @@ M.refresh = function()
134136
vim.treesitter.get_parser():for_each_tree(function(tree, language_tree)
135137
local language = language_tree:lang()
136138
if language == 'markdown' then
137-
M.handle_markdown(tree)
139+
M.handle_markdown(tree:root())
140+
elseif language == 'latex' then
141+
M.handle_latex(tree:root())
138142
end
139143
end)
140144
end
141145

142-
---@param tree TSTree
143-
M.handle_markdown = function(tree)
146+
---@param root TSNode
147+
M.handle_markdown = function(root)
144148
local highlights = state.config.highlights
145149
---@diagnostic disable-next-line: missing-parameter
146-
for id, node in state.markdown_query:iter_captures(tree:root(), 0) do
150+
for id, node in state.markdown_query:iter_captures(root, 0) do
147151
local capture = state.markdown_query.captures[id]
148152
local value = vim.treesitter.get_node_text(node, 0)
149153
local start_row, start_col, end_row, end_col = node:range()
@@ -212,4 +216,27 @@ M.handle_markdown = function(tree)
212216
end
213217
end
214218

219+
---@param root TSNode
220+
M.handle_latex = function(root)
221+
if vim.fn.executable('latex2text') ~= 1 then
222+
return
223+
end
224+
225+
local latex = vim.treesitter.get_node_text(root, 0)
226+
local expression = vim.trim(vim.fn.system('latex2text', latex))
227+
local extra_space = vim.fn.strdisplaywidth(latex) - vim.fn.strdisplaywidth(expression)
228+
if extra_space < 0 then
229+
return
230+
end
231+
232+
local start_row, start_col, end_row, end_col = root:range()
233+
local virt_text = { expression .. string.rep(' ', extra_space), state.config.highlights.latex }
234+
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, start_col, {
235+
end_row = end_row,
236+
end_col = end_col,
237+
virt_text = { virt_text },
238+
virt_text_pos = 'overlay',
239+
})
240+
end
241+
215242
return M

lua/render-markdown/state.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
---@field public code string
1212
---@field public bullet string
1313
---@field public table TableHighlights
14+
---@field public latex string
1415

1516
---@class Config
1617
---@field public markdown_query string

0 commit comments

Comments
 (0)