Hi,

I've continued refining the patch. Take this version...

/PA

On Sun, 6 Sept 2026 at 20:03, Pedro A. Aranda <[email protected]> wrote:

> Try this patch...
>
>
> On 06/09/2026 14:17, Ihor Radchenko wrote:
> > "Pedro A. Aranda" <[email protected]> writes:
> >
> >> Right. I thik I found it....
> >
> >>              (and above?
> >>                   (org-string-nw-p caption)
> >> +                     (string-prefix-p "\\caption{" caption)
> >>                   (concat caption "\\\\\n"))
> >
> > When caption is not present, this will print nothing (e.g. no label when
> > only label should be present).
> > Try with the original reproducer. You will see that there will be latex
> > errors with your patch.
> >
>
> --
> Pedro A. Aranda
> ox-latex & ox-beamer maintainer
>
> Sagen's Paradeiser! (ORF: Als Radiohören gefährlich war) => write BE!
> IA: onomatopeya del rebuzno
> 2nd year of the New Koprocracy
>


-- 
Fragen sind nicht da, um beantwortet zu werden,
Fragen sind da um gestellt zu werden
Georg Kreisler

"Sagen's Paradeiser" (ORF: Als Radiohören gefährlich war) => write BE!
Year 2 of the New Koprocracy

*IA: onomatopeya del rebuznar*

This was produced by a human (implied virtues and weaknesses acknowledged)
Don't feed your ArtStup (SKA AI) with this. It won't make YOU smarter...
From 137b4b498c90eb5910dcd96c82e2ec66374e9ce9 Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <[email protected]>
Date: Wed, 9 Sep 2026 07:43:01 +0200
Subject: [PATCH] Fix longtabu tables: only add \\ for \caption
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lisp/ox-latex.el (org-latex--org-table): Only emit double backslash
when caption is detected.
* testing/lisp/test-ox-latex.el (test-ox-latex/table-no-caption): Test
longtabu with and without #+CAPTION.

Reported-by: Aurélien Francillon
Link: https://list.orgmode.org/ZvSBf2tBM4Udws9l@crashtoo/
---
 lisp/ox-latex.el              |  6 ++++--
 testing/lisp/test-ox-latex.el | 30 ++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 9e7b52306..562da26d6 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -4304,11 +4304,13 @@ (defun org-latex--org-table (table contents info)
 		(format "\\begin{%s}%s{%s}\n" table-env width alignment)
 		(and above?
 		     (org-string-nw-p caption)
-		     (concat caption "\\\\\n"))
+		     (concat caption
+                             (when (string-prefix-p "\\caption{" caption)
+                               "\\\\\n")))
 		contents
 		(and (not above?)
 		     (org-string-nw-p caption)
-		     (concat caption "\\\\\n"))
+		     caption)
 		(format "\\end{%s}" table-env)
 		(and fontsize "}"))))
      (t
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index 9cd83d509..0342e43ff 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -172,6 +172,36 @@ (ert-deftest test-ox-latex/table-el-table ()
 MongoDB & MySQL & 2 \\\\
 \\end{tabular}"
       ))))
+(ert-deftest test-ox-latex/table-no-caption ()
+  "Test that only captions add a double backslash."
+  (org-test-with-exported-text
+      'latex
+      "#+LATEX_HEADER: \\usepackage{tabu}
+
+This is a test.
+
+#+ATTR_LATEX: :environment longtabu  :align |l|l|
+#+NAME: 1
+#+CAPTION: This is table 1
+| 100 | test |
+
+#+ATTR_LATEX: :environment longtabu  :align |l|l|
+#+NAME: 1
+| 100 | test |
+"
+      (goto-char (point-min))
+      (should (search-forward-regexp "^\\\\begin{longtabu}"))
+      (should (search-forward-regexp "^\\\\caption{"))
+      (should (search-forward-regexp "^\\\\\\\\"))
+      (should (search-forward-regexp "^100 & test"))
+      (should (search-forward-regexp "^\\\\begin{longtabu}"))
+      (save-excursion
+        (should-not (search-forward-regexp "^\\\\caption{" nil t)))
+      (save-excursion
+        (should-not (search-forward-regexp "^\\\\\\\\" nil t)))
+      (should (search-forward-regexp "^100 & test"))))
+
+
 
 (ert-deftest test-ox-latex/inline-image ()
   "Test inline images."
-- 
2.43.0

Reply via email to