From 539728840f7acae3d8e4853af580b6d43baad1e3 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Sat, 23 Dec 2023 12:03:36 +0100 Subject: [PATCH] ox-latex: Fix exporting longtable with multiline header * lisp/ox-latex.el (org-latex-table-row): Use all the rows when constructing header definition. * testing/lisp/test-ox-latex.el (test-ox-latex/longtable): Add new test. Reported-by: Brett Presnell Link: https://orgmode.org/list/87mt9zywco.fsf@localhost --- lisp/ox-latex.el | 14 +++++++++++++- testing/lisp/test-ox-latex.el | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index 41c2d3994..1458ded02 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -4046,6 +4046,18 @@ a communication channel." (org-export-get-previous-element table-row info) info)) "") (t "\\midrule")) + ;; Memorize table header in case it is multiline. We need this + ;; information to define contents before "\\endhead" in longtable environments. + (when (org-export-table-row-in-header-p table-row info) + (let ((table-head-cache (plist-get info :org-latex-table-head-cache))) + (unless (hash-table-p table-head-cache) + (setq table-head-cache (make-hash-table :test #'eq)) + (plist-put info :org-latex-table-head-cache table-head-cache)) + (if-let ((head-contents (gethash (org-element-parent table-row) table-head-cache))) + (puthash (org-element-parent table-row) (concat head-contents org-latex-line-break-safe "\n" contents) + table-head-cache) + (puthash (org-element-parent table-row) contents table-head-cache)))) + ;; Return LaTeX string as the transcoder. (concat ;; When BOOKTABS are activated enforce top-rule even when no ;; hline was specifically marked. @@ -4075,7 +4087,7 @@ a communication channel." "") (booktabsp "\\toprule\n") (t "\\hline\n")) - contents + (gethash (org-element-parent table-row) (plist-get info :org-latex-table-head-cache)) (if booktabsp "\\midrule" "\\hline") (if booktabsp "\\midrule" "\\hline") columns diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el index 5c58cce68..35e37e46a 100644 --- a/testing/lisp/test-ox-latex.el +++ b/testing/lisp/test-ox-latex.el @@ -58,5 +58,40 @@ lorem ipsum dolor\\\\[0pt] lorem ipsum dolor\\\\[0pt] \\end{verse}")))) +(ert-deftest test-ox-latex/longtable () + "Test table export with longtable environment." + (org-test-with-exported-text + 'latex + "#+attr_latex: :environment longtable +| First | Second | +| Column | Column | +|--------------+--------| +| a | 1 | +| b | 2 | +| \\pagebreak c | 3 | +| d | 4 | +" + (goto-char (point-min)) + (should + (search-forward + "\\begin{longtable}{lr} +First & Second\\\\[0pt] +Column & Column\\\\[0pt] +\\hline +\\endfirsthead")) + (goto-char (point-min)) + (should + (search-forward + "First & Second\\\\[0pt] +Column & Column \\\\[0pt] + +\\hline +\\endhead")) + (goto-char (point-min)) + (should + (search-forward + "\\hline\\multicolumn{2}{r}{Continued on next page} \\\\ +\\endfoot")))) + (provide 'test-ox-latex) ;;; test-ox-latex.el ends here