Native support for \smash[t] and \smash[b]

This is a follow-up of bug #8967. The implementation is self-explaining, the
only part which needs a comment is lyx2lyx: Since a 100% correct solution is
not possible, it has been decided not to switch amsmath off in the forward
conversion if no other ams command than \smash[t] and \smash[b] is used, but
to consider it a bug that older versions do not load amsmath automatically for
these commands. In the backward direction it is easy to keep the document
compilable, so just do that.
This commit is contained in:
Georg Baum 2014-05-27 22:10:02 +02:00
parent ff8450f839
commit 5dc9568f8d
25 changed files with 120 additions and 31 deletions

View File

@ -11,6 +11,11 @@ adjustments are made to tex2lyx and bugs are fixed in lyx2lyx.
-----------------------
2013-05-27 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* Format incremented to 476
Native support for \smash[t] and \smash[b] with automatic amsmath
loading
2014-05-05 Enrico Forestieri <forenr@lyx.org>
* Format incremented to 475
New Separator insets. The parbreak separator introduces a LaTeX

View File

@ -1180,6 +1180,8 @@ dist_imagesmath_DATA = \
images/math/smallsetminus.png \
images/math/smallsmile.png \
images/math/smash.png \
images/math/smashb.png \
images/math/smasht.png \
images/math/smile.png \
images/math/smiley.png \
images/math/space.png \

BIN
lib/images/math/smashb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 B

BIN
lib/images/math/smasht.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 B

View File

@ -85,7 +85,7 @@ format_relation = [("0_06", [200], minor_versions("0.6" , 4)),
("1_6", range(277,346), minor_versions("1.6" , 10)),
("2_0", range(346,414), minor_versions("2.0", 8)),
("2_1", range(414,475), minor_versions("2.1", 0)),
("2_2", range(475,476), minor_versions("2.2", 0))
("2_2", range(475,477), minor_versions("2.2", 0))
]
####################################################################

View File

@ -244,6 +244,37 @@ def revert_separator(document):
i = i + 1
def revert_smash(document):
" Set amsmath to on if smash commands are used "
commands = ["smash[t]", "smash[b]"]
i = find_token(document.header, "\\use_package amsmath", 0)
if i == -1:
document.warning("Malformed LyX document: Can't find \\use_package amsmath.")
return;
value = get_value(document.header, "\\use_package amsmath", i).split()[1]
if value != "1":
# nothing to do if package is not auto but on or off
return;
j = 0
while True:
j = find_token(document.body, '\\begin_inset Formula', j)
if j == -1:
return
k = find_end_of_inset(document.body, j)
if k == -1:
document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(j))
j += 1
continue
code = "\n".join(document.body[j:k])
for c in commands:
if code.find("\\%s" % c) != -1:
# set amsmath to on, since it is loaded by the newer format
document.header[i] = "\\use_package amsmath 2"
return
j = k
##
# Conversion hub
#
@ -251,9 +282,14 @@ def revert_separator(document):
supported_versions = ["2.2.0","2.2"]
convert = [
[475, [convert_separator]],
# nothing to do for 476: We consider it a bug that older versions
# did not load amsmath automatically for these commands, and do not
# want to hardcode amsmath off.
[476, []],
]
revert = [
[475, [revert_smash]],
[474, [revert_separator]]
]

View File

@ -353,6 +353,8 @@ ToolbarSet
Item "Horizontal phantom \\hphantom" "math-insert \hphantom"
Item "Vertical phantom \\vphantom" "math-insert \vphantom"
Item "Smash \\smash" "math-insert \smash"
Item "Top smash \\smasht" "math-insert \smasht"
Item "Bottom smash \\smashb" "math-insert \smashb"
Item "Left overlap \\mathllap" "math-insert \mathllap"
Item "Center overlap \\mathclap" "math-insert \mathclap"
Item "Right overlap \\mathrlap" "math-insert \mathrlap"

View File

