From e27ba726b15e71eca700141c2030ac858bc8025c Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 19 Feb 2025 06:48:02 +0100 Subject: [PATCH] feat(image): make math packages configurable. Closes #1295 --- lua/snacks/image/doc.lua | 3 ++- lua/snacks/image/init.lua | 5 +++++ tests/image/test.tex | 10 ++++++++++ tests/image/test.typ | 5 +++-- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lua/snacks/image/doc.lua b/lua/snacks/image/doc.lua index ce38c6db..5d9575de 100644 --- a/lua/snacks/image/doc.lua +++ b/lua/snacks/image/doc.lua @@ -40,7 +40,8 @@ M.transforms = { if not content:find("^\\begin") then content = ("\\[%s\\]"):format(content) end - local packages = { "xcolor", "amsmath", "amssymb" } + local packages = { "xcolor" } + vim.list_extend(packages, Snacks.image.config.convert.math.packages) for _, line in ipairs(vim.api.nvim_buf_get_lines(ctx.buf, 0, -1, false)) do if line:find("\\usepackage") then for _, p in ipairs(vim.split(line:match("{(.-)}") or "", ",%s*")) do diff --git a/lua/snacks/image/init.lua b/lua/snacks/image/init.lua index 3f035479..86b4e791 100644 --- a/lua/snacks/image/init.lua +++ b/lua/snacks/image/init.lua @@ -103,6 +103,11 @@ local defaults = { ---@class snacks.image.convert.Config convert = { notify = true, -- show a notification on error + math = { + -- for latex documents, the doc packages are included automatically, + -- but you can add more packages here. Useful for markdown documents. + packages = { "amsmath", "amssymb", "amsfonts", "amscd", "mathtools", "physics", "siunitx", "mhchem" }, + }, ---@type snacks.image.args mermaid = function() local theme = vim.o.background == "light" and "neutral" or "dark" diff --git a/tests/image/test.tex b/tests/image/test.tex index 54b94977..7f9ca2f3 100644 --- a/tests/image/test.tex +++ b/tests/image/test.tex @@ -2,6 +2,7 @@ \usepackage{graphicx} % For images \usepackage{amsmath} % For math symbols \usepackage{amssymb} % For extra math symbols +\usepackage{multirow} % For extra math symbols \begin{document} @@ -20,8 +21,17 @@ Inline image: \newpage + \section{Math Tests} + +$ + cases( + E_x = E_0 e^(j omega t - y z), + H_y = gamma / (j omega mu_0) E_x = eta^e E_0 e^(j omega - y z) = H_0 e^(j omega t - y z) + ) +$ + Inline math: \(E=mc^2$ and $\int_0^1 x^2 \,dx\) Inline math: $E=mc^2$ and $\int_0^1 x^2 \,dx$ diff --git a/tests/image/test.typ b/tests/image/test.typ index 9723386c..529193a3 100644 --- a/tests/image/test.typ +++ b/tests/image/test.typ @@ -11,7 +11,7 @@ #set text(size: 12pt, fill: rgb("#FF0000")) $ 5 + 5 = 10 $ -$ E = m c^2 $ +$ E = g c^2 $ $ A = pi r^2 $ @@ -21,7 +21,8 @@ $ cal(A) := { x in RR | x "is natural" } $ #let x = 5 -$ #x < 17 $ + +$ x < 17 $ $ (3x + y) / 7 &= 9 && "given" \ 3x + y &= 63 & "multiply by 7" \