Since more about 10 years it has been a dream for me to have a TeX code inset for math.
http://www.lyx.org/trac/ticket/2579

For LyX 2.2 I want implemented such a beast. Its features are simply these:
- it is added to a formula by typing \TeX in math
- its content is not parsed by LyX, so everything is allowed like in the TeX code inset (ERT)
- its content is output to LaTeX as it is
- its content appears in red like normal TeX code

This sound simple but every year when I experiment with this but fail. Attached is a patch idea. Unfortunately I am not able to disable the parsing of the inset content and when re-opening a file with the new inset the color of the inset is lost. Does anybody have an idea how to achieve what I want? I assume that InsetNest is the wrong type but what kind of inset can/must I use? How to I manage that LyX recognize the TeX code inset without writing anything to the LaTeX output that I don't want?

many thanks in advance for any hint and help and regards
Uwe
 src/Makefile.am                 |  2 +
 src/insets/Inset.cpp            |  1 +
 src/insets/InsetCode.h          | 12 +++---
 src/insets/InsetCommand.cpp     |  2 +-
 src/mathed/InsetMathNest.cpp    |  1 +
 src/mathed/InsetMathTeXCode.cpp | 82 +++++++++++++++++++++++++++++++++++++++++
 src/mathed/InsetMathTeXCode.h   | 51 +++++++++++++++++++++++++
 src/mathed/MathFactory.cpp      |  3 ++
 8 files changed, 148 insertions(+), 6 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 9b6b538..492c10b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -431,6 +431,7 @@ SOURCEFILESMATHED = \
        mathed/InsetMathSubstack.cpp \
        mathed/InsetMathSymbol.cpp \
        mathed/InsetMathTabular.cpp \
+       mathed/InsetMathTeXCode.cpp\ 
        mathed/InsetMathUnderset.cpp \
        mathed/InsetMathUnknown.cpp \
        mathed/InsetMathXArrow.cpp \
@@ -500,6 +501,7 @@ HEADERFILESMATHED = \
        mathed/InsetMathSubstack.h \
        mathed/InsetMathSymbol.h \
        mathed/InsetMathTabular.h \
+       mathed/InsetMathTeXCode.h\ 
        mathed/InsetMathUnderset.h \
        mathed/InsetMathUnknown.h \
        mathed/InsetMathXArrow.h \
diff --git a/src/insets/Inset.cpp b/src/insets/Inset.cpp
index cce0f32..c286c20 100644
--- a/src/insets/Inset.cpp
+++ b/src/insets/Inset.cpp
@@ -169,6 +169,7 @@ static void build_translator()
        insetnames[MATH_SUBSTACK_CODE] = InsetName("mathsubstack");
        insetnames[MATH_SYMBOL_CODE] = InsetName("mathsymbol");
        insetnames[MATH_TABULAR_CODE] = InsetName("mathtabular");
+       insetnames[MATH_TEX_CODE] = InsetName("mathtex");
        insetnames[MATH_UNDERSET_CODE] = InsetName("mathunderset");
        insetnames[MATH_UNKNOWN_CODE] = InsetName("mathunknown");
        insetnames[MATH_XARROW_CODE] = InsetName("mathxarrow");
