From 7a1498cdd71f72594f239d503c1d29dba773a7a7 Mon Sep 17 00:00:00 2001 From: Daneel Date: Mon, 23 Feb 2026 15:09:27 +0100 Subject: [PATCH] =?UTF-8?q?fix(org-latex):=20setq-local=20instead=20of=20i?= =?UTF-8?q?nsert=20for=20tabularx=20=E2=80=94=20no=20buffer=20modification?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root cause of Emacs 31 recursion: insert in org-export-before-processing-hook triggers hooks that inhibit-modification-hooks does not suppress on Emacs 31 (window management, org-fold text properties, etc.). The old Mac (older Emacs) was not affected. Fix: setq-local org-latex-default-table-environment in the hook. No buffer content modification → no hook chains → no recursion possible. org-latex.el natively handles tabularx by adding \linewidth width argument. tabularx added to org-latex-packages-alist. --- config.el | 51 ++++++++++----------------------------------------- 1 file changed, 10 insertions(+), 41 deletions(-) diff --git a/config.el b/config.el index ac8fc5e..79bce88 100644 --- a/config.el +++ b/config.el @@ -325,49 +325,18 @@ Bound to cmd+v in org-mode and markdown-mode." ;;; ORG MODE — LATEX EXPORT ;;; ============================================================ -;; Count data columns in an Org table line -(defun my/org-count-table-columns (line) - "Count the number of data columns in Org table LINE." - (length (cl-remove-if - (lambda (s) (string-match-p "^-*$" (string-trim s))) - (cdr (butlast (split-string line "|")))))) - -;; Generate tabularx column spec: first column left-aligned, rest Y (auto-width) -(defun my/org-table-attr-latex-spec (ncols) - "Return tabularx column spec for NCOLS columns: first l, rest Y." - (concat "l" (make-string (max 0 (1- ncols)) ?Y))) - -;; Automatically insert #+ATTR_LATEX tabularx before tables on LaTeX export. -;; inhibit-modification-hooks prevents org-element-cache from recursing -;; during the insert (which caused max-lisp-eval-depth on Emacs 31). -(defun my/org-auto-tabularx (backend) - "Insert #+ATTR_LATEX tabularx before each table when exporting to LaTeX." +;; LaTeX table export: use tabularx automatically — no buffer modification. +;; Setting a buffer-local variable in the hook is safe (no insert = no recursion). +;; org-latex.el natively handles tabularx/tabulary by adding \linewidth width arg. +(defun my/org-set-tabularx (backend) + "Set tabularx as default LaTeX table environment for this export." (when (org-export-derived-backend-p backend 'latex) - (let ((inhibit-modification-hooks t)) - (save-excursion - (goto-char (point-min)) - (while (not (eobp)) - (cond - ((looking-at "^|") - (let ((prev-line (save-excursion - (forward-line -1) - (buffer-substring-no-properties - (line-beginning-position) (line-end-position))))) - (when (not (string-match-p "^|" prev-line)) - (when (not (string-match-p "^#\\+ATTR_LATEX" prev-line)) - (let* ((table-line (buffer-substring-no-properties - (line-beginning-position) (line-end-position))) - (ncols (my/org-count-table-columns table-line)) - (spec (my/org-table-attr-latex-spec ncols)) - (attr (format "#+ATTR_LATEX: :environment tabularx :width \\textwidth :align %s\n" - spec))) - (when (> ncols 0) - (insert attr))))) - (forward-line)) - (t - (forward-line))))))))) + (setq-local org-latex-default-table-environment "tabularx"))) -(add-hook 'org-export-before-processing-hook #'my/org-auto-tabularx) +(add-hook 'org-export-before-processing-hook #'my/org-set-tabularx) + +(with-eval-after-load 'ox-latex + (add-to-list 'org-latex-packages-alist '("" "tabularx"))) ;; Optional: enable booktabs style (horizontal rules in tables) ;; (setq org-latex-tables-booktabs t)