Load mathtools.sty automatically (bug #7949) and make lyx2lyx reversion

more robust.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@40574 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2012-01-05 20:53:48 +00:00
parent a86893a84c
commit 6332326cf6
9 changed files with 101 additions and 52 deletions

View File

@ -11,6 +11,11 @@ adjustments are made to tex2lyx and bugs are fixed in lyx2lyx.
-----------------------
2012-01-05 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* Format incremented to 423 (r40574)
support for the LaTeX-package mathtools (fix bug 7949)
New buffer param \use_package mathtools
2012-01-03 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* Format incremented to 422 (r40563)
\use_amsmath 0/1/2 => \use_package amsmath 0/1/2

View File

@ -35,7 +35,7 @@ from parser_tools import del_token, find_token, find_end_of, find_end_of_inset,
from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
#from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \
#from lyx2lyx_tools import insert_to_preamble, \
# put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
# revert_font_attrs, hex2ratio, str2bool
@ -99,7 +99,7 @@ def revert_undertilde(document):
# force load case
add_to_preamble(document, ["\\usepackage{undertilde}"])
return
# so we are in the auto case. we want to load undertilde if \utilde is used.
i = 0
while True:
@ -344,13 +344,62 @@ def convert_use_packages(document):
def revert_use_packages(document):
"use_package xxx yyy => use_xxx yyy"
packages = ["amsmath", "esint", "mathdots", "mhchem", "undertilde"]
for p in packages:
packages = {"amsmath":"1", "esint":"1", "mathdots":"1", "mhchem":"1", "undertilde":"1"}
# the order is arbitrary for the use_package version, and not all packages need to be given.
# Ensure a complete list and correct order (important for older LyX versions and especially lyx2lyx)
j = -1
for p in packages.keys():
regexp = re.compile(r'(\\use_package\s+%s)' % p)
i = find_re(document.header, regexp, 0)
if i != -1:
values = get_value(document.header, "\\use_package" , i)
document.header[i] = "\\use_%s" % values
value = get_value(document.header, "\\use_package" , i).split()[1]
del document.header[i]
j = i
for (p, v) in packages.items():
document.header.insert(j, "\\use_%s %s" % (p, value))
j = j + 1
def convert_use_mathtools(document):
"insert use_package mathtools"
i = find_token(document.header, "\\use_package", 0)
if i == -1:
document.warning("Malformed LyX document: Can't find \\use_package.")
return;
document.header.insert(i + 1, "\\use_package mathtools 0")
def revert_use_mathtools(document):
"remove use_package mathtools"
regexp = re.compile(r'(\\use_package\s+mathtools)')
i = find_re(document.header, regexp, 0)
value = "1" # default is auto
if i != -1:
value = get_value(document.header, "\\use_package" , i).split()[1]
del document.header[i]
if value == "2": # on
add_to_preamble(document, ["\\usepackage{mathtools}"])
elif value == "1": # auto
commands = ["lgathered", "rgathered", "vcentcolon", "dblcolon", \
"coloneqq", "Coloneqq", "coloneq", "Coloneq", "eqqcolon", \
"Eqqcolon", "eqcolon", "Eqcolon", "colonapprox", \
"Colonapprox", "colonsim", "Colonsim"]
i = 0
while True:
i = find_token(document.body, '\\begin_inset Formula', i)
if i == -1:
return
j = find_end_of_inset(document.body, i)
if j == -1:
document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
i += 1
continue
code = "\n".join(document.body[i:j])
for c in commands:
if code.find("\\%s" % c) != -1:
add_to_preamble(document, ["\\usepackage{mathtools}"])
return
i = j
##
@ -368,9 +417,11 @@ convert = [
[420, [convert_biblio_style]],
[421, [convert_longtable_captions]],
[422, [convert_use_packages]],
[423, [convert_use_mathtools]],
]
revert = [
[422, [revert_use_mathtools]],
[421, [revert_use_packages]],
[420, [revert_longtable_captions]],
[419, [revert_biblio_style]],

View File

@ -932,21 +932,20 @@ pod lyxblacktext 0 0 func x amsmath
# mathtools.sty
# no automatic loading without file format change
\def\vcentcolon{\kern4mu:\kern3mu}
\def\dblcolon{\vcentcolon\kern-8mu\vcentcolon}
\def\coloneqq{\vcentcolon\kern-7mu=}
\def\Coloneqq{\dblcolon\kern-7mu=}
\def\coloneq{\vcentcolon\kern-7mu-}
\def\Coloneq{\dblcolon\kern-7mu-}
\def\eqqcolon{=\kern-8mu\vcentcolon}
\def\Eqqcolon{=\kern-8mu\dblcolon}
\def\eqcolon{-\kern-8mu\vcentcolon}
\def\Eqcolon{-\kern-8mu\dblcolon}
\def\colonapprox{\vcentcolon\kern-7mu\approx}
\def\Colonapprox{\dblcolon\kern-7mu\approx}
\def\colonsim{\vcentcolon\kern-7mu\sim}
\def\Colonsim{\dblcolon\kern-7mu\sim}
\def\vcentcolon{\kern4mu:\kern3mu} mathtools
\def\dblcolon{\vcentcolon\kern-8mu\vcentcolon} mathtools
\def\coloneqq{\vcentcolon\kern-7mu=} mathtools
\def\Coloneqq{\dblcolon\kern-7mu=} mathtools
\def\coloneq{\vcentcolon\kern-7mu-} mathtools
\def\Coloneq{\dblcolon\kern-7mu-} mathtools
\def\eqqcolon{=\kern-8mu\vcentcolon} mathtools
\def\Eqqcolon{=\kern-8mu\dblcolon} mathtools
\def\eqcolon{-\kern-8mu\vcentcolon} mathtools
\def\Eqcolon{-\kern-8mu\dblcolon} mathtools
\def\colonapprox{\vcentcolon\kern-7mu\approx} mathtools
\def\Colonapprox{\dblcolon\kern-7mu\approx} mathtools
\def\colonsim{\vcentcolon\kern-7mu\sim} mathtools
\def\Colonsim{\dblcolon\kern-7mu\sim} mathtools
#

View File

@ -450,6 +450,7 @@ vector<string> const & BufferParams::auto_packages()
packages.push_back("amsmath");
packages.push_back("esint");
packages.push_back("mathdots");
packages.push_back("mathtools");
packages.push_back("mhchem");
packages.push_back("undertilde");
}

View File

@ -186,6 +186,10 @@ char const * packages_gui[][4] =
N_("Use math&dots package automatically"),
N_("Use mathdo&ts package"),
N_("The LaTeX package mathdots is only used if the command \\iddots is inserted into formulas")},
{"mathtools",
N_("Use mathtools package automatically"),
N_("Use mathtools package"),
N_("The LaTeX package mathtools is only used if some mathematical relations are inserted into formulas")},
{"mhchem",
N_("Use mhchem &package automatically"),
N_("Use mh&chem package"),

View File

@ -147,6 +147,8 @@ void InsetMathSplit::validate(LaTeXFeatures & features) const
if (name_ == "split" || name_ == "gathered" || name_ == "aligned" ||
name_ == "alignedat")
features.require("amsmath");
else if (name_ == "lgathered" || name_ == "rgathered")
features.require("mathtools");
InsetMathGrid::validate(features);
}

