diff --git a/src/mathed/InsetMathBox.cpp b/src/mathed/InsetMathBox.cpp index 91c1a0ab2d..bb4ffc6944 100644 --- a/src/mathed/InsetMathBox.cpp +++ b/src/mathed/InsetMathBox.cpp @@ -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); } diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp index 88e163deb4..4dbdd2a030 100644 --- a/src/mathed/InsetMathNest.cpp +++ b/src/mathed/InsetMathNest.cpp @@ -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); } diff --git a/src/mathed/InsetMathString.cpp b/src/mathed/InsetMathString.cpp index a8eb1067c3..bfa17019b0 100644 --- a/src/mathed/InsetMathString.cpp +++ b/src/mathed/InsetMathString.cpp @@ -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 << '}'; } diff --git a/src/mathed/MathStream.cpp b/src/mathed/MathStream.cpp index 6ad980597d..8677358b55 100644 --- a/src/mathed/MathStream.cpp +++ b/src/mathed/MathStream.cpp @@ -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); diff --git a/src/mathed/MathStream.h b/src/mathed/MathStream.h index 8786889085..92b63c2a6b 100644 --- a/src/mathed/MathStream.h +++ b/src/mathed/MathStream.h @@ -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_; };