commit 27b34fe654035620f627eff5e913ec8338241085
Author: Juergen Spitzmueller <[email protected]>
Date:   Sun Nov 16 18:21:14 2025 +0100

    Escape math delimiter chars if needed (#13250)
    
    This concerns at least "|" in index and nomencl.
    
    Cherry pick of 5f3ca2c4f7cdee and 9cc05eac19c634
---
 src/mathed/InsetMathDelim.cpp | 30 +++++++++++++++++++++++-------
 src/mathed/InsetMathNest.cpp  |  2 +-
 src/mathed/MathStream.cpp     |  5 +++--
 src/mathed/MathStream.h       |  9 +++++++--
 4 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/src/mathed/InsetMathDelim.cpp b/src/mathed/InsetMathDelim.cpp
index 517bc7590f..bd61a922ae 100644
--- a/src/mathed/InsetMathDelim.cpp
+++ b/src/mathed/InsetMathDelim.cpp
@@ -22,6 +22,7 @@
 #include "LaTeXFeatures.h"
 
 #include "support/docstring.h"
+#include "support/lstrings.h"
 
 #include "frontends/FontMetrics.h"
 
@@ -31,15 +32,30 @@ using namespace std;
 
 namespace lyx {
 
-static docstring convertDelimToLatexName(docstring const & name)
+static docstring convertDelimToLatexName(docstring const & name, docstring 
const & escape_chars)
 {
+       docstring result;
        if (name.size() == 1) {
                char_type const c = name[0];
                if (c == '<' || c == '(' || c == '[' || c == '.'
                    || c == '>' || c == ')' || c == ']' || c == '/' || c == '|')
-                       return name;
+                       result = name;
+       } else
+               result = '\\' + name + ' ';
+
+       // since some chars used for delims ('|' at least)
+       // must be escaped in Index and Nomencl, we have
+       // to handle this (#13250)
+       if (!escape_chars.empty()) {
+               odocstringstream ods;
+               for (char_type const c : result) {
+                       if (support::contains(escape_chars.substr(1), c))
+                               ods << escape_chars.substr(0,1);
+                       ods.put(c);
+               }
+               result = ods.str();
        }
-       return '\\' + name + ' ';
+       return result;
 }
 
 
@@ -85,15 +101,15 @@ void InsetMathDelim::validate(LaTeXFeatures & features) 
const
 void InsetMathDelim::writeMath(TeXMathStream & os) const
 {
        MathEnsurer ensurer(os);
-       os << "\\left" << convertDelimToLatexName(left_) << cell(0)
-          << "\\right" << convertDelimToLatexName(right_);
+       os << "\\left" << convertDelimToLatexName(left_, os.escapeChars()) << 
cell(0)
+          << "\\right" << convertDelimToLatexName(right_, os.escapeChars());
 }
 
 
 void InsetMathDelim::normalize(NormalStream & os) const
 {
-       os << "[delim " << convertDelimToLatexName(left_) << ' '
-          << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
+       os << "[delim " << convertDelimToLatexName(left_, docstring()) << ' '
+          << convertDelimToLatexName(right_, docstring()) << ' ' << cell(0) << 
']';
 }
 
 
diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp
index fd5abbc39f..cb025a0ce5 100644
--- a/src/mathed/InsetMathNest.cpp
+++ b/src/mathed/InsetMathNest.cpp
@@ -367,7 +367,7 @@ void InsetMathNest::latex(otexstream & os, OutputParams 
const & runparams) const
        else
                ot = TeXMathStream::wsDefault;
        TeXMathStream wi(os, runparams.moving_arg, true, ot,
-                        runparams.encoding);
+                        runparams.encoding, runparams.escape_chars);
        wi.strikeoutMath(runparams.inDeletedInset);
        if (runparams.inulemcmd) {
                wi.ulemCmd(TeXMathStream::UNDERLINE);
diff --git a/src/mathed/MathStream.cpp b/src/mathed/MathStream.cpp
index c95aa26da6..6711e821d4 100644
--- a/src/mathed/MathStream.cpp
+++ b/src/mathed/MathStream.cpp
@@ -439,9 +439,10 @@ TeXMathStream & operator<<(TeXMathStream & ws, docstring 
const & s)
 
 
 TeXMathStream::TeXMathStream(otexrowstream & os, bool fragile, bool latex,
-                             OutputType output, Encoding const * encoding)
+                            OutputType output, Encoding const * encoding,
+                            docstring escapechars)
        : os_(os), fragile_(fragile), latex_(latex),
-         output_(output), encoding_(encoding)
+         output_(output), encoding_(encoding), escape_chars_(escapechars)
 {}
 
 
diff --git a/src/mathed/MathStream.h b/src/mathed/MathStream.h
index 39fe949fb8..73dd05ea50 100644
--- a/src/mathed/MathStream.h
+++ b/src/mathed/MathStream.h
@@ -145,8 +145,9 @@ public:
        };
        ///
        explicit TeXMathStream(otexrowstream & os, bool fragile = false,
-                              bool latex = false, OutputType output = 
wsDefault,
-                              Encoding const * encoding = nullptr);
+                              bool latex = false, OutputType output = 
wsDefault,
+                              Encoding const * encoding = nullptr,
+                              docstring escapechar = docstring());
        ///
        ~TeXMathStream();
        ///
@@ -209,6 +210,8 @@ public:
        bool inMathClass() const { return mathclass_; }
        /// LaTeX encoding
        Encoding const * encoding() const { return encoding_; }
+       ///
+       docstring escapeChars() const { return escape_chars_; }
 
        /// Temporarily change the TexRow information about the outer row entry.
        Changer changeRowEntry(TexRow::RowEntry const & entry);
@@ -253,6 +256,8 @@ private:
        TexRow::RowEntry row_entry_ = TexRow::row_none;
        /// whether we are in a MathClass inset
        bool mathclass_ = false;
+       ///
+       docstring escape_chars_;
 };
 
 ///
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to