[LyX/master] Load geometry after graphics

Juergen Spitzmueller spitz at lyx.org
Sat Jul 27 10:48:05 UTC 2024


commit ca4fc01847c721b0ed23ef7a486444d47ff70cf0
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Sat Jul 27 12:46:12 2024 +0200

    Load geometry after graphics
    
    Newer graphics driver overwrite some (output) page settings otherwise
    See https://tex.stackexchange.com/a/384952/19291
    
    Re-fixes #10970
---
 src/BufferParams.cpp  | 36 +++++++++++++++++++++---------------
 src/BufferParams.h    |  2 ++
 src/LaTeXFeatures.cpp |  6 ++++++
 3 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp
index 0a01f9bb4f..a81d9af866 100644
--- a/src/BufferParams.cpp
+++ b/src/BufferParams.cpp
@@ -2078,43 +2078,49 @@ bool BufferParams::writeLaTeX(otexstream & os, LaTeXFeatures & features,
 		case PAPER_DEFAULT:
 			break;
 		}
-		docstring g_options = trim(ods.str(), ",");
-		os << "\\usepackage";
+		string g_options = to_ascii(trim(ods.str(), ","));
+		// geometry must be loaded after graphics nowadays, since
+		// graphic drivers might overwrite some settings
+		// see https://tex.stackexchange.com/a/384952/19291
+		// Hence we store this and output it later
+		ostringstream gs;
+		gs << "\\usepackage";
 		// geometry-light means that the class works with geometry, but overwrites
 		// the package options and paper sizes (memoir does this).
 		// In this case, all options need to go to \geometry
 		// and the standard paper sizes need to go to the class options.
 		if (!g_options.empty() && !features.isProvided("geometry-light")) {
-			os << '[' << g_options << ']';
+			gs << '[' << g_options << ']';
 			g_options.clear();
 		}
-		os << "{geometry}\n";
+		gs << "{geometry}\n";
 		if (use_geometry || features.isProvided("geometry-light")) {
-			os << "\\geometry{verbose";
+			gs << "\\geometry{verbose";
 			if (!g_options.empty())
 				// Output general options here with "geometry light".
-				os << "," << g_options;
+				gs << "," << g_options;
 			// output this only if use_geometry is true
 			if (use_geometry) {
 				if (!topmargin.empty())
-					os << ",tmargin=" << from_ascii(Length(topmargin).asLatexString());
+					gs << ",tmargin=" << Length(topmargin).asLatexString();
 				if (!bottommargin.empty())
-					os << ",bmargin=" << from_ascii(Length(bottommargin).asLatexString());
+					gs << ",bmargin=" << Length(bottommargin).asLatexString();
 				if (!leftmargin.empty())
-					os << ",lmargin=" << from_ascii(Length(leftmargin).asLatexString());
+					gs << ",lmargin=" << Length(leftmargin).asLatexString();
 				if (!rightmargin.empty())
-					os << ",rmargin=" << from_ascii(Length(rightmargin).asLatexString());
+					gs << ",rmargin=" << Length(rightmargin).asLatexString();
 				if (!headheight.empty())
-					os << ",headheight=" << from_ascii(Length(headheight).asLatexString());
+					gs << ",headheight=" << Length(headheight).asLatexString();
 				if (!headsep.empty())
-					os << ",headsep=" << from_ascii(Length(headsep).asLatexString());
+					gs << ",headsep=" << Length(headsep).asLatexString();
 				if (!footskip.empty())
-					os << ",footskip=" << from_ascii(Length(footskip).asLatexString());
+					gs << ",footskip=" << Length(footskip).asLatexString();
 				if (!columnsep.empty())
-					os << ",columnsep=" << from_ascii(Length(columnsep).asLatexString());
+					gs << ",columnsep=" << Length(columnsep).asLatexString();
 			}
-		os << "}\n";
+			gs << "}\n";
 		}
+		set_geometry = gs.str();
 	} else if (orientation == ORIENTATION_LANDSCAPE
 		   || papersize != PAPER_DEFAULT) {
 		features.require("papersize");
diff --git a/src/BufferParams.h b/src/BufferParams.h
index 9bfd16d492..72d2117193 100644
--- a/src/BufferParams.h
+++ b/src/BufferParams.h
@@ -249,6 +249,8 @@ public:
 	/// use custom margins
 	bool use_geometry;
 	///
+	mutable std::string set_geometry;
+	///
 	std::string paperwidth;
 	///
 	std::string paperheight;
diff --git a/src/LaTeXFeatures.cpp b/src/LaTeXFeatures.cpp
index 81022cad01..3736953480 100644
--- a/src/LaTeXFeatures.cpp
+++ b/src/LaTeXFeatures.cpp
@@ -1375,6 +1375,12 @@ string const LaTeXFeatures::getPackages() const
 				 << "]{graphicx}\n";
 	}
 
+	// geometry must be loaded after graphics, since
+	// graphic drivers might overwrite some settings
+	// see https://tex.stackexchange.com/a/384952/19291
+	if (!params_.set_geometry.empty())
+		packages << params_.set_geometry;
+
 	// These must be loaded after graphicx, since they try
 	// to load graphicx without options
 	if (mustProvide("rotating"))


More information about the lyx-cvs mailing list