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