Native support for \notag

This is mainly needed to reduce the amount of ERT if you convert AMS example
documents with tex2lyx. No GUI support is needed, since \notag is equivalent
to \nonumber.
This commit is contained in:
Georg Baum 2014-05-27 22:17:35 +02:00
parent 5dc9568f8d
commit 4cea2efe21
5 changed files with 64 additions and 47 deletions

View File

@ -247,7 +247,7 @@ def revert_separator(document):
def revert_smash(document): def revert_smash(document):
" Set amsmath to on if smash commands are used " " Set amsmath to on if smash commands are used "
commands = ["smash[t]", "smash[b]"] commands = ["smash[t]", "smash[b]", "notag"]
i = find_token(document.header, "\\use_package amsmath", 0) i = find_token(document.header, "\\use_package amsmath", 0)
if i == -1: if i == -1:
document.warning("Malformed LyX document: Can't find \\use_package amsmath.") document.warning("Malformed LyX document: Can't find \\use_package amsmath.")

View File

@ -149,7 +149,7 @@ docstring hullName(HullType type)
static InsetLabel * dummy_pointer = 0; static InsetLabel * dummy_pointer = 0;
InsetMathHull::InsetMathHull(Buffer * buf) InsetMathHull::InsetMathHull(Buffer * buf)
: InsetMathGrid(buf, 1, 1), type_(hullNone), numbered_(1, true), : InsetMathGrid(buf, 1, 1), type_(hullNone), numbered_(1, NUMBER),
numbers_(1, empty_docstring()), label_(1, dummy_pointer), numbers_(1, empty_docstring()), label_(1, dummy_pointer),
preview_(new RenderPreview(this)), use_preview_(false) preview_(new RenderPreview(this)), use_preview_(false)
{ {
@ -164,7 +164,7 @@ InsetMathHull::InsetMathHull(Buffer * buf)
InsetMathHull::InsetMathHull(Buffer * buf, HullType type) InsetMathHull::InsetMathHull(Buffer * buf, HullType type)
: InsetMathGrid(buf, getCols(type), 1), type_(type), numbered_(1, true), : InsetMathGrid(buf, getCols(type), 1), type_(type), numbered_(1, NUMBER),
numbers_(1, empty_docstring()), label_(1, dummy_pointer), numbers_(1, empty_docstring()), label_(1, dummy_pointer),
preview_(new RenderPreview(this)), use_preview_(false) preview_(new RenderPreview(this)), use_preview_(false)
{ {
@ -297,7 +297,7 @@ void InsetMathHull::addToToc(DocIterator const & pit, bool output_active) const
Toc & toc = buffer().tocBackend().toc("equation"); Toc & toc = buffer().tocBackend().toc("equation");
for (row_type row = 0; row != nrows(); ++row) { for (row_type row = 0; row != nrows(); ++row) {
if (!numbered_[row]) if (!numbered(row))
continue; continue;
if (label_[row]) if (label_[row])
label_[row]->addToToc(pit, output_active); label_[row]->addToToc(pit, output_active);
@ -730,10 +730,10 @@ void InsetMathHull::label(row_type row, docstring const & label)
} }
void InsetMathHull::numbered(row_type row, bool num) void InsetMathHull::numbered(row_type row, Numbered num)
{ {
numbered_[row] = num; numbered_[row] = num;
if (!numbered_[row] && label_[row]) { if (!numbered(row) && label_[row]) {
delete label_[row]; delete label_[row];
label_[row] = 0; label_[row] = 0;
} }
@ -742,19 +742,32 @@ void InsetMathHull::numbered(row_type row, bool num)
bool InsetMathHull::numbered(row_type row) const bool InsetMathHull::numbered(row_type row) const
{ {
return numbered_[row]; return numbered_[row] == NUMBER;
} }
bool InsetMathHull::ams() const bool InsetMathHull::ams() const
{ {
return type_ == hullAlign switch (type_) {
|| type_ == hullFlAlign case hullAlign:
|| type_ == hullMultline case hullFlAlign:
|| type_ == hullGather case hullMultline:
|| type_ == hullAlignAt case hullGather:
|| type_ == hullXAlignAt case hullAlignAt:
|| type_ == hullXXAlignAt; case hullXAlignAt:
case hullXXAlignAt:
return true;
case hullNone:
case hullSimple:
case hullEquation:
case hullEqnArray:
case hullRegexp:
break;
}
for (size_t row = 0; row < numbered_.size(); ++row)
if (numbered_[row] == NOTAG)
return true;
return false;
} }
@ -776,7 +789,7 @@ bool InsetMathHull::numberedType() const
if (type_ == hullRegexp) if (type_ == hullRegexp)
return false; return false;
for (row_type row = 0; row < nrows(); ++row) for (row_type row = 0; row < nrows(); ++row)
if (numbered_[row]) if (numbered(row))
return true; return true;
return false; return false;
} }
@ -946,14 +959,14 @@ void InsetMathHull::addRow(row_type row)
docstring number = empty_docstring(); docstring number = empty_docstring();
if (type_ == hullMultline) { if (type_ == hullMultline) {
if (row + 1 == nrows()) { if (row + 1 == nrows()) {
numbered_[row] = false; numbered_[row] = NONUMBER;
swap(label, label_[row]); swap(label, label_[row]);
swap(number, numbers_[row]); swap(number, numbers_[row]);
} else } else
numbered = false; numbered = false;
} }
numbered_.insert(numbered_.begin() + row + 1, numbered); numbered_.insert(numbered_.begin() + row + 1, numbered ? NUMBER : NONUMBER);
numbers_.insert(numbers_.begin() + row + 1, number); numbers_.insert(numbers_.begin() + row + 1, number);
label_.insert(label_.begin() + row + 1, label); label_.insert(label_.begin() + row + 1, label);
InsetMathGrid::addRow(row); InsetMathGrid::addRow(row);
@ -966,14 +979,7 @@ void InsetMathHull::swapRow(row_type row)
return; return;
if (row + 1 == nrows()) if (row + 1 == nrows())
--row; --row;
// gcc implements the standard std::vector<bool> which is *not* a container: swap(numbered_[row], numbered_[row + 1]);
// http://www.gotw.ca/publications/N1185.pdf
// As a results, it doesn't like this:
// swap(numbered_[row], numbered_[row + 1]);
// so we do it manually:
bool const b = numbered_[row];
numbered_[row] = numbered_[row + 1];
numbered_[row + 1] = b;
swap(numbers_[row], numbers_[row + 1]); swap(numbers_[row], numbers_[row + 1]);
swap(label_[row], label_[row + 1]); swap(label_[row], label_[row + 1]);
InsetMathGrid::swapRow(row); InsetMathGrid::swapRow(row);
@ -985,9 +991,7 @@ void InsetMathHull::delRow(row_type row)
if (nrows() <= 1 || !rowChangeOK()) if (nrows() <= 1 || !rowChangeOK())
return; return;
if (row + 1 == nrows() && type_ == hullMultline) { if (row + 1 == nrows() && type_ == hullMultline) {
bool const b = numbered_[row - 1]; swap(numbered_[row - 1], numbered_[row]);
numbered_[row - 1] = numbered_[row];
numbered_[row] = b;
swap(numbers_[row - 1], numbers_[row]); swap(numbers_[row - 1], numbers_[row]);
swap(label_[row - 1], label_[row]); swap(label_[row - 1], label_[row]);
InsetMathGrid::delRow(row); InsetMathGrid::delRow(row);
@ -1023,7 +1027,7 @@ void InsetMathHull::delCol(col_type col)
docstring InsetMathHull::nicelabel(row_type row) const docstring InsetMathHull::nicelabel(row_type row) const
{ {
if (!numbered_[row]) if (!numbered(row))
return docstring(); return docstring();
docstring const & val = numbers_[row]; docstring const & val = numbers_[row];
if (!label_[row]) if (!label_[row])
@ -1260,14 +1264,18 @@ docstring InsetMathHull::eolString(row_type row, bool fragile, bool latex,
{ {
docstring res; docstring res;
if (numberedType()) { if (numberedType()) {
if (label_[row] && numbered_[row]) { if (label_[row] && numbered(row)) {
docstring const name = docstring const name =
latex ? escape(label_[row]->getParam("name")) latex ? escape(label_[row]->getParam("name"))
: label_[row]->getParam("name"); : label_[row]->getParam("name");
res += "\\label{" + name + '}'; res += "\\label{" + name + '}';
} }
if (!numbered_[row] && (type_ != hullMultline)) if (type_ != hullMultline) {
res += "\\nonumber "; if (numbered_[row] == NONUMBER)
res += "\\nonumber ";
else if (numbered_[row] == NOTAG)
res += "\\notag ";
}
} }
// Never add \\ on the last empty line of eqnarray and friends // Never add \\ on the last empty line of eqnarray and friends
last_eoln = false; last_eoln = false;
@ -1479,7 +1487,7 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
// if there is an argument, find the corresponding label, else // if there is an argument, find the corresponding label, else
// check whether there is at least one label. // check whether there is at least one label.
for (row = 0; row != nrows(); ++row) for (row = 0; row != nrows(); ++row)
if (numbered_[row] && label_[row] if (numbered(row) && label_[row]
&& (cmd.argument().empty() || label(row) == cmd.argument())) && (cmd.argument().empty() || label(row) == cmd.argument()))
break; break;
} }
@ -1672,12 +1680,12 @@ bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
// if there is no argument and we're inside math, we retrieve // if there is no argument and we're inside math, we retrieve
// the row number from the cursor position. // the row number from the cursor position.
row = (type_ == hullMultline) ? nrows() - 1 : cur.row(); row = (type_ == hullMultline) ? nrows() - 1 : cur.row();
enabled = numberedType() && label_[row] && numbered_[row]; enabled = numberedType() && label_[row] && numbered(row);
} else { } else {
// if there is an argument, find the corresponding label, else // if there is an argument, find the corresponding label, else
// check whether there is at least one label. // check whether there is at least one label.
for (row_type row = 0; row != nrows(); ++row) { for (row_type row = 0; row != nrows(); ++row) {
if (numbered_[row] && label_[row] && if (numbered(row) && label_[row] &&
(cmd.argument().empty() || label(row) == cmd.argument())) { (cmd.argument().empty() || label(row) == cmd.argument())) {
enabled = true; enabled = true;
break; break;
@ -2038,7 +2046,7 @@ bool InsetMathHull::haveNumbers() const
if (getType() == hullSimple) if (getType() == hullSimple)
return havenumbers; return havenumbers;
for (size_t i = 0; i != numbered_.size(); ++i) { for (size_t i = 0; i != numbered_.size(); ++i) {
if (numbered_[i]) { if (numbered(i)) {
havenumbers = true; havenumbers = true;
break; break;
} }

View File

@ -30,6 +30,15 @@ class RenderPreview;
/// This provides an interface between "LyX insets" and "LyX math insets" /// This provides an interface between "LyX insets" and "LyX math insets"
class InsetMathHull : public InsetMathGrid { class InsetMathHull : public InsetMathGrid {
public: public:
/// How a line is numbered
enum Numbered {
/// not numbered, LaTeX code \\nonumber if line differs from inset
NONUMBER,
/// numbered, LaTeX code \\number if line differs from inset
NUMBER,
/// not numbered, LaTeX code \\notag if line differs from inset
NOTAG
};
/// ///
InsetMathHull(Buffer * buf); InsetMathHull(Buffer * buf);
/// ///
@ -65,7 +74,9 @@ public:
/// ///
ColorCode backgroundColor(PainterInfo const &) const; ColorCode backgroundColor(PainterInfo const &) const;
/// ///
void numbered(row_type row, bool num); void numbered(row_type row, bool num) { numbered(row, num ? NUMBER : NONUMBER); }
///
void numbered(row_type row, Numbered num);
/// ///
bool numbered(row_type row) const; bool numbered(row_type row) const;
/// ///
@ -231,7 +242,7 @@ private:
/// "none", "simple", "display", "eqnarray",... /// "none", "simple", "display", "eqnarray",...
HullType type_; HullType type_;
/// ///
std::vector<bool> numbered_; std::vector<Numbered> numbered_;
/// ///
std::vector<docstring> numbers_; std::vector<docstring> numbers_;
/// ///

View File

@ -1368,15 +1368,13 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
} }
} }
else if (t.cs() == "nonumber") { // \notag is the same as \nonumber if amsmath is used
if (grid.asHullInset()) else if ((t.cs() == "nonumber" || t.cs() == "notag") &&
grid.asHullInset()->numbered(cellrow, false); grid.asHullInset())
} grid.asHullInset()->numbered(cellrow, false);
else if (t.cs() == "number") { else if (t.cs() == "number" && grid.asHullInset())
if (grid.asHullInset()) grid.asHullInset()->numbered(cellrow, true);
grid.asHullInset()->numbered(cellrow, true);
}
else if (t.cs() == "hline") { else if (t.cs() == "hline") {
grid.rowinfo(cellrow).lines_ ++; grid.rowinfo(cellrow).lines_ ++;

View File

@ -30,7 +30,7 @@ extern char const * const lyx_version_info;
// Do not remove the comment below, so we get merge conflict in // Do not remove the comment below, so we get merge conflict in
// independent branches. Instead add your own. // independent branches. Instead add your own.
#define LYX_FORMAT_LYX 476 // gb: \smash[t] and \smash[b] #define LYX_FORMAT_LYX 476 // gb: \smash[t], \smash[b] and \notag
#define LYX_FORMAT_TEX2LYX 476 #define LYX_FORMAT_TEX2LYX 476
#if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX #if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX