]> git.lyx.org Git - features.git/commitdiff
Produce a cleaner latex output by avoiding \lyxmathsym when in text mode
authorEnrico Forestieri <forenr@lyx.org>
Wed, 4 Jun 2008 00:36:04 +0000 (00:36 +0000)
committerEnrico Forestieri <forenr@lyx.org>
Wed, 4 Jun 2008 00:36:04 +0000 (00:36 +0000)
inside math mode, and also ensure the proper mode inside math when using
the symbols from the unicodesymbols file. This hopefully fixes bug 3938.
Note that this approach could also be used for fixing bug 1527.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25109 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/InsetMathBox.cpp
src/mathed/InsetMathNest.cpp
src/mathed/InsetMathString.cpp
src/mathed/MathStream.cpp
src/mathed/MathStream.h

index 91c1a0ab2d8ea9748275280d1e27644cbcfd87a0..bb4ffc6944a4cad6b050dbb2362c6f5178eb0bca 100644 (file)
@@ -39,7 +39,10 @@ InsetMathBox::InsetMathBox(docstring const & name)
 
 void InsetMathBox::write(WriteStream & os) const
 {
+       bool oldmode = os.textMode();
+       os.textMode(true);
        os << '\\' << name_ << '{' << cell(0) << '}';
+       os.textMode(oldmode);
 }
 
 
@@ -115,7 +118,10 @@ void InsetMathFBox::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathFBox::write(WriteStream & os) const
 {
+       bool oldmode = os.textMode();
+       os.textMode(true);
        os << "\\fbox{" << cell(0) << '}';
+       os.textMode(oldmode);
 }
 
 
@@ -211,6 +217,8 @@ void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathMakebox::write(WriteStream & os) const
 {
+       bool oldmode = os.textMode();
+       os.textMode(true);
        os << (framebox_ ? "\\framebox" : "\\makebox");
        if (cell(0).size() || !os.latex()) {
                os << '[' << cell(0) << ']';
@@ -218,6 +226,7 @@ void InsetMathMakebox::write(WriteStream & os) const
                        os << '[' << cell(1) << ']';
        }
        os << '{' << cell(2) << '}';
+       os.textMode(oldmode);
 }
 
 
@@ -266,7 +275,10 @@ void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathBoxed::write(WriteStream & os) const
 {
+       bool oldmode = os.textMode();
+       os.textMode(true);
        os << "\\boxed{" << cell(0) << '}';
+       os.textMode(oldmode);
 }
 
 
index 88e163deb4edfd9da4997b966a056d541b5f5e36..4dbdd2a03015d693c7e767e0324d47466ab59103 100644 (file)
@@ -73,6 +73,11 @@ using cap::cutSelection;
 using cap::replaceSelection;
 using cap::selClearOrDel;
 
+char const * text_commands[] =
+{ "text", "textrm", "textsf", "texttt", "textmd", "textbf", "textup", "textit",
+  "textsl", "textsc" };
+int const num_text_commands = sizeof(text_commands) / sizeof(*text_commands);
+
 
 InsetMathNest::InsetMathNest(idx_type nargs)
        : cells_(nargs), lock_(false), mouse_hover_(false)
@@ -336,7 +341,15 @@ MathData InsetMathNest::glue() const
 
 void InsetMathNest::write(WriteStream & os) const
 {
-       os << '\\' << name().c_str();
+       bool oldmode = os.textMode();
+       docstring const latex_name = name().c_str();
+       for (int i = 0; i < num_text_commands; ++i) {
+               if (latex_name == from_ascii(text_commands[i])) {
+                       os.textMode(true);
+                       break;
+               }
+       }
+       os << '\\' << latex_name;
        for (size_t i = 0; i < nargs(); ++i)
                os << '{' << cell(i) << '}';
        if (nargs() == 0)
@@ -345,6 +358,7 @@ void InsetMathNest::write(WriteStream & os) const
                os << "\\lyxlock";
                os.pendingSpace(true);
        }
