commit fab718dee5e8c4ecd7f99c3d4e645c66ca2f9157
Author: Enrico Forestieri <[email protected]>
Date:   Mon Jul 21 20:30:11 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 382b14b71e..b5e01dae08 100644
--- a/src/mathed/MathParser.cpp
+++ b/src/mathed/MathParser.cpp
@@ -945,11 +945,32 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                else if (t.cat() == catBegin) {
                        MathData ar(buf);
                        parse(ar, 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 (ar.size() > 1 && grid.asHullInset()) {
+                               for (size_t i = 0; i < ar.size() - 1; ++i) {
+                                       InsetMathMacro * mm = ar[i]->asMacro();
+                                       InsetMathBrace * mb = ar[i + 
1]->asBraceInset();
+                                       if (mm && mb && mm->name() == "label") {
+                                               MathData & md = mb->cell(0);
+                                               if (md.size() == 1 && 
md[0]->asStringInset()) {
+                                                       label = 
md[0]->asStringInset()->str();
+                                                       
grid.asHullInset()->label(cellrow, label);
+                                                       ar.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 (ar.size() == 1 && ar[0]->extraBraces())
                                cell->append(ar);
-                       else
+                       else if (ar.size() > 0 || label.empty())
                                cell->push_back(MathAtom(new 
InsetMathBrace(buf, ar)));
                }
 
@@ -1793,7 +1814,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                        // FIXME: This is swallowed in inline formulas
                        docstring label = parse_verbatim_item();
                        MathData ar(buf);
-                       asArray(label, ar);
+                       ar.push_back(MathAtom(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