commit 0f179c8c1b7ecb476c2ff171435edce0c5b37dee
Author: Enrico Forestieri <[email protected]>
Date:   Sat Jul 19 10:28:19 2025 +0200

    Correctly parse labels enclosed in braces in mathed
    
    If a label in math is enclosed in braces, the math parser
    is not able to correctly assign the label to the GUI inset.
    Hence, references to these labels are marked as broken in
    the GUI, even if the document could still compile correctly.
    Howerver, in these cases the label is parsed as a regular math
    construct, so that, for example, "a_b" becomes a script inset
    and then the argument of the label is exported as "a_{b}" and
    references to "a_b" cause compilation errors.
    After this commit the argument of the label is stored verbatim
    and can thus be recovered and correctly shown in the GUI.
    Fixes #13195.
---
 src/mathed/MathParser.cpp | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/mathed/MathParser.cpp b/src/mathed/MathParser.cpp
index a349622736..24ccae6957 100644
--- a/src/mathed/MathParser.cpp
+++ b/src/mathed/MathParser.cpp
@@ -947,11 +947,32 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                else if (t.cat() == catBegin) {
                        MathData md(buf);
                        parse(md, FLAG_BRACE_LAST, mode);
+                       // Correctly set the label if this is a display inset.
+                       // A label inside braces is parsed as a normal macro,
+                       // so recover its argument stored as a verbatim string.
+                       // This issue may occur when importing from latex
+                       // (see bug 13195).
+                       docstring label;
+                       if (md.size() > 1 && grid.asHullInset()) {
+                               for (size_t i = 0; i < md.size() - 1; ++i) {
+                                       InsetMathMacro * mm = 
md[i]->asMacroInset();
+                                       InsetMathBrace * mb = md[i + 
1]->asBraceInset();
+                                       if (mm && mb && mm->name() == "label") {
+                                               MathData & ar = mb->cell(0);
+                                               if (ar.size() == 1 && 
ar[0]->asStringInset()) {
+                                                       label = 
ar[0]->asStringInset()->str();
+                                                       
grid.asHullInset()->label(cellrow, label);
+                                                       md.erase(i, i + 2);
+                                                       break;
+                                               }
+                                       }
+                               }
+                       }
                        // do not create a BraceInset if they were written by 
LyX
                        // this helps to keep the annoyance of  "a choose b"  
to a minimum
                        if (md.size() == 1 && md[0]->extraBraces())
                                cell->append(md);
-                       else
+                       else if (md.size() > 0 || label.empty())
                                cell->emplace_back(new InsetMathBrace(buf, md));
                }
 
@@ -1794,7 +1815,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                        // FIXME: This is swallowed in inline formulas
                        docstring label = parse_verbatim_item();
                        MathData md(buf);
-                       asMathData(label, md);
+                       md.emplace_back(new InsetMathString(buf, label));
                        if (grid.asHullInset()) {
                                grid.asHullInset()->label(cellrow, label);
                        } else {
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to