View File

@ -459,14 +459,15 @@ Preamble::Preamble() : one_language(true)
h_use_bibtopic = "false";
h_use_indices = "false";
h_use_geometry = "false";
h_use_amsmath = "1";
h_use_default_options = "false";
h_use_esint = "1";
h_use_hyperref = "0";
h_use_mhchem = "0";
h_use_mathdots = "0";
h_use_refstyle = "0";
h_use_undertilde = "0";
h_use_packages["amsmath"] = "1";
h_use_packages["esint"] = "1";
h_use_packages["mhchem"] = "0";
h_use_packages["mathdots"] = "0";
h_use_packages["mathtools"] = "0";
h_use_packages["undertilde"] = "0";
}
@ -646,19 +647,11 @@ void Preamble::handle_package(Parser &p, string const & name,
;
else if (name == "amsmath" || name == "amssymb")
h_use_amsmath = "2";
h_use_packages["amsmath"] = "2";
else if (name == "esint")
h_use_esint = "2";
else if (name == "mhchem")
h_use_mhchem = "2";
else if (name == "mathdots")
h_use_mathdots = "2";
else if (name == "undertilde")
h_use_undertilde = "2";
else if (name == "esint" || name == "mhchem" || name == "mathdots" ||
name == "mathtools" || name == "undertilde")
h_use_packages[name] = "2";
else if (name == "babel") {
h_language_package = "default";
@ -925,13 +918,11 @@ bool Preamble::writeLyXHeader(ostream & os, bool subdoc)
os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
}
os << "\\papersize " << h_papersize << "\n"
<< "\\use_geometry " << h_use_geometry << "\n"
<< "\\use_package amsmath " << h_use_amsmath << '\n'
<< "\\use_package esint " << h_use_esint << '\n'
<< "\\use_package mhchem " << h_use_mhchem << '\n'
<< "\\use_package mathdots " << h_use_mathdots << '\n'
<< "\\use_package undertilde " << h_use_undertilde << '\n'
<< "\\cite_engine " << h_cite_engine << "\n"
<< "\\use_geometry " << h_use_geometry << '\n';
for (map<string, string>::const_iterator it = h_use_packages.begin();
it != h_use_packages.end(); it++)
os << "\\use_package " << it->first << ' ' << it->second << '\n';
os << "\\cite_engine " << h_cite_engine << '\n'
<< "\\biblio_style " << h_biblio_style << "\n"
<< "\\use_bibtopic " << h_use_bibtopic << "\n"
<< "\\use_indices " << h_use_indices << "\n"

View File

@ -142,14 +142,10 @@ private:
std::string h_use_bibtopic;
std::string h_use_indices;
std::string h_use_geometry;
std::string h_use_amsmath;
std::map<std::string, std::string> h_use_packages;
std::string h_use_default_options;
std::string h_use_esint;
std::string h_use_hyperref;
std::string h_use_mhchem;
std::string h_use_mathdots;
std::string h_use_refstyle;
std::string h_use_undertilde;
/*!
* Add package \p name with options \p options to used_packages.

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 422 // baum : \\use_package
#define LYX_FORMAT_TEX2LYX 422
#define LYX_FORMAT_LYX 423 // baum : \\use_package mathtools
#define LYX_FORMAT_TEX2LYX 423
#if LYX_FORMAT_FOR_TEX2LYX != LYX_FORMAT_FOR_LYX
#warning "tex2lyx produces an out of date file format."