In mathed, sometimes one needs to typeset something in math-in-text mode.
For example, this may be the case with \tag or other text commands.
TeX allows nesting math inside a text mode command, for example
\[y=f(x) \tag{$1\alpha$}\] is a valid construct. However, this is
cumbersome to achieve in LyX, as you cannot enter it directly in mathed
but have to write it in normal text, select it, and then hit Ctrl-M.

Note that, when exporting to latex, LyX 1.6 automatically encloses in
\ensuremath a symbol that is entered in text mode but needs math mode.
However, this is done for individual symbols, such that $\text{\phi=\pi}$
is exported as $\text{\ensuremath{\phi}=\ensuremath{\pi}}$, so losing
correct spaces around = and so on.

The attached patch introduces the EnsureMath inset, obtained by typing
\ensuremath in mathed, alleviating the previous shortcomings.

José, can this go in?

-- 
Enrico
Index: src/mathed/MathFactory.cpp
===================================================================
--- src/mathed/MathFactory.cpp  (revision 26295)
+++ src/mathed/MathFactory.cpp  (working copy)
@@ -20,6 +20,7 @@
 #include "InsetMathColor.h"
 #include "InsetMathDecoration.h"
 #include "InsetMathDots.h"
+#include "InsetMathEnsureMath.h"
 #include "InsetMathFont.h"
 #include "InsetMathFontOld.h"
 #include "InsetMathFrac.h"
@@ -443,6 +444,8 @@ MathAtom createInsetMath(docstring const
                return MathAtom(new 
InsetMathPhantom(InsetMathPhantom::phantom));
        if (s == "vphantom")
                return MathAtom(new 
InsetMathPhantom(InsetMathPhantom::vphantom));
+       if (s == "ensuremath")
+               return MathAtom(new InsetMathEnsureMath);
 
        return MathAtom(new MathMacro(s));
 }
