added latex export post processing for org

This commit is contained in:
Martin Sukany
2026-02-20 10:41:58 +01:00
parent e9e0ab4e0b
commit b51f0c634a

View File

@@ -712,3 +712,60 @@
;; TRAMP cache — neopakovat drahé remote dotazy ;; TRAMP cache — neopakovat drahé remote dotazy
(setq remote-file-name-inhibit-cache nil (setq remote-file-name-inhibit-cache nil
tramp-verbose 1) tramp-verbose 1)
;; Latex org export - hack pro spravne tabulky
(defun my/org-count-table-columns (line)
"Spočítej počet datových sloupců v Org table LINE."
(length (cl-remove-if
(lambda (s) (string-match-p "^-*$" (string-trim s)))
(cdr (butlast (split-string line "|"))))))
(defun my/org-table-attr-latex-spec (ncols)
"Vygeneruj column spec pro tabularx: první sloupec l, zbytek Y (centered X)."
(concat "l" (make-string (max 0 (1- ncols)) ?Y)))
(defun my/org-auto-tabularx (backend)
"Automaticky přidej #+ATTR_LATEX tabularx před každou tabulku při LaTeX exportu."
(when (org-export-derived-backend-p backend 'latex)
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(cond
;; Řádek začíná | — může být začátek tabulky
((looking-at "^|")
(let ((prev-line (save-excursion
(forward-line -1)
(buffer-substring-no-properties
(line-beginning-position) (line-end-position)))))
;; Je to PRVNÍ řádek tabulky? (předchozí řádek NEzačíná |)
(when (not (string-match-p "^|" prev-line))
;; Chybí #+ATTR_LATEX?
(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)))))))
;; Zaregistruj hook — spustí se před každým exportem
(add-hook 'org-export-before-processing-hook #'my/org-auto-tabularx)
;; Volitelně: zapni booktabs styl (horizontal rules v tabulkách)
;; (setq org-latex-tables-booktabs t)
;;
;; Jak použít:
;; 1. Zkopíruj tento obsah do ~/.config/doom/config.el
;; 2. Spusť: doom sync (nebo M-x doom/reload)
;; 3. Exportuj dokument: SPC m e l p
;; Tabulky se automaticky obalí do tabularx — nic nemusíš přidávat ručně.
;;
;; Chceš jiný výchozí column spec? Uprav my/org-table-attr-latex-spec.
;; Například pro "všechny sloupce rovnoměrně": (make-string ncols ?Y)