commit 679385875f9dc74ca4b691af77e86762af4f51b0
Author: Enrico Forestieri <for...@lyx.org>
Date:   Sun Aug 3 01:03:46 2025 +0200

    Account for unknown required packages
    
    We really don't use different LaTeXFeatures objects, but the check
    for already accounted for packages has to be done at the end of the
    preamble, not in the middle...
---
 src/BufferParams.cpp  |  4 ++++
 src/LaTeXFeatures.cpp | 23 ++++++++++++++++-------
 src/LaTeXFeatures.h   |  6 ++++++
 3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp
index 74e41f60f1..f26bb67bdb 100644
--- a/src/BufferParams.cpp
+++ b/src/BufferParams.cpp
@@ -3066,6 +3066,10 @@ bool BufferParams::writeLaTeX(otexstream & os, 
LaTeXFeatures & features,
        // package-specific theorem definitions
        os << features.getThmExtraDefinitions();
 
+       // load last the unknown packages mentioned in the Require tag
+       // of layouts/custom insets
+       os << features.getUnknownPackages();
+
        // Since menukeys uses catoptions, which does some heavy changes on 
key-value options,
        // it is recommended to load menukeys as the last package (even after 
hyperref)
        if (features.isRequired("menukeys"))
diff --git a/src/LaTeXFeatures.cpp b/src/LaTeXFeatures.cpp
index 3670dc26d7..59c73a8dd7 100644
--- a/src/LaTeXFeatures.cpp
+++ b/src/LaTeXFeatures.cpp
@@ -775,7 +775,11 @@ bool LaTeXFeatures::isProvided(string const & name) const
 
 bool LaTeXFeatures::mustProvide(string const & name) const
 {
-       return isRequired(name) && !isProvided(name);
+       if (isRequired(name) && !isProvided(name)) {
+               features_loaded_.insert(name);
+               return true;
+       }
+       return false;
 }
 
 
@@ -1060,6 +1064,17 @@ bool LaTeXFeatures::hasRTLLanguage() const
 }
 
 
+string LaTeXFeatures::getUnknownPackages() const
+{
+       ostringstream packages;
+       for (string const & name : features_) {
+               if (features_loaded_.find(name) == features_loaded_.end() && 
!isProvided(name))
+                       packages << "\\usepackage{" << name << "}\n";
+       }
+       return packages.str();
+}
+
+
 namespace {
 
 char const * simplefeatures[] = {
@@ -1305,12 +1320,6 @@ string const LaTeXFeatures::getPackages() const
 {
        ostringstream packages;
 
-       // FIXME: currently, we can only load packages and macros known
-       // to LyX.
-       // However, with the Require tag of layouts/custom insets,
-       // also unknown packages can be requested. They are silently
-       // swallowed now. We should change this eventually.
-
        // Simple hooks to add things before or after a given "simple"
        // feature. Useful if loading order matters.
        map<string, string> before_simplefeature_;
diff --git a/src/LaTeXFeatures.h b/src/LaTeXFeatures.h
index 2d6fce25ca..ba6eb0062c 100644
--- a/src/LaTeXFeatures.h
+++ b/src/LaTeXFeatures.h
@@ -208,6 +208,10 @@ public:
        bool hasRTLLanguage() const;
        /// This is used to avoid recursion in InsetMathMacro::validate
        std::set<docstring> & activeMacros() { return active_macros_; }
+       /// Return a series of \usepackage{} commands for unknown packages
+       /// mentioned in the Require tag of layouts/custom insets that
+       /// that have not already been accounted for
+       std::string getUnknownPackages() const;
 
 private:
        ///
@@ -224,6 +228,8 @@ private:
        Features features_;
        /// Features that are provided
        Features provides_;
+       /// Already accounted for features
+       mutable Features features_loaded_;
        /// Static preamble bits, from external templates, or anywhere else
        typedef std::list<TexString> SnippetList;
        ///
-- 
lyx-cvs mailing list
lyx-cvs@lists.lyx.org
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to