]> git.lyx.org Git - lyx.git/blobdiff - src/LaTeXFeatures.cpp
Use the right latex backend when a converter needs aux files.
[lyx.git] / src / LaTeXFeatures.cpp
index 6f8ce5c9caf35315dc1ff1ac62072248355a73a7..d5a0b6f6b90043b7015f37d64dad51804ea5071b 100644 (file)
@@ -38,6 +38,7 @@
 #include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
+#include "support/regex.h"
 
 #include <algorithm>
 
@@ -575,6 +576,8 @@ char const * simplefeatures[] = {
        "mathdesign",
        "mathrsfs",
        "mathabx",
+       "mathtools",
+       "cancel",
        "ascii",
        "url",
        "covington",
@@ -690,7 +693,7 @@ string const LaTeXFeatures::getPackages() const
                packages << amsPackages;
 
        // fixltx2e must be loaded after amsthm, since amsthm produces an error with
-       // the redefined \[ command (bug 7233). Load is as early as possible, since
+       // the redefined \[ command (bug 7233). Load it as early as possible, since
        // other packages might profit from it.
        if (mustProvide("fixltx2e"))
                packages << "\\usepackage{fixltx2e}\n";
@@ -704,24 +707,27 @@ string const LaTeXFeatures::getPackages() const
        // integral symbols from wasysym and amsmath.
        // See http://www.lyx.org/trac/ticket/1942
        if (mustProvide("wasysym") &&
-           (params_.use_esint != BufferParams::package_off || !isRequired("esint")))
+           params_.use_package("wasysym") != BufferParams::package_off &&
+           (params_.use_package("esint") != BufferParams::package_off || !isRequired("esint")))
                packages << "\\usepackage{wasysym}\n";
 
        // accents must be loaded after amsmath
-       if (mustProvide("accents"))
+       if (mustProvide("accents") &&
+           params_.use_package("accents") != BufferParams::package_off)
                packages << "\\usepackage{accents}\n";
 
        // mathdots must be loaded after amsmath
        if (mustProvide("mathdots") &&
-               params_.use_mathdots != BufferParams::package_off)
+               params_.use_package("mathdots") != BufferParams::package_off)
                packages << "\\usepackage{mathdots}\n";
 
        // yhmath must be loaded after amsmath
-       if (mustProvide("yhmath"))
+       if (mustProvide("yhmath") &&
+           params_.use_package("yhmath") != BufferParams::package_off)
                packages << "\\usepackage{yhmath}\n";
 
        if (mustProvide("undertilde") &&
-               params_.use_undertilde != BufferParams::package_off)
+               params_.use_package("undertilde") != BufferParams::package_off)
                packages << "\\usepackage{undertilde}\n";
 
        // [x]color and pdfcolmk are handled in getColorOptions() above
@@ -756,7 +762,7 @@ string const LaTeXFeatures::getPackages() const
        // esint must be after amsmath and wasysym, since it will redeclare
        // inconsistent integral symbols
        if (mustProvide("esint") &&
-           params_.use_esint != BufferParams::package_off)
+           params_.use_package("esint") != BufferParams::package_off)
                packages << "\\usepackage{esint}\n";
 
        // natbib.sty
@@ -765,7 +771,7 @@ string const LaTeXFeatures::getPackages() const
        // This special case is indicated by the "natbib-internal" key.
        if (mustProvide("natbib") && !tclass.provides("natbib-internal")) {
                packages << "\\usepackage[";
-               if (params_.citeEngine() == ENGINE_NATBIB_NUMERICAL)
+               if (params_.citeEngineType() == ENGINE_TYPE_NUMERICAL)
                        packages << "numbers";
                else
                        packages << "authoryear";
@@ -795,9 +801,8 @@ string const LaTeXFeatures::getPackages() const
                packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
                            "\\usepackage{ulem}\n";
 
-       if (params_.use_mhchem == BufferParams::package_on ||
-           (mustProvide("mhchem") &&
-            params_.use_mhchem != BufferParams::package_off))
+       if (mustProvide("mhchem") &&
+           params_.use_package("mhchem") != BufferParams::package_off)
                packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
                            "\\usepackage{mhchem}\n";
 
@@ -1050,7 +1055,7 @@ string const LaTeXFeatures::loadAMSPackages() const
                tmp << "\\usepackage{amsthm}\n";
 
        if (mustProvide("amsmath")
-           && params_.use_amsmath != BufferParams::package_off) {
+           && params_.use_package("amsmath") != BufferParams::package_off) {
                tmp << "\\usepackage{amsmath}\n";
        } else {
                // amsbsy and amstext are already provided by amsmath
@@ -1061,7 +1066,7 @@ string const LaTeXFeatures::loadAMSPackages() const
        }
 
        if (mustProvide("amssymb")
-           || params_.use_amsmath == BufferParams::package_on)
+           || params_.use_package("amsmath") == BufferParams::package_on)
                tmp << "\\usepackage{amssymb}\n";
 
        return tmp.str();
