This was asked on Emacs SE at
https://emacs.stackexchange.com/questions/85513/org-mode-export-to-latex-with-rmlines
by user Marko.

Exporting a table.el table with :rmlines set is buggy. In this case, all
the extra `\hline`s that are produced for such tables are supposed to be
deleted, except for the second one which (usually) separates the header
from the body of the table.

The relevant code looks like this:

--8<---------------cut here---------------start------------->8---
    ...
    (let ((attr (org-export-read-attribute :attr_latex table))
      (caption (org-latex--caption/label-string table info))
      (above? (org-latex--caption-above-p table info)))
      (when (plist-get attr :rmlines)
    ;; When the "rmlines" attribute is provided, remove all hlines
    ;; but the one separating heading from the table body.
    (let ((n 0) (pos 0))
      (while (and (< (length output) pos)
              (setq pos (string-match "^\\\\hline\n?" output pos)))
        (cl-incf n)
        (unless (= n 2) (setq output (replace-match "" nil nil output))))))
    ...
--8<---------------cut here---------------end--------------->8---

and it contains two bugs.

The first one is the `<' when we check whether the length of the output
allows us to check for more instances: it should be `>'.

The second bug is what happens for the second `\hline': when n=2, we
don't remove it, but we don't change `pos', so the *next* iteration of
the loop finds the same `\hline' instance, but now n=3, so we go ahead
and delete it anyway (as well as all subsequent ones), leaving *no*
`\hline's in the output.

See attachment for a patch (plus a test).

Emacs  : GNU Emacs 31.0.50 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.49, cairo version 1.18.2)
 of 2026-02-18
Package: Org mode version 9.8-pre (release_9.7.39-700-g93b795 @ /home/nick/src/emacs/org/org-mode/lisp/)
--
Nick
From c456f3710bce453a04b9cd18c83c22bcc5286c0b Mon Sep 17 00:00:00 2001
From: Nick Dokos <[email protected]>
Date: Fri, 20 Feb 2026 21:43:04 -0500
Subject: [PATCH] org-latex--table.el-table: Fix :rmlines behavior for table.el
 export

* lisp/ox-latex.el (org-latex--table.el-table): If :rmlines is set,
reverse the sense of the inequality for the end-of-ouput check and
ensure preservation of the second `\hline'.

* testing/lisp/test-ox-latex.el (test-ox-latex/table-el-table): Add
test.

See https://emacs.stackexchange.com/questions/85513/org-mode-export-to-latex-with-rmlines

Reported-by: Marko
---
 lisp/ox-latex.el              |  6 ++++--
 testing/lisp/test-ox-latex.el | 28 ++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index d134b164f5bc..3db1903d0b98 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -4120,10 +4120,12 @@ property."
 	;; When the "rmlines" attribute is provided, remove all hlines
 	;; but the one separating heading from the table body.
 	(let ((n 0) (pos 0))
-	  (while (and (< (length output) pos)
+	  (while (and (> (length output) pos)
 		      (setq pos (string-match "^\\\\hline\n?" output pos)))
 	    (cl-incf n)
-	    (unless (= n 2) (setq output (replace-match "" nil nil output))))))
+	    (if (= n 2)
+                (cl-incf pos)
+              (setq output (replace-match "" nil nil output))))))
       (org-latex--decorate-table output attr caption above? info))))
 
 (defun org-latex--math-table (table info)
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index de663c8e42c0..28ccfe725409 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -81,6 +81,7 @@ lorem ipsum dolor\\\\
 lorem ipsum dolor\\\\
 \\end{verse}")))
   ;; Footnotes inside verse blocks
+
   (org-test-with-exported-text
       'latex
       "#+begin_verse
@@ -143,6 +144,33 @@ Column & Column \\\\
       "\\hline\\multicolumn{2}{r}{Continued on next page} \\\\
 \\endfoot"))))
 
+(ert-deftest test-ox-latex/table-el-table ()
+  "Test table export with table.el table and :rmlines."
+  (org-test-with-exported-text
+      'latex
+      "#+attr_latex: :rmlines yes
++--------------------------+-----------+
+|   ... better than ...    | ... times |
++--------------+-----------+-----------+
+| PostgreSQL   | MySQL     |     2     |
++--------------+-----------+-----------+
+| PostgreSQL   | MongoDB   |     2     |
++--------------+-----------+-----------+
+| MongoDB      | MySQL     |     2     |
++--------------+-----------+-----------+
+"
+    (goto-char (point-min))
+    (should
+     (search-forward
+      "\\begin{tabular}{|l|l|l|}
+\\multicolumn{2}{|l|}{... better than ...} & ... times \\\\
+\\hline
+PostgreSQL & MySQL & 2 \\\\
+PostgreSQL & MongoDB & 2 \\\\
+MongoDB & MySQL & 2 \\\\
+\\end{tabular}"
+      ))))
+
 (ert-deftest test-ox-latex/inline-image ()
   "Test inline images."
   (org-test-with-exported-text
-- 
2.52.0

Reply via email to