Index: src/mathed/InsetMathEnsureMath.cpp
===================================================================
--- src/mathed/InsetMathEnsureMath.cpp  (revision 0)
+++ src/mathed/InsetMathEnsureMath.cpp  (revision 0)
@@ -0,0 +1,76 @@
+/**
+ * \file InsetMathEnsureMath.cpp
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include <config.h>
+
+#include "InsetMathEnsureMath.h"
+
+#include "MathStream.h"
+#include "MathData.h"
+#include "LaTeXFeatures.h"
+
+#include <ostream>
+
+
+namespace lyx {
+
+InsetMathEnsureMath::InsetMathEnsureMath()
+       : InsetMathNest(1)
+{}
+
+
+Inset * InsetMathEnsureMath::clone() const
+{
+       return new InsetMathEnsureMath(*this);
+}
+
+
+void InsetMathEnsureMath::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+       FontSetChanger dummy(mi.base, "mathnormal");
+       cell(0).metrics(mi, dim);
+       metricsMarkers(dim);
+}
+
+
+void InsetMathEnsureMath::draw(PainterInfo & pi, int x, int y) const
+{
+       FontSetChanger dummy(pi.base, "mathnormal");
+       cell(0).draw(pi, x, y);
+       drawMarkers(pi, x, y);
+}
+
+
+void InsetMathEnsureMath::metricsT(TextMetricsInfo const & mi, Dimension & 
dim) const
+{
+       cell(0).metricsT(mi, dim);
+}
+
+
+void InsetMathEnsureMath::drawT(TextPainter & pain, int x, int y) const
+{
+       cell(0).drawT(pain, x, y);
+}
+
+
+void InsetMathEnsureMath::write(WriteStream & os) const
+{
+       ModeSpecifier specifier(os, MATH_MODE);
+       os << "\\ensuremath{" << cell(0) << "}";
+}
+
+
+void InsetMathEnsureMath::infoize(odocstream & os) const
+{
+       os << "EnsureMath";
+}
+
+
+} // namespace lyx
Index: src/mathed/MathParser.cpp
===================================================================
--- src/mathed/MathParser.cpp   (revision 26295)
+++ src/mathed/MathParser.cpp   (working copy)
@@ -1526,7 +1526,7 @@ void Parser::parse1(InsetMathGrid & grid
                }
 #endif
 
-               else if (t.cs() == "lyxmathsym" || t.cs() == "ensuremath") {
+               else if (t.cs() == "lyxmathsym") {
                        skipSpaces();
                        if (getToken().cat() != catBegin) {
                                error("'{' expected in \\" + t.cs());
@@ -1547,22 +1547,16 @@ void Parser::parse1(InsetMathGrid & grid
                                error("'}' expected in \\" + t.cs());
                                return;
                        }
-                       if (t.cs() == "ensuremath") {
+                       docstring rem;
+                       cmd = Encodings::fromLaTeXCommand(cmd, rem);
+                       for (size_t i = 0; i < cmd.size(); ++i)
+                               cell->push_back(MathAtom(new 
InsetMathChar(cmd[i])));
+                       if (rem.size()) {
+                               MathAtom at = createInsetMath(t.cs());
+                               cell->push_back(at);
                                MathData ar;
-                               mathed_parse_cell(ar, cmd);
+                               mathed_parse_cell(ar, '{' + rem + '}');
                                cell->append(ar);
-                       } else {
-                               docstring rem;
-                               cmd = Encodings::fromLaTeXCommand(cmd, rem);
-                               for (size_t i = 0; i < cmd.size(); ++i)
-                                       cell->push_back(MathAtom(new 
InsetMathChar(cmd[i])));
-                               if (rem.size()) {
-                                       MathAtom at = createInsetMath(t.cs());
-                                       cell->push_back(at);
-                                       MathData ar;
-                                       mathed_parse_cell(ar, '{' + rem + '}');
-                                       cell->append(ar);
-                               }
                        }
                }
 
Index: src/mathed/InsetMathEnsureMath.h
===================================================================
--- src/mathed/InsetMathEnsureMath.h    (revision 0)
+++ src/mathed/InsetMathEnsureMath.h    (revision 0)
@@ -0,0 +1,46 @@
+// -*- C++ -*-
+/**
+ * \file InsetMathEnsureMath.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef MATH_ENSUREMATHINSET_H
+#define MATH_ENSUREMATHINSET_H
+
+#include "InsetMathNest.h"
+
+
+namespace lyx {
+
+
+/// Inset for ensuring math mode
+class InsetMathEnsureMath : public InsetMathNest {
+public:
+       InsetMathEnsureMath();
+       ///
+       mode_type currentMode() const { return MATH_MODE; }
+       ///
+       void metrics(MetricsInfo & mi, Dimension & dim) const;
+       ///
+       void draw(PainterInfo & pi, int x, int y) const;
+       ///
+       void metricsT(TextMetricsInfo const & mi, Dimension & dim) const;
+       ///
+       void drawT(TextPainter & pi, int x, int y) const;
+       ///
+       void write(WriteStream & os) const;
+       ///
+       void infoize(odocstream & os) const;
+private:
+       virtual Inset * clone() const;
+};
+
+
+} // namespace lyx
+
+#endif
Index: src/Makefile.am
===================================================================
--- src/Makefile.am     (revision 26295)
+++ src/Makefile.am     (working copy)
@@ -338,6 +338,7 @@ SOURCEFILESMATHED = \
        mathed/InsetMathDelim.cpp \
        mathed/InsetMathDiff.cpp \
        mathed/InsetMathDots.cpp \
+       mathed/InsetMathEnsureMath.cpp \
        mathed/InsetMathEnv.cpp \
        mathed/InsetMathExFunc.cpp \
        mathed/InsetMathExInt.cpp \
@@ -400,6 +401,7 @@ HEADERFILESMATHED = \
        mathed/InsetMathDelim.h \
        mathed/InsetMathDiff.h \
        mathed/InsetMathDots.h \
+       mathed/InsetMathEnsureMath.h \
        mathed/InsetMathEnv.h \
        mathed/InsetMathExFunc.h \
        mathed/InsetMathExInt.h \

Reply via email to