]> git.lyx.org Git - features.git/commitdiff
Improve texl2yx refstyle support
authorGeorg Baum <baum@lyx.org>
Fri, 22 Feb 2013 20:59:23 +0000 (21:59 +0100)
committerGeorg Baum <baum@lyx.org>
Fri, 22 Feb 2013 20:59:23 +0000 (21:59 +0100)
- Convert prettyref to the autopackage mechanism
- Do not load refstyle automatically if some refstyle preamble code of LyX
  is found, since LyX will only load the package if an actual reference
  command is used. This is needed for mixed refstyle/prettyref documents.
- Only recognize refstyle commands if refstyle was detected in the preamble
- Only recognize prettyref commands if prettyref was detected in the preamble
- Add a mixed refstyle/prettyref test case

src/tex2lyx/Preamble.cpp
src/tex2lyx/Preamble.h
src/tex2lyx/test/test-insets.lyx.lyx
src/tex2lyx/test/test-insets.tex
src/tex2lyx/text.cpp

index 9d72a5e77bfeed4dd4397e0608598a745c9df24e..72800eca9579739082531a6ab3870cbfd5f06a40 100644 (file)
@@ -181,9 +181,9 @@ const char * const known_xetex_packages[] = {"arabxetex", "fixlatvian",
 const char * const known_lyx_packages[] = {"amsbsy", "amsmath", "amssymb",
 "amstext", "amsthm", "array", "babel", "booktabs", "calc", "CJK", "color",
 "float", "fontspec", "graphicx", "hhline", "ifthen", "longtable", "makeidx",
-"multirow", "nomencl", "pdfpages", "refstyle", "rotating", "rotfloat", "splitidx",
-"setspace", "subscript", "textcomp", "tipa", "tipx", "tone", "ulem", "url",
-"varioref", "verbatim", "wrapfig", "xunicode", 0};
+"multirow", "nomencl", "pdfpages", "prettyref", "refstyle", "rotating",
+"rotfloat", "splitidx", "setspace", "subscript", "textcomp", "tipa", "tipx",
+"tone", "ulem", "url", "varioref", "verbatim", "wrapfig", "xunicode", 0};
 
 // used for the handling of \newindex
 int index_number = 0;
@@ -522,7 +522,7 @@ Preamble::Preamble() : one_language(true), explicit_babel(false),
        h_use_geometry            = "false";
        h_use_default_options     = "false";
        h_use_hyperref            = "false";
-       h_use_refstyle            = "0";
+       h_use_refstyle            = false;
        h_use_packages["amsmath"]    = "1";
        h_use_packages["amssymb"]    = "0";
        h_use_packages["esint"]      = "1";
@@ -897,9 +897,6 @@ void Preamble::handle_package(Parser &p, string const & name,
                h_language_package = "\\usepackage{" + name + "}";
        }
 
