Skip to content

Commit a06bf65

Browse files
committed
Fix fractions
1 parent 441f3b2 commit a06bf65

File tree

1 file changed

+17
-51
lines changed

1 file changed

+17
-51
lines changed

lua/neorg/modules/core/latex/renderer/module.lua

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ local function create_latex_source(snippet, is_inline)
7474
if is_inline then
7575
content = string.gsub(content, "^%$|", "")
7676
content = string.gsub(content, "|%$$", "")
77-
-- Force display math style for clearer fractions
77+
78+
-- --- FIX #1: ENSURE PROPER DISPLAY MATH ENVIRONMENT ---
79+
-- Wrap content in \[ ... \] to force display math mode, mirroring snacks.nvim's
80+
-- robust behavior and preventing typesetting errors.
7881
if not content:find("\\begin") then
79-
content = "\\displaystyle " .. content
82+
content = "\\[" .. content .. "\\]"
8083
end
8184
end
8285

83-
-- --- FIX 1: TIGHTENED LATEX TEMPLATE ---
84-
-- Use varwidth and a 0pt border to create a tightly cropped image, matching snacks.nvim's strategy.
8586
local template = [[
8687
\documentclass[preview,border=0pt,varwidth,12pt]{standalone}
8788
\usepackage{amsmath, amssymb, amsfonts, amscd, mathtools, xcolor}
@@ -105,58 +106,26 @@ module.load = function()
105106
end
106107
placement = snacks_placement
107108

108-
-- --- FIX 2: CONDITIONAL MONKEY-PATCH FOR INLINE COLLAPSE ---
109-
-- This new patch restores snacks.nvim's logic to keep short equations on one line
110-
-- with trailing text, while allowing taller equations (like fractions) to expand
111-
-- vertically when space is available.
109+
-- --- FIX #2: REMOVE THE AGGRESSIVE INLINE COLLAPSE MONKEY-PATCH ---
110+
-- The original patch forced multi-line fractions into a single row, causing
111+
-- visual distortion. By removing it, we allow snacks.nvim's default, correct
112+
-- rendering to take over.
113+
-- (The empty `do ... end` block below is intentional; we are no longer patching `placement:state`)
112114
do
113-
if not placement._conditional_math_collapse then
114-
placement._conditional_math_collapse = true
115-
local orig_state = placement.state
116-
117-
-- Helper to check for text after the image range on the same line.
118-
local function has_trailing_text(buf, range)
119-
if not range or range[1] ~= range[3] then
120-
return false
121-
end
122-
local line = vim.api.nvim_buf_get_lines(buf, range[1] - 1, range[1], false)[1] or ""
123-
return line:sub(range[4] + 1):find("%S") ~= nil
124-
end
125-
126-
function placement:state()
127-
local st = orig_state(self)
128-
129-
-- Only apply this logic to inline math images.
130-
if self.opts and self.opts.inline and self.opts.type == "math" then
131-
local trailing = has_trailing_text(self.buf, self.opts.range)
132-
133-
-- If there is trailing text and the math is short, collapse it to a single row
134-
-- to keep it on the same line as the text. This is snacks.nvim's heuristic.
135-
if trailing and st.loc.height <= 3 and st.loc.height > 1 then
136-
local w, h = st.loc.width, st.loc.height
137-
st.loc.width = math.ceil(w / h) + 2
138-
st.loc.height = 1
139-
end
140-
-- If there is no trailing text, the original multi-row size is kept, allowing
141-
-- fractions to render with sufficient height.
142-
end
143-
144-
return st
145-
end
146-
end
115+
-- No-op: The problematic monkey-patch has been removed.
147116
end
148117

149-
-- --- FIX 1: IMPROVED IMAGEMAGICK SETTINGS ---
150118
-- Use snacks.nvim's high-density strategy for crisp, correctly-sized images.
151119
local read_density = 192
152120
local magick_args = {
153121
"-density",
154122
read_density,
155-
"{src}", -- Rasterize first page of PDF
123+
"{src}[0]", -- Rasterize first page of PDF
156124
"-background",
157125
"none", -- Use transparent background for inline math
158126
"-trim", -- Remove whitespace
159-
-- Do NOT reset output density. Snacks uses the high DPI to render crisply.
127+
"-alpha",
128+
"remove",
160129
}
161130
local snacks_image = require("snacks.image")
162131
snacks_image.config.convert.magick.pdf = magick_args
@@ -194,15 +163,13 @@ module.load = function()
194163
end)
195164
end
196165

197-
-- --- OPTIONAL TWEAK: Adjusted window bounds ---
198-
-- Slightly reduce the max height for inline math to encourage the collapse heuristic.
199166
local function window_bounds(inline)
200167
local cols = vim.api.nvim_win_get_width(0)
201168
local rows = vim.api.nvim_win_get_height(0)
202169
if inline then
203-
return math.floor(cols * 0.85), math.max(2, math.floor(rows * 0.15))
170+
return math.floor(cols * 0.85), math.floor(rows * 0.35)
204171
else
205-
return math.floor(cols * 0.90), math.floor(rows * 0.35)
172+
return math.floor(cols * 0.90), math.floor(rows * 0.50)
206173
end
207174
end
208175

@@ -230,8 +197,7 @@ function module.private.update_placements(buf)
230197
end
231198

232199
local snippet = module.required["core.integrations.treesitter"].get_node_text(node, buf)
233-
local check_len = #snippet + 2
234-
if check_len < module.config.public.min_length then
200+
if #snippet < module.config.public.min_length then
235201
return
236202
end
237203

0 commit comments

Comments
 (0)