Hello,

This patch implements an idea I had about previews and numbering of equations. It seems to me that it solves issues and is simpler than what we had (which had some bad hardcoding anyway), but since I am not a big preview user, I am not sure of what is fixed, what is still broken and what is newly broken.

Solves #11535 Numbering of repeated previewed equations is wrong

Should not break #6894 HTML export of math equations and referencing
(although I am not sure how to test that)

Please test.

JMarc
From bfdd4dbf8bd01729e0088f96cec0dbe351e7bd48 Mon Sep 17 00:00:00 2001
From: Jean-Marc Lasgouttes <lasgout...@lyx.org>
Date: Wed, 20 Jul 2022 00:32:03 +0200
Subject: [PATCH] WIP: Reimplement support for numbering of equation previews

WARNING: this is experimental code that will obviously have flaws!

This is an experiment for simplifying and fixing the generation of
previews with numbering. The equation number is set explicitely in
each row that needs it with the numbering that has been computed by
updateBuffer.

To this end, InsetMathHull::number_ has been moved to
InsetMathGrid::RowInfo. This is incenditally simpler to handle.

The mechanism uses the \tag{} macro, or provides a replacement when it is missing.

The special code for saving counters from 645f9fdf6d6 (ticket #6894) is
removed and replaced by this simpler mechanism.

This fixes ticket #11535
---
 src/mathed/InsetMathGrid.cpp |  3 ++
 src/mathed/InsetMathGrid.h   |  2 +
 src/mathed/InsetMathHull.cpp | 75 ++++++++----------------------------
 src/mathed/InsetMathHull.h   |  6 ---
 4 files changed, 21 insertions(+), 65 deletions(-)

diff --git a/src/mathed/InsetMathGrid.cpp b/src/mathed/InsetMathGrid.cpp
index 96ec13b954..8fa0495785 100644
--- a/src/mathed/InsetMathGrid.cpp
+++ b/src/mathed/InsetMathGrid.cpp
@@ -1260,6 +1260,9 @@ void InsetMathGrid::write(TeXMathStream & os,
 		col_type lastcol = 0;
 		bool emptyline = true;
 		bool last_eoln = true;
+		if (os.output() == TeXMathStream::wsPreview && !rowinfo_[row].number.empty()) {
+			os << "\\tag{" << rowinfo_[row].number << "}\n";
+		}
 		for (col_type col = beg_col; col < end_col; ++col) {
 			idx_type const idx = index(row, col);
 			bool const empty_cell = cell(idx).empty();
diff --git a/src/mathed/InsetMathGrid.h b/src/mathed/InsetMathGrid.h
index ce8a852401..53525d009b 100644
--- a/src/mathed/InsetMathGrid.h
+++ b/src/mathed/InsetMathGrid.h
@@ -56,6 +56,8 @@ public:
 		mutable int ascent = 0;
 		/// cached offset for each bufferview
 		mutable std::map<BufferView const *, int> offset;
+		/// the equation number computed by updateBuffer
+		mutable docstring number;
 		/// how many hlines above this row?
 		unsigned int lines = 0;
 		/// parameter to the line break
diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index 689950d53e..53f02959c7 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -152,8 +152,7 @@ static InsetLabel * dummy_pointer = 0;
 
 InsetMathHull::InsetMathHull(Buffer * buf)
 	: InsetMathGrid(buf, 1, 1), type_(hullNone), numbered_(1, NONUMBER),
-	  numbers_(1, empty_docstring()), label_(1, dummy_pointer),
-	  preview_(new RenderPreview(this))
+	  label_(1, dummy_pointer), preview_(new RenderPreview(this))
 {
 	//lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
 	//lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
@@ -167,8 +166,7 @@ InsetMathHull::InsetMathHull(Buffer * buf)
 
 InsetMathHull::InsetMathHull(Buffer * buf, HullType type)
 	: InsetMathGrid(buf, getCols(type), 1), type_(type), numbered_(1, NONUMBER),
-	  numbers_(1, empty_docstring()), label_(1, dummy_pointer),
-	  preview_(new RenderPreview(this))
+	  label_(1, dummy_pointer), preview_(new RenderPreview(this))
 {
 	buffer_ = buf;
 	initMath();
@@ -202,7 +200,6 @@ InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
 	InsetMathGrid::operator=(other);
 	type_  = other.type_;
 	numbered_ = other.numbered_;
-	numbers_ = other.numbers_;
 	buffer_ = other.buffer_;
 	for (auto & i : label_)
 		delete i;
@@ -228,14 +225,6 @@ void InsetMathHull::setBuffer(Buffer & buffer)
 }
 
 
-// FIXME This should really be controlled by the TOC level, or
-// something of the sort.
-namespace {
-	const char * counters_to_save[] = {"section", "chapter"};
-	unsigned int const numcnts = sizeof(counters_to_save)/sizeof(char *);
-} // namespace
-
-
 void InsetMathHull::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
 {
 	if (!buffer_) {
@@ -253,30 +242,18 @@ void InsetMathHull::updateBuffer(ParIterator const & it, UpdateType utype, bool
 		Counters & cnts =
 			buffer_->masterBuffer()->params().documentClass().counters();
 
-		// right now, we only need to do this at export time
-		if (utype == OutputUpdate) {
-			for (size_t i = 0; i < numcnts; ++i) {
-				docstring const cnt = from_ascii(counters_to_save[i]);
-				if (cnts.hasCounter(cnt))
-					counter_map[cnt] = cnts.value(cnt);
-			}
-		}
-
-		// this has to be done separately
 		docstring const eqstr = from_ascii("equation");
 		if (cnts.hasCounter(eqstr)) {
-			if (utype == OutputUpdate)
-				counter_map[eqstr] = cnts.value(eqstr);
 			for (size_t i = 0; i != label_.size(); ++i) {
 				if (numbered(i)) {
 					Paragraph const & par = it.paragraph();
 					if (!par.isDeleted(it.pos())) {
 						cnts.step(eqstr, utype);
-						numbers_[i] = cnts.theCounter(eqstr, lang);
+						rowinfo(i).number = cnts.theCounter(eqstr, lang);
 					} else
-						numbers_[i] = from_ascii("#");
+						rowinfo(i).number = from_ascii("#");
 				} else
-					numbers_[i] = empty_docstring();
+					rowinfo(i).number = empty_docstring();
 			}
 		}
 	}
@@ -320,7 +297,7 @@ void InsetMathHull::addToToc(DocIterator const & pit, bool output_active,
 	b.pushItem(pit, docstring(), output_active);
 	if (first != last)
 		b.argumentItem(bformat(from_ascii("(%1$s-%2$s)"),
-		                       numbers_[first], numbers_[last]));
+		                       rowinfo(first).number, rowinfo(last).number));
 	for (row_type row = 0; row != nrows(); ++row) {
 		if (!numbered(row))
 			continue;
@@ -838,26 +815,13 @@ void InsetMathHull::preparePreview(DocIterator const & pos,
 		setfont += from_ascii("\\" + lsize + '\n');
 
 	docstring setcnt;
-	if (forexport && haveNumbers()) {
-		docstring eqstr = from_ascii("equation");
-		CounterMap::const_iterator it = counter_map.find(eqstr);
-		if (it != counter_map.end()) {
-			int num = it->second;
-			if (num >= 0)
-				setcnt += from_ascii("\\setcounter{") + eqstr + '}' +
-				          '{' + convert<docstring>(num) + '}' + '\n';
-		}
-		for (size_t i = 0; i != numcnts; ++i) {
-			docstring cnt = from_ascii(counters_to_save[i]);
-			it = counter_map.find(cnt);
-			if (it == counter_map.end())
-					continue;
-			int num = it->second;
-			if (num > 0)
-				setcnt += from_ascii("\\setcounter{") + cnt + '}' +
-				          '{' + convert<docstring>(num) + '}';
-		}
+	if (haveNumbers()) {
+		setcnt = from_ascii(
+			"\\makeatletter\n"
+			"\\providecommand{\\tag}[1]{\\def\\@eqnnum{(#1)}}\n"
+			"\\makeatother\n");
 	}
+
 	docstring const snippet = macro_preamble + setfont + setcnt
 	                          + latexString(*this) + endfont;
 	LYXERR(Debug::MACROS, "Preview snippet: " << snippet);
@@ -1302,18 +1266,15 @@ void InsetMathHull::addRow(row_type row)
 	bool numbered = numberedType();
 	// Move the number and raw pointer, do not call label() (bug 7511)
 	InsetLabel * label = dummy_pointer;
-	docstring number = empty_docstring();
 	if (type_ == hullMultline) {
 		if (row + 1 == nrows())  {
 			numbered_[row] = NONUMBER;
 			swap(label, label_[row]);
-			swap(number, numbers_[row]);
 		} else
 			numbered = false;
 	}
 
 	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);
 }
@@ -1326,7 +1287,6 @@ void InsetMathHull::swapRow(row_type row)
 	if (row + 1 == nrows())
 		--row;
 	swap(numbered_[row], numbered_[row + 1]);
-	swap(numbers_[row], numbers_[row + 1]);
 	swap(label_[row], label_[row + 1]);
 	InsetMathGrid::swapRow(row);
 }
@@ -1338,7 +1298,6 @@ void InsetMathHull::delRow(row_type row)
 		return;
 	if (row + 1 == nrows() && type_ == hullMultline) {
 		swap(numbered_[row - 1], numbered_[row]);
-		swap(numbers_[row - 1], numbers_[row]);
 		swap(label_[row - 1], label_[row]);
 		InsetMathGrid::delRow(row);
 		return;
@@ -1349,7 +1308,6 @@ void InsetMathHull::delRow(row_type row)
 	if (row == nrows() + 1)
 		row--;
 	numbered_.erase(numbered_.begin() + row);
-	numbers_.erase(numbers_.begin() + row);
 	delete label_[row];
 	label_.erase(label_.begin() + row);
 }
@@ -1378,7 +1336,7 @@ docstring InsetMathHull::nicelabel(row_type row) const
 			return docstring();
 		return '[' + label_[row]->screenLabel() + ']';
 	}
-	docstring const & val = numbers_[row];
+	docstring const & val = rowinfo(row).number;
 	if (!label_[row])
 		return '(' + val + ')';
 	return '(' + val + ',' + label_[row]->screenLabel() + ')';
@@ -1729,7 +1687,6 @@ void InsetMathHull::infoize(odocstream & os) const
 void InsetMathHull::check() const
 {
 	LATTEST(numbered_.size() == nrows());
-	LATTEST(numbers_.size() == nrows());
 	LATTEST(label_.size() == nrows());
 }
 
@@ -2530,7 +2487,7 @@ void InsetMathHull::htmlize(HtmlStream & os) const
 		}
 		if (havenumbers) {
 			os << MTag("td");
-			docstring const & num = numbers_[row];
+			docstring const & num = rowinfo(row).number;
 			if (!num.empty())
 				os << '(' << num << ')';
 		  os << ETag("td");
@@ -2578,7 +2535,7 @@ void InsetMathHull::mathmlize(MathMLStream & ms) const
 		// fleqn?
 		if (haveNumbers()) {
 			ms << MTag("mtd");
-			docstring const & num = numbers_[row];
+			docstring const & num = rowinfo(row).number;
 			if (!num.empty())
 				ms << MTagInline("mtext") << '(' << num << ')' << ETagInline("mtext");
 		    ms << ETag("mtd");
@@ -2612,7 +2569,7 @@ void InsetMathHull::mathAsLatex(TeXMathStream & os) const
 		}
 		if (havenumbers) {
 			os << "<td>";
-			docstring const & num = numbers_[row];
+			docstring const & num = rowinfo(row).number;
 			if (!num.empty())
 				os << '(' << num << ')';
 		  os << "</td>";
diff --git a/src/mathed/InsetMathHull.h b/src/mathed/InsetMathHull.h
index b0191886e4..593b16509f 100644
--- a/src/mathed/InsetMathHull.h
+++ b/src/mathed/InsetMathHull.h
@@ -263,17 +263,11 @@ private:
 	///
 	std::vector<Numbered> numbered_;
 	///
-	std::vector<docstring> numbers_;
-	///
 	std::vector<InsetLabel *> label_;
 	///
 	unique_ptr<RenderPreview> preview_;
 	///
 	DocIterator docit_;
-	///
-	typedef std::map<docstring, int> CounterMap;
-	/// used to store current values of important counters
-	CounterMap counter_map;
 //
 // Incorporate me
 //
-- 
2.34.1

-- 
lyx-devel mailing list
lyx-devel@lists.lyx.org
http://lists.lyx.org/mailman/listinfo/lyx-devel

Reply via email to