restore: original org-auto-tabularx with inhibit-modification-hooks (worked on Mac1)
Restore the hook-based approach fromacb3875(original working version) + the inhibit-modification-hooks fix from457b9d3that made it work on Emacs 31.457b9d3confirmed working by Martin (PDF exported) — only issue was the syntax warning from the stray paren. This commit has correct paren balance. Also keep pdf-tools config and corfu-terminal Emacs 31 guard from98b53cb.
This commit is contained in:
50
config.el
50
config.el
@@ -325,13 +325,49 @@ Bound to cmd+v in org-mode and markdown-mode."
|
|||||||
;;; ORG MODE — LATEX EXPORT
|
;;; ORG MODE — LATEX EXPORT
|
||||||
;;; ============================================================
|
;;; ============================================================
|
||||||
|
|
||||||
;; LaTeX table export: use tabularx as default environment.
|
;; Count data columns in an Org table line
|
||||||
;; Org natively supports tabularx: when environment is "tabularx" or "tabulary",
|
(defun my/org-count-table-columns (line)
|
||||||
;; it automatically inserts the required width argument (\linewidth).
|
"Count the number of data columns in Org table LINE."
|
||||||
;; No buffer modification, no advice, no filter needed.
|
(length (cl-remove-if
|
||||||
(after! ox-latex
|
(lambda (s) (string-match-p "^-*$" (string-trim s)))
|
||||||
(setq org-latex-default-table-environment "tabularx")
|
(cdr (butlast (split-string line "|"))))))
|
||||||
(add-to-list 'org-latex-packages-alist '("" "tabularx")))
|
|
||||||
|
;; 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)
|
||||||
|
(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)))))))))
|
||||||
|
|
||||||
|
(add-hook 'org-export-before-processing-hook #'my/org-auto-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