[LyX/2.5.x] Error if tex file has a preamble but no (accessible) \begin{document} (#13297)

Juergen Spitzmueller <[email protected]> Sat, 25 Apr 2026 06:29:09 +0000
Newsgroups gmane.editors.lyx.cvs
Message-ID <[email protected]>
commit cc01a5de32ff9443e1d0ba224cbc288dcb572a3f
Author: Juergen Spitzmueller <[email protected]>
Date:   Wed Apr 8 08:52:45 2026 +0200

    Error if tex file has a preamble but no (accessible) \begin{document} (#13297)
    
    (cherry picked from commit b1a75e7ffa969cfacc6c3fa7088476999d5b0ae0)
---
 src/tex2lyx/Preamble.cpp | 13 ++++++++++++-
 status.25x               |  7 +++++--
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/tex2lyx/Preamble.cpp b/src/tex2lyx/Preamble.cpp
index 205f04870a..1f95ec55d8 100644
--- a/src/tex2lyx/Preamble.cpp
+++ b/src/tex2lyx/Preamble.cpp
@@ -2762,6 +2762,10 @@ void Preamble::parse(Parser & p, string const & forceclass,
 		class_set = true;
 	}
 
+	// if we have a full document, check that we have
+	// a body (see #13297)
+	bool complete_preamble = false;
+
 	while (is_full_document && p.good()) {
 		if (detectEncoding && h_inputencoding != "auto-legacy" &&
 		    h_inputencoding != "auto-legacy-plain")
@@ -3546,8 +3550,10 @@ void Preamble::parse(Parser & p, string const & forceclass,
 
 		if (t.cs() == "begin") {
 			string const name = p.getArg('{', '}');
-			if (name == "document")
+			if (name == "document") {
+				complete_preamble = true;
 				break;
+			}
 			h_preamble << "\\begin{" << name << "}";
 			continue;
 		}
@@ -3765,6 +3771,11 @@ void Preamble::parse(Parser & p, string const & forceclass,
 		}
 	}
 
+	if (is_full_document && !complete_preamble) {
+		error_message("Found preamble but not \\begin{document}. Aborting!");
+		exit(EXIT_FAILURE);
+	}
+
 	// set textclass if not yet done (snippets without \documentclass and forced class)
 	if (!class_set) 
 		setTextClass(h_textclass, tc);
diff --git a/status.25x b/status.25x
index 716f1d9a30..9c9d3630a0 100644
--- a/status.25x
+++ b/status.25x
@@ -79,9 +79,12 @@ What's new
 
 * TEX2LYX
 
-- Fix import of table with linebreak in column specification
+- Fix import of table with linebreak in column specification.
 
-- Fix parsing of more complex column specification
+- Fix parsing of more complex column specification.
+
+- Error if TeX file has a preamble but no (accessible) \begin{document}
+  (bug 13297).
 
 
 * LYXHTML
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs