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):
" 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)
if i == -1:
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;
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),
preview_(new RenderPreview(this)), use_preview_(false)
{
@ -164,7 +164,7 @@ InsetMathHull::InsetMathHull(Buffer * buf)
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),
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");
for (row_type row = 0; row != nrows(); ++row) {
if (!numbered_[row])
if (!numbered(row))
continue;
if (label_[row])
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;
if (!numbered_[row] && label_[row]) {
if (!numbered(row) && label_[row]) {
delete label_[row];
label_[row] = 0;
}
@ -742,19 +742,32 @@ void InsetMathHull::numbered(row_type row, bool num)
bool InsetMathHull::numbered(row_type row) const
{
return numbered_[row];
return numbered_[row] == NUMBER;
}
bool InsetMathHull::ams() const
{
return type_ == hullAlign
|| type_ == hullFlAlign
|| type_ == hullMultline
|| type_ == hullGather
|| type_ == hullAlignAt
|| type_ == hullXAlignAt
|| type_ == hullXXAlignAt;
switch (type_) {
case hullAlign:
case hullFlAlign:
case hullMultline:
case hullGather:
case hullAlignAt:
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)
return false;
for (row_type row = 0; row < nrows(); ++row)
if (numbered_[row])
if (numbered(row))
return true;
return false;
}
@ -946,14 +959,14 @@ void InsetMathHull::addRow(row_type row)
docstring number = empty_docstring();
if (type_ == hullMultline) {
if (row + 1 == nrows()) {
numbered_[row] = false;
numbered_[row] = NONUMBER;
swap(label, label_[row]);
swap(number, numbers_[row]);
} else
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);
label_.insert(label_.begin() + row + 1, label);
InsetMathGrid::addRow(row);
@ -966,14 +979,7 @@ void InsetMathHull::swapRow(row_type row)
return;
if (row + 1 == nrows())
--row;
// gcc implements the standard std::vector<bool> which is *not* a container:
// 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(numbered_[row], numbered_[row + 1]);
swap(numbers_[row], numbers_[row + 1]);
swap(label_[row], label_[row + 1]);
InsetMathGrid::swapRow(row);
@ -985,9 +991,7 @@ void InsetMathHull::delRow(row_type row)
if (nrows() <= 1 || !rowChangeOK())
return;
if (row + 1 == nrows() && type_ == hullMultline) {
bool const b = numbered_[row - 1];
numbered_[row - 1] = numbered_[row];
numbered_[row] = b;
swap(numbered_[row - 1], numbered_[row]);
swap(numbers_[row - 1], numbers_[row]);
swap(label_[row - 1], label_[row]);
InsetMathGrid::delRow(row);
@ -1023,7 +1027,7 @@ void InsetMathHull::delCol(col_type col)
docstring InsetMathHull::nicelabel(row_type row) const
{
if (!numbered_[row])
if (!numbered(row))
return docstring();
docstring const & val = numbers_[row];
if (!label_[row])
@ -1260,14 +1264,18 @@ docstring InsetMathHull::eolString(row_type row, bool fragile, bool latex,
{
docstring res;
if (numberedType()) {
if (label_[row] && numbered_[row]) {
if (label_[row] && numbered(row)) {
docstring const name =
latex ? escape(label_[row]->getParam("name"))
: label_[row]->getParam("name");
res += "\\label{" + name + '}';
}
if (!numbered_[row] && (type_ != hullMultline))
res += "\\nonumber ";
if (type_ != hullMultline) {
if (numbered_[row] == NONUMBER)
res += "\\nonumber ";
else if (numbered_[row] == NOTAG)
res += "\\notag ";
}
}
// Never add \\ on the last empty line of eqnarray and friends
last_eoln = false;
@ -1479,7 +1487,7 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
// if there is an argument, find the corresponding label, else
// check whether there is at least one label.
for (row = 0; row != nrows(); ++row)
if (numbered_[row] && label_[row]
if (numbered(row) && label_[row]
&& (cmd.argument().empty() || label(row) == cmd.argument()))
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
// the row number from the cursor position.
row = (type_ == hullMultline) ? nrows() - 1 : cur.row();
enabled = numberedType() && label_[row] && numbered_[row];
enabled = numberedType() && label_[row] && numbered(row);
} else {
// if there is an argument, find the corresponding label, else
// check whether there is at least one label.
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())) {
enabled = true;
break;
@ -2038,7 +2046,7 @@ bool InsetMathHull::haveNumbers() const
if (getType() == hullSimple)
return havenumbers;
for (size_t i = 0; i != numbered_.size(); ++i) {
if (numbered_[i]) {
if (numbered(i)) {
havenumbers = true;
break;
}

View File

@ -30,6 +30,15 @@ class RenderPreview;
/// This provides an interface between "LyX insets" and "LyX math insets"
class InsetMathHull : public InsetMathGrid {
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);
///
@ -65,7 +74,9 @@ public:
///
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;
///
@ -231,7 +242,7 @@ private:
/// "none", "simple", "display", "eqnarray",...
HullType type_;
///
std::vector<bool> numbered_;
std::vector<Numbered> numbered_;
///
std::vector<docstring> numbers_;
///

View File

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