From 7b0b092e2160f58ce157baf9cc1ada8e507e448f Mon Sep 17 00:00:00 2001 From: Daneel Date: Mon, 23 Feb 2026 14:57:36 +0100 Subject: [PATCH] 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. --- config.el | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/config.el b/config.el index 2fb5b6d..2dde320 100644 --- a/config.el +++ b/config.el @@ -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)