On 11/27/2015 10:01 PM, Uwe Stöhr wrote:
> Am 27.11.2015 um 23:06 schrieb Richard Heck:
>
>> The attached patch corrects an oversight in the lyx2lyx code for the
>> Solution and Solution* environments, namely, that the latter was not
>> handled. Someone please have a look. And Uwe, can you please test?
>
> Your patch is not applicable. You have unchanged code in your patch
> that does not exist in current git master.

Sorry, please try the attached.

> However, this change:
>
> -            add_to_preamble(document, "\\theoremstyle{plain}\n" \
> +            add_to_preamble(document, "\\theoremstyle{definition}\n" \
>
> Is incorrect. plain is correct for "theorems-std" and "theorems-ams"
> while for "theorems-bytype" and "theorems-ams-bytype" definition is
> correct.

Well, I was puzzled about this. The file theorems.inc (included in
theorems-std) has:

Style Solution
        CopyStyle             Definition
        LatexName             sol
        LabelString           "Solution \thetheorem."
        Preamble
                \theoremstyle{definition}
                \newtheorem{sol}[thm]{\protect\solutionname}
        EndPreamble
        LangPreamble
                \providecommand{\solutionname}{_(Solution)}
        EndLangPreamble
        BabelPreamble
               
\addto\captions$$lang{\renewcommand{\solutionname}{_(Solution)}}
        EndBabelPreamble
End

Same for theorems-ams.inc. I was just trying to make lyx2lyx do the same
as what the modules do. Or are they wrong now?

Richard

>From 614ba75924c9759eadacba5370a69934cbc3684c Mon Sep 17 00:00:00 2001
From: Richard Heck <rgh...@lyx.org>
Date: Fri, 27 Nov 2015 16:42:22 -0500
Subject: [PATCH 1/2] Clean up reversion code for new Solution environment.

---
 lib/lyx2lyx/lyx_2_2.py | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/lib/lyx2lyx/lyx_2_2.py b/lib/lyx2lyx/lyx_2_2.py
index b9ea260..3be66f4 100644
--- a/lib/lyx2lyx/lyx_2_2.py
+++ b/lib/lyx2lyx/lyx_2_2.py
@@ -2121,39 +2121,51 @@ def revert_solution(document):
         if mod == "theorems-std" or mod == "theorems-bytype" \
         or mod == "theorems-ams" or mod == "theorems-ams-bytype":
             have_mod = True
-            continue
+            break
     if not have_mod:
         return
+
     consecutive = False
+    need_preamble_code = False
     i = 0
     while True:
         i = find_token(document.body, "\\begin_layout Solution", i)
         if i == -1:
-            return
+            break
         j = find_end_of_layout(document.body, i)
         if j == -1:
             document.warning("Malformed LyX document: Can't find end of Solution layout")
             i += 1
             continue
+
+        need_preamble_code = True
         # if this is not a consecutive env, add start command
         begcmd = []
         if not consecutive:
             begcmd = put_cmd_in_ert("\\begin{sol}")
+
         # has this a consecutive theorem of same type?
-        consecutive = False
         consecutive = document.body[j + 2] == "\\begin_layout Solution"
         # if this is not followed by a consecutive env, add end command
+        endcmd = []
         if not consecutive:
-            document.body[j : j + 1] = put_cmd_in_ert("\\end{sol}") + ["\\end_layout"]
+            endcmd = put_cmd_in_ert("\\end{sol}") + ["\\end_layout"]
+            document.body[j : j + 1] = endcmd
+
         document.body[i : i + 1] = ["\\begin_layout Standard", ""] + begcmd
+        i = j + 1 + len(begcmd)
+        if endcmd:
+            i += len(endcmd) - 1
+        document.warning(document.body[i])
+
+    if need_preamble_code:
         add_to_preamble(document, "\\providecommand{\solutionname}{Solution}")
         if mod == "theorems-std" or mod == "theorems-ams":
             add_to_preamble(document, "\\theoremstyle{plain}\n" \
-                                      "\\newtheorem{sol}[thm]{\\protect\\solutionname}")
+                                    "\\newtheorem{sol}[thm]{\\protect\\solutionname}")
         if mod == "theorems-bytype" or mod == "theorems-ams-bytype":
             add_to_preamble(document, "\\theoremstyle{definition}\n" \
-                                      "\\newtheorem{sol}{\\protect\\solutionname}")
-        i = j
+                                    "\\newtheorem{sol}{\\protect\\solutionname}")
 
 
 def revert_verbatim_star(document):
-- 
2.1.0

>From 005fbf49f841d877391f90a4e324c83b36060ca3 Mon Sep 17 00:00:00 2001
From: Richard Heck <rgh...@lyx.org>
Date: Fri, 27 Nov 2015 17:02:53 -0500
Subject: [PATCH 2/2] Handle Solution* environment in lyx2lyx.

---
 lib/lyx2lyx/lyx_2_2.py | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

diff --git a/lib/lyx2lyx/lyx_2_2.py b/lib/lyx2lyx/lyx_2_2.py
index 3be66f4..ec7b0dc 100644
--- a/lib/lyx2lyx/lyx_2_2.py
+++ b/lib/lyx2lyx/lyx_2_2.py
@@ -2112,9 +2112,9 @@ def revert_fontsettings(document):
 
 
 def revert_solution(document):
-    " Reverts the solution environmen of the theorem module to TeX code "
+    " Reverts the solution environment of the theorem module to TeX code "
 
-    # Do we use theorems-std module?
+    # Do we use one of the modules that provides Solution?
     have_mod = False
     mods = document.get_module_list()
     for mod in mods:
@@ -2127,6 +2127,7 @@ def revert_solution(document):
 
     consecutive = False
     need_preamble_code = False
+    need_starred_code  = False
     i = 0
     while True:
         i = find_token(document.body, "\\begin_layout Solution", i)
@@ -2138,34 +2139,49 @@ def revert_solution(document):
             i += 1
             continue
 
-        need_preamble_code = True
+        is_starred = document.body[i].startswith("\\begin_layout Solution*")
+        if is_starred:
+            latexcmd = "sol*"
+            need_starred_code = True
+        else:
+            latexcmd = "sol"
+            need_preamble_code = True
+
         # if this is not a consecutive env, add start command
         begcmd = []
         if not consecutive:
-            begcmd = put_cmd_in_ert("\\begin{sol}")
+            begcmd = put_cmd_in_ert("\\begin{%s}" % (latexcmd))
 
         # has this a consecutive theorem of same type?
-        consecutive = document.body[j + 2] == "\\begin_layout Solution"
+        consecutive = document.body[j + 2] == document.body[i]
         # if this is not followed by a consecutive env, add end command
         endcmd = []
         if not consecutive:
-            endcmd = put_cmd_in_ert("\\end{sol}") + ["\\end_layout"]
+            endcmd = put_cmd_in_ert("\\end{%s}" % (latexcmd)) + ["\\end_layout"]
             document.body[j : j + 1] = endcmd
 
         document.body[i : i + 1] = ["\\begin_layout Standard", ""] + begcmd
         i = j + 1 + len(begcmd)
         if endcmd:
             i += len(endcmd) - 1
-        document.warning(document.body[i])
 
-    if need_preamble_code:
+    if need_preamble_code or need_starred_code:
         add_to_preamble(document, "\\providecommand{\solutionname}{Solution}")
+
+    if need_preamble_code:
         if mod == "theorems-std" or mod == "theorems-ams":
-            add_to_preamble(document, "\\theoremstyle{plain}\n" \
+            add_to_preamble(document, "\\theoremstyle{definition}\n" \
                                     "\\newtheorem{sol}[thm]{\\protect\\solutionname}")
         if mod == "theorems-bytype" or mod == "theorems-ams-bytype":
             add_to_preamble(document, "\\theoremstyle{definition}\n" \
                                     "\\newtheorem{sol}{\\protect\\solutionname}")
+    if need_starred_code:
+        if mod == "theorems-std" or mod == "theorems-ams":
+            add_to_preamble(document, "\\theoremstyle{definition}\n" \
+                                    "\\newtheorem{sol*}[thm]{\\protect\\solutionname}")
+        if mod == "theorems-bytype" or mod == "theorems-ams-bytype":
+            add_to_preamble(document, "\\theoremstyle{definition}\n" \
+                                    "\\newtheorem{sol*}{\\protect\\solutionname}")
 
 
 def revert_verbatim_star(document):
-- 
2.1.0

Reply via email to