@ -197,7 +197,7 @@ void InsetMathPhantom::draw(PainterInfo & pi, int x, int y) const
pi.pain.line(x1, y2, x3, y2, Color_added_space);
}
else if (kind_ == smash) {
else if (kind_ == smash || kind_ == smasht || kind_ == smashb) {
// y1---------
// |
// y2----- \ | /
@ -222,15 +222,24 @@ void InsetMathPhantom::draw(PainterInfo & pi, int x, int y) const
int const y4 = std::min(y5, y3 + arrow_size);
// top arrow
pi.pain.line(x1, y2, x2, y3, Color_added_space);
pi.pain.line(x3, y2, x2, y3, Color_added_space);
if (kind_ != smashb) {
pi.pain.line(x1, y2, x2, y3, Color_added_space);
pi.pain.line(x3, y2, x2, y3, Color_added_space);
}
// bottom arrow
pi.pain.line(x1, y4, x2, y3, Color_added_space);
pi.pain.line(x3, y4, x2, y3, Color_added_space);
if (kind_ != smasht) {
pi.pain.line(x1, y4, x2, y3, Color_added_space);
pi.pain.line(x3, y4, x2, y3, Color_added_space);
}
// joining line
pi.pain.line(x2, y1, x2, y5, Color_added_space);
if (kind_ == smasht)
pi.pain.line(x2, y1, x2, y3, Color_added_space);
else if (kind_ == smashb)
pi.pain.line(x2, y3, x2, y5, Color_added_space);
else
pi.pain.line(x2, y1, x2, y5, Color_added_space);
}
drawMarkers(pi, x, y);
@ -255,6 +264,12 @@ void InsetMathPhantom::write(WriteStream & os) const
case smash:
os << "\\smash{";
break;
case smasht:
os << "\\smash[t]{";
break;
case smashb:
os << "\\smash[b]{";
break;
case mathclap:
os << "\\mathclap{";
break;
@ -284,6 +299,12 @@ void InsetMathPhantom::normalize(NormalStream & os) const
case smash:
os << "[smash ";
break;
case smasht:
os << "[smasht ";
break;
case smashb:
os << "[smashb ";
break;
case mathclap:
os << "[mathclap ";
break;
@ -313,6 +334,12 @@ void InsetMathPhantom::infoize(odocstream & os) const
case smash:
os << "Smash";
break;
case smasht:
os << "Smashtop";
break;
case smashb:
os << "Smashbottom";
break;
case mathllap:
os << "Mathllap";
break;
@ -335,6 +362,10 @@ void InsetMathPhantom::validate(LaTeXFeatures & features) const
case hphantom:
case smash:
break;
case smasht:
case smashb:
features.require("amsmath");
break;
case mathclap:
case mathllap:
case mathrlap:

View File

@ -25,6 +25,8 @@ public:
vphantom,
hphantom,
smash,
smasht,
smashb,
mathclap,
mathllap,
mathrlap

View File

@ -561,6 +561,11 @@ MathAtom createInsetMath(docstring const & s, Buffer * buf)
return MathAtom(new InsetMathCancelto(buf));
if (s == "smash")
return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
// The following 2 string values are only for math toolbar use, no LaTeX names
if (s == "smashb")
return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
if (s == "smasht")
return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
if (s == "mathclap")
return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
if (s == "mathllap")

View File

@ -1849,16 +1849,22 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
skipSpaces();
if (nextToken().asInput() == "[") {
// Since the phantom inset cannot handle optional arguments
// we must not create an InsetMathPhantom (bug 8967).
// other than b and t, we must not create an InsetMathPhantom
// if opt is different from b and t (bug 8967).
docstring const opt = parse_verbatim_option();
docstring const arg = parse_verbatim_item();
cell->push_back(MathAtom(new MathMacro(buf, t.cs())));
MathData ar;
mathed_parse_cell(ar, '[' + opt + ']', mode_);
cell->append(ar);
ar = MathData();
mathed_parse_cell(ar, '{' + arg + '}', mode_);
cell->append(ar);
if (opt == "t" || opt == "b") {
cell->push_back(createInsetMath(t.cs() + opt, buf));
parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
} else {
docstring const arg = parse_verbatim_item();
cell->push_back(MathAtom(new MathMacro(buf, t.cs())));
MathData ar;
mathed_parse_cell(ar, '[' + opt + ']', mode_);
cell->append(ar);
ar = MathData();
mathed_parse_cell(ar, '{' + arg + '}', mode_);
cell->append(ar);
}
}
else {
cell->push_back(createInsetMath(t.cs(), buf));

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass article

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass article

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass article

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass article

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass article

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass article

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass article

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass article

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass amsart

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass book

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass article

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass article

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.2
\lyxformat 475
\lyxformat 476
\begin_document
\begin_header
\textclass article

View File

@ -30,8 +30,8 @@ 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 475 // ef: new separator inset
#define LYX_FORMAT_TEX2LYX 475
#define LYX_FORMAT_LYX 476 // gb: \smash[t] and \smash[b]
#define LYX_FORMAT_TEX2LYX 476
#if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX
#ifndef _MSC_VER