-       else if (name == "prettyref")
-               ; // ignore this FIXME: Use the package separator mechanism instead
-
        else if (name == "lyxskak") {
                // ignore this and its options
                const char * const o[] = {"ps", "mover", 0};
@@ -910,7 +907,9 @@ void Preamble::handle_package(Parser &p, string const & name,
                if (name == "splitidx")
                        h_use_indices = "true";
                if (name == "refstyle")
-                       h_use_refstyle = "1";
+                       h_use_refstyle = true;
+               else if (name == "prettyref")
+                       h_use_refstyle = false;
                if (!in_lyx_preamble) {
                        h_preamble << package_beg_sep << name
                                   << package_mid_sep << "\\usepackage{"
@@ -1417,7 +1416,6 @@ void Preamble::parse(Parser & p, string const & forceclass,
                                || name == "thmref"
                                || name == "lemref") {
                                p.skip_spaces();
-                               preamble.registerAutomaticallyLoadedPackage("refstyle");
                                in_lyx_preamble = true;
                        }
                        // only non-lyxspecific stuff
@@ -1453,7 +1451,6 @@ void Preamble::parse(Parser & p, string const & forceclass,
                                || name == "\\providecommand\\corref[1]{\\ref{cor:#1}}"
                                || name == "\\providecommand\\propref[1]{\\ref{prop:#1}}") {
                                p.skip_spaces();
-                               preamble.registerAutomaticallyLoadedPackage("refstyle");
                                in_lyx_preamble = true;
                        }
                        // only non-lyxspecific stuff
index ecb0be17189dd8d38cc341a5eebc130a5e0a5ebb..3216c3cc6d697edab7c766bed8a293903af427cb 100644 (file)
@@ -39,7 +39,7 @@ public:
        ///
        std::string notefontcolor() const { return h_notefontcolor; }
        ///
-       std::string refstyle() const { return h_use_refstyle; }
+       bool refstyle() const { return h_use_refstyle; }
        ///
        bool fontCJKSet() const { return h_font_cjk_set; }
        ///
@@ -192,7 +192,7 @@ private:
        std::map<std::string, std::string> h_use_packages;
        std::string h_use_default_options;
        std::string h_use_hyperref;
-       std::string h_use_refstyle;
+       bool h_use_refstyle;
 
        /*!
         * Add package \p name with options \p options to used_packages.
index d0d2da944f339bd3d1b7c8c993491b6cfac25119..3d58cd745523037a54dda9ee52633911d8770010 100644 (file)
@@ -5,6 +5,11 @@
 \textclass article
 \begin_preamble
 
+% Load refstyle before prettyref so that prettyref wins.
+% The real refstyle tests are in test-refstyle-theorems.tex.
+\usepackage{refstyle}
+
+
 \def\mycommand{\textquestiondown}
 
 
@@ -245,7 +250,49 @@ reference "lab:test"
 
 \end_inset
 
-.
+. The command 
+\begin_inset ERT
+status collapsed
+
+\begin_layout Plain Layout
+
+\backslash
+secref
+\end_layout
+
+\end_inset
+
+
+\begin_inset ERT
+status collapsed
+
+\begin_layout Plain Layout
+{
+\end_layout
+
+\end_inset
+
+lab:test
+\begin_inset ERT
+status collapsed
+
+\begin_layout Plain Layout
+}
+\end_layout
+
+\end_inset
+
+ will be parsed in ERT, since 
+\begin_inset ERT
+status collapsed
+
+\begin_layout Plain Layout
+LyX
+\end_layout
+
+\end_inset
+
+ does not support refstyle and prettyref natively at the same time.
 \end_layout
 
 \begin_layout Section
index 28d43011258cffc522dabae9d6d7d0b057d53a41..0feb452c06cf961b5a3c4e2e2fedd130d61b5325 100644 (file)
@@ -18,7 +18,6 @@
 \providecommand{\makenomenclature}{\makeglossary}
 \makenomenclature
 \usepackage{varioref}
-\usepackage{prettyref}
 \usepackage{splitidx}
 \makeindex
 \newindex[Index]{idx}
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
 
+% Load refstyle before prettyref so that prettyref wins.
+% The real refstyle tests are in test-refstyle-theorems.tex.
+\usepackage{refstyle}
+\AtBeginDocument{\providecommand\secref[1]{\ref{sec:#1}}}
+\usepackage{prettyref}
+
 \def\mycommand{\textquestiondown}
 
 \begin{document}
@@ -82,6 +87,8 @@ reference~\pageref{lab:test}, but also equation
 reference~\eqref{lab:test} (from amsmath package), or varioref's
 equivalents~\vref{lab:test} and~\vpageref{lab:test}, without
 forgetting pretty references like~\prettyref{lab:test}.
+The command \secref{lab:test} will be parsed in ERT, since LyX
+does not support refstyle and prettyref natively at the same time.
 
 \section{Cites\index{Cites}}
 
index d3ceb58ffdb369f294e1e854e63ca9d5e62e561a..2a988bcb6b7c7226dda620dca61079177a6ea114 100644 (file)
@@ -3407,40 +3407,13 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        skip_spaces_braces(p);
                }
 
-               else if ((where = is_known(t.cs(), known_ref_commands))) {
-                       // \eqref can also occur if refstyle is used
-                       if (t.cs() == "eqref" && preamble.refstyle() == "1") {
-                               context.check_layout(os);
-                               begin_command_inset(os, "ref", "formatted");
-                               os << "reference \"eq:"
-                                  << convert_command_inset_arg(p.verbatim_item())
-                                  << "\"\n";
-                               end_inset(os);
-                               preamble.registerAutomaticallyLoadedPackage("refstyle");
-                       } else {
-                               string const opt = p.getOpt();
-                               if (opt.empty()) {
-                                       context.check_layout(os);
-                                       begin_command_inset(os, "ref",
-                                               known_coded_ref_commands[where - known_ref_commands]);
-                                       os << "reference \""
-                                          << convert_command_inset_arg(p.verbatim_item())
-                                          << "\"\n";
-                                       end_inset(os);
-                                       if (t.cs() == "vref" || t.cs() == "vpageref")
-                                               preamble.registerAutomaticallyLoadedPackage("varioref");
-                               } else {
-                                       // LyX does not yet support optional arguments of ref commands
-                                       output_ert_inset(os, t.asInput() + '[' + opt + "]{" +
-                                              p.verbatim_item() + "}", context);
-                               }
-                       }
-               }
-
-               else if ((where = is_known(t.cs(), known_refstyle_commands))) {
+               // handle refstyle first to catch \eqref which can also occur
+               // without refstyle. Only recognize these commands if
+               // refstyle.sty was found in the preamble (otherwise \eqref
+               // and user defined ref commands could be misdetected).
+               else if ((where = is_known(t.cs(), known_refstyle_commands)) &&
+                        preamble.refstyle()) {
                        context.check_layout(os);
-                       // \eqref can also occur if refstyle is not used
-                       // this case is already handled in the previous else if
                        begin_command_inset(os, "ref", "formatted");
                        os << "reference \"";
                        os << known_refstyle_prefixes[where - known_refstyle_commands]
@@ -3451,6 +3424,30 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        preamble.registerAutomaticallyLoadedPackage("refstyle");
                }
 
+               // if refstyle is used, we must not convert \prettyref to a
+               // formatted reference, since that would result in a refstyle command.
+               else if ((where = is_known(t.cs(), known_ref_commands)) &&
+                        (t.cs() != "prettyref" || !preamble.refstyle())) {
+                       string const opt = p.getOpt();
+                       if (opt.empty()) {
+                               context.check_layout(os);
+                               begin_command_inset(os, "ref",
+                                       known_coded_ref_commands[where - known_ref_commands]);
+                               os << "reference \""
+                                  << convert_command_inset_arg(p.verbatim_item())
+                                  << "\"\n";
+                               end_inset(os);
+                               if (t.cs() == "vref" || t.cs() == "vpageref")
+                                       preamble.registerAutomaticallyLoadedPackage("varioref");
+                               else if (t.cs() == "prettyref")
+                                       preamble.registerAutomaticallyLoadedPackage("prettyref");
+                       } else {
+                               // LyX does not yet support optional arguments of ref commands
+                               output_ert_inset(os, t.asInput() + '[' + opt + "]{" +
+                                      p.verbatim_item() + '}', context);
+                       }
+               }
+
                else if (use_natbib &&
                         is_known(t.cs(), known_natbib_commands) &&
                         ((t.cs() != "citefullauthor" &&