From fd378450755b698cc3ddb1a8d14e8b78d19c57a2 Mon Sep 17 00:00:00 2001 From: Thibaut Cuvelier Date: Thu, 21 Mar 2024 21:32:45 +0100 Subject: [PATCH] Amend 16660d12. The previous commit introduced wrong behaviours for <>. The new code carefully escapes what needs to be escaped from LaTeX, using the now-standard XML tools (XMLStream). --- autotests/export/xhtml/math_output_latex.lyx | 2 +- .../export/xhtml/math_output_latex.xhtml | 2 +- src/insets/InsetLabel.cpp | 2 + src/mathed/InsetMathHull.cpp | 62 ++++++++++++------- src/mathed/InsetMathHull.h | 4 +- 5 files changed, 45 insertions(+), 27 deletions(-) diff --git a/autotests/export/xhtml/math_output_latex.lyx b/autotests/export/xhtml/math_output_latex.lyx index 59d77d2f9b..7c49de59f6 100644 --- a/autotests/export/xhtml/math_output_latex.lyx +++ b/autotests/export/xhtml/math_output_latex.lyx @@ -100,7 +100,7 @@ The problem occurs when adding a label. \begin_layout Standard \begin_inset Formula \begin{equation} -x^{2}\label{eq:1} +x^{2}<\log x\label{eq:1} \end{equation} \end_inset diff --git a/autotests/export/xhtml/math_output_latex.xhtml b/autotests/export/xhtml/math_output_latex.xhtml index cec2d5ba0d..713def3459 100644 --- a/autotests/export/xhtml/math_output_latex.xhtml +++ b/autotests/export/xhtml/math_output_latex.xhtml @@ -22,7 +22,7 @@ div.standard {

Math formula output as raw LaTeX

The problem occurs when adding a label. https://www.lyx.org/trac/ticket/13048
-
x^{2}(1)
+
diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp index 1ca8ea08ae..ab5a5e1716 100644 --- a/src/insets/InsetLabel.cpp +++ b/src/insets/InsetLabel.cpp @@ -380,6 +380,8 @@ void InsetLabel::docbook(XMLStream & xs, OutputParams const & runparams) const docstring InsetLabel::xhtml(XMLStream & xs, OutputParams const &) const { + // Print the label as an HTML anchor, so that an external link can point to this equation. + // (URL: FILE.html#EQ-ID.) // FIXME XHTML // Unfortunately, the name attribute has been deprecated, so we have to use // id here to get the document to validate as XHTML 1.1. This will cause a diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index bb368b3b93..94d293870d 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -2574,35 +2574,56 @@ void InsetMathHull::mathmlize(MathMLStream & ms) const } -void InsetMathHull::mathAsLatex(TeXMathStream & os) const +docstring InsetMathHull::mathAsLatex() const { - MathEnsurer ensurer(os, false); bool const havenumbers = haveNumbers(); bool const havetable = havenumbers || nrows() > 1 || ncols() > 1; if (!havetable) { + odocstringstream ls; + otexrowstream ots(ls); + TeXMathStream os(ots, false, true, TeXMathStream::wsPreview); + ModeSpecifier specifier(os, MATH_MODE); + MathEnsurer ensurer(os, false); + os << cell(index(0, 0)); - return; + return ls.str(); } - os << ""; + odocstringstream ods; + XMLStream xs(ods); + + xs << xml::StartTag("table", "class='mathtable'"); for (row_type row = 0; row < nrows(); ++row) { - os << ""; + xs << xml::StartTag("tr"); for (col_type col = 0; col < ncols(); ++col) { - os << ""; + xs << xml::StartTag("td", "class='math'"); + + odocstringstream ls; + otexrowstream ots(ls); + TeXMathStream os(ots, false, true, TeXMathStream::wsPreview); + ModeSpecifier specifier(os, MATH_MODE); + MathEnsurer ensurer(os, false); + + os << cell(index(0, 0)); + // ls.str() contains a raw LaTeX string, which might require some encoding before being valid XML. + xs << ls.str(); + + xs << xml::EndTag("td"); } if (havenumbers) { - os << ""; + if (!num.empty()) { + xs << '(' << num << ')'; + } + xs << xml::EndTag("td"); } - os << ""; + xs << xml::EndTag("tr"); } - os << "
"; - os << cell(index(row, col)); - os << ""; + xs << xml::StartTag("td"); docstring const & num = numbers_[row]; - if (!num.empty()) - os << '(' << num << ')'; - os << "
"; + xs << xml::EndTag("table"); + + return ods.str(); } @@ -2703,7 +2724,7 @@ docstring InsetMathHull::xhtml(XMLStream & xs, OutputParams const & op) const string const tag = (getType() == hullSimple) ? "span" : "div"; xs << xml::CR() << xml::StartTag(tag, "style = \"text-align: center;\"") - << xml::CompTag("img", "src=\"" + filename + "\" alt=\"Mathematical Equation\"") + << xml::CompTag("img", "src=\"" + filename + R"(" alt="Mathematical Equation")") << xml::EndTag(tag) << xml::CR(); success = true; @@ -2716,12 +2737,8 @@ docstring InsetMathHull::xhtml(XMLStream & xs, OutputParams const & op) const if (!success /* || mathtype != BufferParams::LaTeX */) { // Unfortunately, we cannot use latexString() because we do not want // $...$ or whatever. - odocstringstream ls; - otexrowstream ots(ls); - TeXMathStream wi(ots, false, true, TeXMathStream::wsPreview); - ModeSpecifier specifier(wi, MATH_MODE); - mathAsLatex(wi); - docstring const latex = ls.str(); + // The returned value already has the correct escaping for HTML. + docstring const latex = mathAsLatex(); // class='math' allows for use of jsMath // http://www.math.union.edu/~dpvc/jsMath/ @@ -2729,8 +2746,7 @@ docstring InsetMathHull::xhtml(XMLStream & xs, OutputParams const & op) const // probably should allow for some kind of customization here string const tag = (getType() == hullSimple) ? "span" : "div"; xs << xml::StartTag(tag, "class='math'") - << XMLStream::ESCAPE_AND << latex // Don't escape <> tags: latex might contain them - // (typically, when there is a label). + << XMLStream::ESCAPE_NONE << latex // Don't escape anything: latex might contain XML. << xml::EndTag(tag) << xml::CR(); } diff --git a/src/mathed/InsetMathHull.h b/src/mathed/InsetMathHull.h index cced01c7d0..8e527c4d52 100644 --- a/src/mathed/InsetMathHull.h +++ b/src/mathed/InsetMathHull.h @@ -151,8 +151,8 @@ public: void mathmlize(MathMLStream &) const override; /// void htmlize(HtmlStream &) const override; - /// - void mathAsLatex(TeXMathStream &) const; + /// Returns the hull as a LaTeX string for embedding in HTML or XML. + docstring mathAsLatex() const; /// void toString(odocstream &) const override; ///