+       os.textMode(oldmode);
 }
 
 
index a8eb1067c3d509a5c7ce9a7cab22859710b0f314..bfa17019b0a88f3d3904cdb2e621904e33d37137 100644 (file)
@@ -111,24 +111,37 @@ void InsetMathString::write(WriteStream & os) const
        docstring::const_iterator cit = str_.begin();
        docstring::const_iterator end = str_.end();
 
-       bool in_lyxmathsym = false;
+       bool in_forced_mode = false;
        while (cit != end) {
                char_type const c = *cit;
                try {
                        docstring command(1, c);
                        if (c < 0x80 || Encodings::latexMathChar(c, command)) {
-                               if (in_lyxmathsym) {
+                               if (os.textMode()) {
+                                       if (c < 0x80 && in_forced_mode) {
+                                               os << '}';
+                                               in_forced_mode = false;
+                                       }
+                                       if (c >= 0x80 && !in_forced_mode) {
+                                               os << "\\ensuremath{";
+                                               in_forced_mode = true;
+                                       }
+                               } else if (in_forced_mode) {
                                        os << '}';
-                                       in_lyxmathsym = false;
+                                       in_forced_mode = false;
                                }
-                               os << command;
                        } else {
-                               if (!in_lyxmathsym) {
+                               if (os.textMode()) {
+                                       if (in_forced_mode) {
+                                               os << '}';
+                                               in_forced_mode = false;
+                                       }
+                               } else if (!in_forced_mode) {
                                        os << "\\lyxmathsym{";
-                                       in_lyxmathsym = true;
+                                       in_forced_mode = true;
                                }
-                               os << command;
                        }
+                       os << command;
                        // We may need a space if the command contains a macro
                        // and the last char is ASCII.
                        if (lyx::support::contains(command, '\\')
@@ -149,7 +162,7 @@ void InsetMathString::write(WriteStream & os) const
                }
                ++cit;
        }
-       if (in_lyxmathsym)
+       if (in_forced_mode)
                os << '}';
 }
 
index 6ad980597d54a92495b819911e196ee8ca755432..8677358b55f9c653931781cd9dbb0abd87679c33 100644 (file)
@@ -105,13 +105,13 @@ WriteStream & operator<<(WriteStream & ws, docstring const & s)
 
 WriteStream::WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun)
        : os_(os), fragile_(fragile), firstitem_(false), latex_(latex),
-         dryrun_(dryrun), pendingspace_(false), line_(0)
+         dryrun_(dryrun), pendingspace_(false), textmode_(false), line_(0)
 {}
 
 
 WriteStream::WriteStream(odocstream & os)
        : os_(os), fragile_(false), firstitem_(false), latex_(false),
-         dryrun_(false), pendingspace_(false), line_(0)
+         dryrun_(false), pendingspace_(false), textmode_(false), line_(0)
 {}
 
 
@@ -134,6 +134,12 @@ void WriteStream::pendingSpace(bool how)
 }
 
 
+void WriteStream::textMode(bool textmode)
+{
+       textmode_ = textmode;
+}
+
+
 WriteStream & operator<<(WriteStream & ws, MathAtom const & at)
 {
        at->write(ws);
index 87868890859dd10c06af32203c9ad97bd4a18a71..92b63c2a6b6723e0ec3e814e7114b8dba6a7d060 100644 (file)
@@ -54,6 +54,10 @@ public:
        void pendingSpace(bool how);
        /// writes space if next thing is isalpha()
        bool pendingSpace() const { return pendingspace_; }
+       /// tell whether we are in text mode or not when producing latex code
+       void textMode(bool textmode);
+       /// tell whether we are in text mode or not when producing latex code
+       bool textMode() const { return textmode_; }
 private:
        ///
        odocstream & os_;
@@ -67,6 +71,8 @@ private:
        bool dryrun_;
        /// do we have a space pending?
        bool pendingspace_;
+       /// are we in text mode when producing latex code?
+       bool textmode_;
        ///
        int line_;
 };