diff --git a/config.el b/config.el index 79bce88..af2fcc8 100644 --- a/config.el +++ b/config.el @@ -325,19 +325,48 @@ Bound to cmd+v in org-mode and markdown-mode." ;;; ORG MODE — LATEX EXPORT ;;; ============================================================ -;; 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) - (setq-local org-latex-default-table-environment "tabularx"))) - -(add-hook 'org-export-before-processing-hook #'my/org-set-tabularx) +;; LaTeX table export: tabular → tabularx{\linewidth} +;; Filter on rendered LaTeX string — no buffer modification, no recursion. +;; Handles both cases: +;; \begin{tabular}{lll} → \begin{tabularx}{\linewidth}{lll} +;; \begin{tabularx}{lll} → \begin{tabularx}{\linewidth}{lll} (missing width) +(defun my/org-latex-fix-tabularx (table _backend _info) + "Convert tabular → tabularx{\\linewidth} in LaTeX table transcoder output." + (when (stringp table) + ;; Fix tabularx with missing width (org versions that don't add \linewidth) + (setq table + (replace-regexp-in-string + "\\\\begin{tabularx}{\\([^\\\\][^}]*\\)}" + "\\\\begin{tabularx}{\\\\linewidth}{\\1}" + table)) + ;; Convert plain tabular to tabularx + (setq table + (replace-regexp-in-string + "\\\\begin{tabular}{\\([^}]*\\)}" + "\\\\begin{tabularx}{\\\\linewidth}{\\1}" + table)) + (setq table + (replace-regexp-in-string + "\\\\end{tabular}" + "\\\\end{tabularx}" + table))) + table) +;; Register filter on ox-latex load AND ensure it via a pre-processing hook +;; (belt+suspenders: whichever fires first wins, both are idempotent). (with-eval-after-load 'ox-latex + (add-to-list 'org-export-filter-table-functions #'my/org-latex-fix-tabularx) (add-to-list 'org-latex-packages-alist '("" "tabularx"))) +(defun my/org-ensure-tabularx-filter (backend) + "Force ox-latex load and register tabularx filter before LaTeX export." + (when (org-export-derived-backend-p backend 'latex) + (require 'ox-latex) + (add-to-list 'org-export-filter-table-functions #'my/org-latex-fix-tabularx) + (add-to-list 'org-latex-packages-alist '("" "tabularx")))) + +(add-hook 'org-export-before-processing-hook #'my/org-ensure-tabularx-filter) + ;; Optional: enable booktabs style (horizontal rules in tables) ;; (setq org-latex-tables-booktabs t)