@@ -1152,6 +1157,15 @@ docstring const getFloatI18nPreamble(docstring const & type,
                        docstring const & name, Language const * lang,
                        Encoding const & enc, bool const polyglossia)
 {
+       // Check whether name can be encoded in the buffer encoding
+       bool encodable = true;
+       for (size_t i = 0; i < name.size(); ++i) {
+               if (!enc.encodable(name[i])) {
+                       encodable = false;
+                       break;
+               }
+       }
+
        docstring const language = polyglossia ? from_ascii(lang->polyglossia())
                                               : from_ascii(lang->babel());
        docstring const langenc = from_ascii(lang->encoding()->iconvName());
@@ -1159,7 +1173,7 @@ docstring const getFloatI18nPreamble(docstring const & type,
        docstring const bufenc = from_ascii(enc.iconvName());
        docstring const s1 = docstring(1, 0xF0000);
        docstring const s2 = docstring(1, 0xF0001);
-       docstring const translated = (langenc == bufenc) ? name
+       docstring const translated = encodable ? name
                : from_ascii("\\inputencoding{") + texenc + from_ascii("}")
                        + s1 + langenc + s2 + name + s1 + bufenc + s2;
 
@@ -1168,6 +1182,49 @@ docstring const getFloatI18nPreamble(docstring const & type,
           << "{\\renewcommand{\\" << type << "name}{" << translated << "}}\n";
        return os.str();
 }
+
+
+docstring const i18npreamble(docstring const & templ, Language const * lang,
+               Encoding const & enc, bool const polyglossia)
+{
+       if (templ.empty())
+               return templ;
+
+       string preamble = polyglossia ?
+               subst(to_utf8(templ), "$$lang", lang->polyglossia()) :
+               subst(to_utf8(templ), "$$lang", lang->babel());
+
+       string const langenc = lang->encoding()->iconvName();
+       string const texenc = lang->encoding()->latexName();
+       string const bufenc = enc.iconvName();
+       // First and second character of plane 15 (Private Use Area)
+       string const s1 = "\xf3\xb0\x80\x80"; // U+F0000
+       string const s2 = "\xf3\xb0\x80\x81"; // U+F0001
+       // FIXME UNICODE
+       // lyx::regex is not unicode-safe.
+       // Should use QRegExp or (boost::u32regex, but that requires ICU)
+       static regex const reg("_\\(([^\\)]+)\\)");
+       smatch sub;
+       while (regex_search(preamble, sub, reg)) {
+               string const key = sub.str(1);
+               docstring const name = lang->translateLayout(key);
+               // Check whether name can be encoded in the buffer encoding
+               bool encodable = true;
+               for (size_t i = 0; i < name.size(); ++i) {
+                       if (!enc.encodable(name[i])) {
+                               encodable = false;
+                               break;
+                       }
+               }
+               string const translated = encodable ? to_utf8(name)
+                       : "\\inputencoding{" + texenc + "}"
+                       + s1 + langenc + s2 + to_utf8(name)
+                       + s1 + bufenc + s2;
+               preamble = subst(preamble, sub.str(), translated);
+       }
+       return from_utf8(preamble);
+}
+
 }
 
 
@@ -1184,17 +1241,20 @@ docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel, bool use_po
        list<docstring>::const_iterator end = usedLayouts_.end();
        for (; cit != end; ++cit) {
                // language dependent commands (once per document)
-               snippets.insert(tclass[*cit].langpreamble(buffer().language(),
+               snippets.insert(i18npreamble(tclass[*cit].langpreamble(),
+                                               buffer().language(),
                                                buffer().params().encoding(),
                                                use_polyglossia));
                // commands for language changing (for multilanguage documents)
                if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
-                       snippets.insert(tclass[*cit].babelpreamble(
+                       snippets.insert(i18npreamble(
+                                               tclass[*cit].babelpreamble(),
                                                buffer().language(),
                                                buffer().params().encoding(),
                                                use_polyglossia));
                        for (lang_it lit = lbeg; lit != lend; ++lit)
-                               snippets.insert(tclass[*cit].babelpreamble(
+                               snippets.insert(i18npreamble(
+                                               tclass[*cit].babelpreamble(),
                                                *lit,
                                                buffer().params().encoding(),
                                                use_polyglossia));
@@ -1238,6 +1298,34 @@ docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel, bool use_po
                }
        }
 
+       cit = usedInsetLayouts_.begin();
+       end = usedInsetLayouts_.end();
+       TextClass::InsetLayouts const & ils = tclass.insetLayouts();
+       for (; cit != end; ++cit) {
+               TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
+               if (it == ils.end())
+                       continue;
+               // language dependent commands (once per document)
+               snippets.insert(i18npreamble(it->second.langpreamble(),
+                                               buffer().language(),
+                                               buffer().params().encoding(),
+                                               use_polyglossia));
+               // commands for language changing (for multilanguage documents)
+               if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
+                       snippets.insert(i18npreamble(
+                                               it->second.babelpreamble(),
+                                               buffer().language(),
+                                               buffer().params().encoding(),
+                                               use_polyglossia));
+                       for (lang_it lit = lbeg; lit != lend; ++lit)
+                               snippets.insert(i18npreamble(
+                                               it->second.babelpreamble(),
+                                               *lit,
+                                               buffer().params().encoding(),
+                                               use_polyglossia));
+               }
+       }
+
        odocstringstream tcpreamble;
        set<docstring>::const_iterator const send = snippets.end();
        set<docstring>::const_iterator it = snippets.begin();