fix(org-latex): use :around advice on org-latex-table instead of filter

org-export-filter-table-functions timing was unreliable (after! ox-latex
runs too late or filter not called for all table types). Around advice on
org-latex-table is called directly during transcoding, guaranteed to run.
Also switched to with-eval-after-load for more predictable load timing.
This commit is contained in:
2026-02-23 14:57:36 +01:00
parent 46c9327dd0
commit 7b0b092e21

View File

@@ -326,25 +326,26 @@ Bound to cmd+v in org-mode and markdown-mode."
;;; ============================================================
;; LaTeX table export: replace tabular → tabularx{\textwidth}
;; Filter approach: works on rendered LaTeX string, never touches the org buffer,
;; so no hook recursion (the old before-processing-hook + insert approach caused
;; 'max-lisp-eval-depth' via org-element-cache after-change hooks).
(defun my/org-latex-table-to-tabularx (table _backend _info)
"Replace \\begin{tabular} with \\begin{tabularx}{\\textwidth} in LaTeX output."
(when (and (stringp table) (string-match "\\\\begin{tabular}" table))
(setq table (replace-regexp-in-string
"\\\\begin{tabular}{\\([^}]*\\)}"
"\\\\begin{tabularx}{\\\\textwidth}{\\1}"
table))
(setq table (replace-regexp-in-string
"\\\\end{tabular}"
"\\\\end{tabularx}"
table)))
table)
;; Around advice on org-latex-table (more reliable than filter hooks).
;; No buffer modification → no recursion risk.
(defun my/org-latex-table-tabularx (orig-fn table contents info)
"Wrap org-latex-table output: replace tabular with tabularx{\\textwidth}."
(let ((result (funcall orig-fn table contents info)))
(when (stringp result)
(setq result
(replace-regexp-in-string
"\\\\begin{tabular}{\\([^}]*\\)}"
"\\\\begin{tabularx}{\\\\textwidth}{\\1}"
result))
(setq result
(replace-regexp-in-string
"\\\\end{tabular}"
"\\\\end{tabularx}"
result)))
result))
(after! ox-latex
(add-to-list 'org-export-filter-table-functions
#'my/org-latex-table-to-tabularx)
(with-eval-after-load 'ox-latex
(advice-add 'org-latex-table :around #'my/org-latex-table-tabularx)
(add-to-list 'org-latex-packages-alist '("" "tabularx")))
;; Optional: enable booktabs style (horizontal rules in tables)