diff --git a/src/insets/InsetCode.h b/src/insets/InsetCode.h
index ea7f981..8747f44 100644
--- a/src/insets/InsetCode.h
+++ b/src/insets/InsetCode.h
@@ -211,15 +211,17 @@ enum InsetCode {
        ///
        MATH_TABULAR_CODE, // 95
        ///
+       MATH_TEX_CODE,
+       ///
        MATH_UNDERSET_CODE,
        ///
        MATH_UNKNOWN_CODE,
        ///
        MATH_XARROW_CODE,
        ///
-       MATH_XYARROW_CODE,
+       MATH_XYARROW_CODE, // 100
        ///
-       MATH_XYMATRIX_CODE, // 100
+       MATH_XYMATRIX_CODE,
        ///
        MATH_MACRO_CODE,
        ///
@@ -227,9 +229,9 @@ enum InsetCode {
        ///
        PREVIEW_CODE,
        ///
-       MATH_DIAGRAM_CODE, 
+       MATH_DIAGRAM_CODE, // 105
        ///
-       SCRIPT_CODE, // 105
+       SCRIPT_CODE,
        ///
        IPA_CODE,
        ///
@@ -237,7 +239,7 @@ enum InsetCode {
        ///
        IPADECO_CODE,
        ///
-       INSET_CODE_SIZE
+       INSET_CODE_SIZE // 110
 };
 
 } // namespace lyx
diff --git a/src/insets/InsetCommand.cpp b/src/insets/InsetCommand.cpp
index 3cc8f70..82fbd90 100644
--- a/src/insets/InsetCommand.cpp
+++ b/src/insets/InsetCommand.cpp
@@ -352,7 +352,7 @@ bool decodeInsetParam(string const & name, string & data,
                InsetSpaceParams p;
                data = InsetSpace::params2string(p);
                break;
-       }
+       }       
        case VSPACE_CODE: {
                VSpace space;
                data = InsetVSpace::params2string(space);
diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp
index dbc0c76..dc9ae74 100644
--- a/src/mathed/InsetMathNest.cpp
+++ b/src/mathed/InsetMathNest.cpp
@@ -2194,6 +2194,7 @@ MathCompletionList::MathCompletionList(Cursor const & cur)
        globals.push_back(from_ascii("\\mathclap"));
        globals.push_back(from_ascii("\\mathllap"));
        globals.push_back(from_ascii("\\mathrlap"));
+       globals.push_back(from_ascii("\\TeX"));
        MathWordList const & words = mathedWordList();
        MathWordList::const_iterator it2;
        //lyxerr << "Globals completion commands: ";
diff --git a/src/mathed/InsetMathTeXCode.cpp b/src/mathed/InsetMathTeXCode.cpp
new file mode 100644
index 0000000..9a801e6
--- /dev/null
+++ b/src/mathed/InsetMathTeXCode.cpp
@@ -0,0 +1,82 @@
+/**
+ * \file InsetMathTeXCode.cpp
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Uwe Stöhr
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include <config.h>
+
+#include "InsetMathTeXCode.h"
+
+#include "LaTeXFeatures.h"
+#include "MathStream.h"
+#include "frontends/Painter.h"
+
+#include <ostream>
+
+namespace lyx {
+
+
+InsetMathTeXCode::InsetMathTeXCode(Buffer * buf)
+       : InsetMathNest(buf, 1)
+{}
+
+
+Inset * InsetMathTeXCode::clone() const
+{
+       return new InsetMathTeXCode(*this);
+}
+
+
+void InsetMathTeXCode::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+       cell(0).metrics(mi, dim);
+       metricsMarkers(dim);
+}
+
+
+void InsetMathTeXCode::draw(PainterInfo & pi, int x, int y) const
+{
+       ColorCode const origcol = pi.base.font.color();
+       //if (visibleContents())
+               pi.base.font.setColor(Color_latex);
+       cell(0).draw(pi, x + 1, y);
+       //if (visibleContents())
+               pi.base.font.setColor(origcol);
+       Dimension const dim = dimension(*pi.base.bv);
+
+       drawMarkers(pi, x, y);
+}
+
+
+void InsetMathTeXCode::write(WriteStream & os) const
+{
+       MathEnsurer ensurer(os);
+       os << '{' << cell(0) << '}';
+}
+
+
+void InsetMathTeXCode::normalize(NormalStream & os) const
+{
+       os << "TeX " << cell(0) << ']';
+}
+
+
+void InsetMathTeXCode::infoize(odocstream & os) const
+{
+               os << "TeX";
+}
+
+
+void InsetMathTeXCode::validate(LaTeXFeatures & features) const
+{
+       InsetMathNest::validate(features);
+       features.require("amsmath");
+}
+
+
+} // namespace lyx
diff --git a/src/mathed/InsetMathTeXCode.h b/src/mathed/InsetMathTeXCode.h
new file mode 100644
index 0000000..484da48
--- /dev/null
+++ b/src/mathed/InsetMathTeXCode.h
@@ -0,0 +1,51 @@
+// -*- C++ -*-
+/**
+ * \file InsetMathTeXCode.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Uwe Stöhr
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef MATH_TEXCODEINSET_H
+#define MATH_TEXCODEINSET_H
+
+#include "InsetMathNest.h"
+
+
+namespace lyx {
+
+class InsetMathTeXCode : public InsetMathNest {
+public:
+       ///
+       explicit InsetMathTeXCode(Buffer * buf);
+       ///
+       void metrics(MetricsInfo & mi, Dimension & dim) const;
+       ///
+       void draw(PainterInfo & pi, int x, int y) const;
+       ///
+       void write(WriteStream & os) const;
+       /// write normalized content
+       void normalize(NormalStream & ns) const;
+       ///
+       void infoize(odocstream & os) const;
+       ///
+       InsetCode lyxCode() const { return MATH_TEX_CODE; }
+       /// Nothing for now
+       void mathmlize(MathStream &) const {}
+       /// Nothing for HTML
+       void htmlize(HtmlStream &) const {}
+       /// request "external features"
+       void validate(LaTeXFeatures & features) const;
+
+private:
+       ///
+       virtual Inset * clone() const;
+};
+
+
+
+} // namespace lyx
+#endif
diff --git a/src/mathed/MathFactory.cpp b/src/mathed/MathFactory.cpp
index 3413127..bbb4d4a 100644
--- a/src/mathed/MathFactory.cpp
+++ b/src/mathed/MathFactory.cpp
@@ -42,6 +42,7 @@
 #include "InsetMathSubstack.h"
 #include "InsetMathSymbol.h"
 #include "InsetMathTabular.h"
+#include "InsetMathTeXCode.h"
 #include "InsetMathUnderset.h"
 #include "InsetMathUnknown.h"
 #include "InsetMathHull.h"
@@ -630,6 +631,8 @@ MathAtom createInsetMath(docstring const & s, Buffer * buf)
                return MathAtom(new InsetMathSpecialChar(s));
        if (s == " ")
                return MathAtom(new InsetMathSpace(" ", ""));
+       if (s == "TeX")
+               return MathAtom(new InsetMathTeXCode(buf));
 
        if (s == "regexp")
                return MathAtom(new InsetMathHull(buf, hullRegexp));

Reply via email to