Produce a cleaner latex output by avoiding \lyxmathsym when in text mode

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
This commit is contained in:
Enrico Forestieri 2008-06-04 00:36:04 +00:00
parent 0d4ab86261
commit 4ad2d0e15c
5 changed files with 62 additions and 11 deletions

View File

@ -39,7 +39,10 @@ InsetMathBox::InsetMathBox(docstring const & name)
void InsetMathBox::write(WriteStream & os) const void InsetMathBox::write(WriteStream & os) const
{ {
bool oldmode = os.textMode();
os.textMode(true);
os << '\\' << name_ << '{' << cell(0) << '}'; 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 void InsetMathFBox::write(WriteStream & os) const
{ {
bool oldmode = os.textMode();
os.textMode(true);
os << "\\fbox{" << cell(0) << '}'; 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 void InsetMathMakebox::write(WriteStream & os) const
{ {
bool oldmode = os.textMode();
os.textMode(true);
os << (framebox_ ? "\\framebox" : "\\makebox"); os << (framebox_ ? "\\framebox" : "\\makebox");
if (cell(0).size() || !os.latex()) { if (cell(0).size() || !os.latex()) {
os << '[' << cell(0) << ']'; os << '[' << cell(0) << ']';
@ -218,6 +226,7 @@ void InsetMathMakebox::write(WriteStream & os) const
os << '[' << cell(1) << ']'; os << '[' << cell(1) << ']';
} }
os << '{' << cell(2) << '}'; 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 void InsetMathBoxed::write(WriteStream & os) const
{ {
bool oldmode = os.textMode();
os.textMode(true);
os << "\\boxed{" << cell(0) << '}'; os << "\\boxed{" << cell(0) << '}';
os.textMode(oldmode);
} }

View File

@ -73,6 +73,11 @@ using cap::cutSelection;
using cap::replaceSelection; using cap::replaceSelection;
using cap::selClearOrDel; 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) InsetMathNest::InsetMathNest(idx_type nargs)
: cells_(nargs), lock_(false), mouse_hover_(false) : cells_(nargs), lock_(false), mouse_hover_(false)
@ -336,7 +341,15 @@ MathData InsetMathNest::glue() const
void InsetMathNest::write(WriteStream & os) 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) for (size_t i = 0; i < nargs(); ++i)
os << '{' << cell(i) << '}'; os << '{' << cell(i) << '}';
if (nargs() == 0) if (nargs() == 0)
@ -345,6 +358,7 @@ void InsetMathNest::write(WriteStream & os) const
os << "\\lyxlock"; os << "\\lyxlock";
os.pendingSpace(true); os.pendingSpace(true);
} }
os.textMode(oldmode);
} }

View File

@ -111,24 +111,37 @@ void InsetMathString::write(WriteStream & os) const
docstring::const_iterator cit = str_.begin(); docstring::const_iterator cit = str_.begin();
docstring::const_iterator end = str_.end(); docstring::const_iterator end = str_.end();
bool in_lyxmathsym = false; bool in_forced_mode = false;
while (cit != end) { while (cit != end) {
char_type const c = *cit; char_type const c = *cit;
try { try {
docstring command(1, c); docstring command(1, c);
if (c < 0x80 || Encodings::latexMathChar(c, command)) { 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 << '}'; os << '}';
in_lyxmathsym = false; in_forced_mode = false;
} }
os << command;
} else { } else {
if (!in_lyxmathsym) { if (os.textMode()) {
if (in_forced_mode) {
os << '}';
in_forced_mode = false;
}
} else if (!in_forced_mode) {
os << "\\lyxmathsym{"; os << "\\lyxmathsym{";
in_lyxmathsym = true; in_forced_mode = true;
} }
os << command;
} }
os << command;
// We may need a space if the command contains a macro // We may need a space if the command contains a macro
// and the last char is ASCII. // and the last char is ASCII.
if (lyx::support::contains(command, '\\') if (lyx::support::contains(command, '\\')
@ -149,7 +162,7 @@ void InsetMathString::write(WriteStream & os) const
} }
++cit; ++cit;
} }
if (in_lyxmathsym) if (in_forced_mode)
os << '}'; os << '}';
} }

View File

@ -105,13 +105,13 @@ WriteStream & operator<<(WriteStream & ws, docstring const & s)
WriteStream::WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun) WriteStream::WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun)
: os_(os), fragile_(fragile), firstitem_(false), latex_(latex), : 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) WriteStream::WriteStream(odocstream & os)
: os_(os), fragile_(false), firstitem_(false), latex_(false), : 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) WriteStream & operator<<(WriteStream & ws, MathAtom const & at)
{ {
at->write(ws); at->write(ws);

View File

@ -54,6 +54,10 @@ public:
void pendingSpace(bool how); void pendingSpace(bool how);
/// writes space if next thing is isalpha() /// writes space if next thing is isalpha()
bool pendingSpace() const { return pendingspace_; } 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: private:
/// ///
odocstream & os_; odocstream & os_;
@ -67,6 +71,8 @@ private:
bool dryrun_; bool dryrun_;
/// do we have a space pending? /// do we have a space pending?
bool pendingspace_; bool pendingspace_;
/// are we in text mode when producing latex code?
bool textmode_;
/// ///
int line_; int line_;
}; };