fix(org-latex): setq-local instead of insert for tabularx — no buffer modification
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.
This commit is contained in:
51
config.el
51
config.el
@@ -325,49 +325,18 @@ Bound to cmd+v in org-mode and markdown-mode."
|
|||||||
;;; ORG MODE — LATEX EXPORT
|
;;; ORG MODE — LATEX EXPORT
|
||||||
;;; ============================================================
|
;;; ============================================================
|
||||||
|
|
||||||
;; Count data columns in an Org table line
|
;; LaTeX table export: use tabularx automatically — no buffer modification.
|
||||||
(defun my/org-count-table-columns (line)
|
;; Setting a buffer-local variable in the hook is safe (no insert = no recursion).
|
||||||
"Count the number of data columns in Org table LINE."
|
;; org-latex.el natively handles tabularx/tabulary by adding \linewidth width arg.
|
||||||
(length (cl-remove-if
|
(defun my/org-set-tabularx (backend)
|
||||||
(lambda (s) (string-match-p "^-*$" (string-trim s)))
|
"Set tabularx as default LaTeX table environment for this export."
|
||||||
(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."
|
|
||||||
(when (org-export-derived-backend-p backend 'latex)
|
(when (org-export-derived-backend-p backend 'latex)
|
||||||
(let ((inhibit-modification-hooks t))
|
(setq-local org-latex-default-table-environment "tabularx")))
|
||||||
(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)))))))))
|
|
||||||
|
|
||||||
(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)
|
;; Optional: enable booktabs style (horizontal rules in tables)
|
||||||
;; (setq org-latex-tables-booktabs t)
|
;; (setq org-latex-tables-booktabs t)
|
||||||
|
|||||||
Reference in New Issue
Block a user