From e1be934dc2ff061e5200017d6503280686983e1f Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sun, 16 Sep 2012 22:24:55 -0700
Subject: [PATCH] LaTeX writer:  Don't rely on the enumerate package.

Instead, use standard LaTeX commands to change numbering style.
---
 src/Text/Pandoc/Writers/LaTeX.hs | 44 ++++++++++++++----------
 templates                        |  2 +-
 tests/writer.latex               | 58 +++++++++++++++++++-------------
 3 files changed, 62 insertions(+), 42 deletions(-)

diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs
index 54c112b09..4929bb2a9 100644
--- a/src/Text/Pandoc/Writers/LaTeX.hs
+++ b/src/Text/Pandoc/Writers/LaTeX.hs
@@ -53,7 +53,6 @@ data WriterState =
               , stOLLevel       :: Int           -- level of ordered list nesting
               , stOptions       :: WriterOptions -- writer options, so they don't have to be parameter
               , stVerbInNote    :: Bool          -- true if document has verbatim text in note
-              , stEnumerate     :: Bool          -- true if document needs fancy enumerated lists
               , stTable         :: Bool          -- true if document has a table
               , stStrikeout     :: Bool          -- true if document has strikeout
               , stUrl           :: Bool          -- true if document has visible URL link
@@ -73,7 +72,7 @@ writeLaTeX options document =
   evalState (pandocToLaTeX options document) $
   WriterState { stInNote = False, stInTable = False,
                 stTableNotes = [], stOLLevel = 1, stOptions = options,
-                stVerbInNote = False, stEnumerate = False,
+                stVerbInNote = False,
                 stTable = False, stStrikeout = False,
                 stUrl = False, stGraphics = False,
                 stLHS = False, stBook = writerChapters options,
@@ -144,7 +143,6 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
                                                 else "article") ] ++
                  [ ("author", a) | a <- authorsText ] ++
                  [ ("verbatim-in-note", "yes") | stVerbInNote st ] ++
-                 [ ("fancy-enums", "yes") | stEnumerate st ] ++
                  [ ("tables", "yes") | stTable st ] ++
                  [ ("strikeout", "yes") | stStrikeout st ] ++
                  [ ("url", "yes") | stUrl st ] ++
@@ -354,20 +352,32 @@ blockToLaTeX (OrderedList (start, numstyle, numdelim) lst) = do
   put $ st {stOLLevel = oldlevel + 1}
   items <- mapM listItemToLaTeX lst
   modify (\s -> s {stOLLevel = oldlevel})
-  exemplar <- if numstyle /= DefaultStyle || numdelim /= DefaultDelim
-                 then do
-                   modify $ \s -> s{ stEnumerate = True }
-                   return $ char '[' <>
-                       text (head (orderedListMarkers (1, numstyle,
-                            numdelim))) <> char ']'
-                 else return empty
-  let resetcounter = if start /= 1 && oldlevel <= 4
-                        then text $ "\\setcounter{enum" ++
-                             map toLower (toRomanNumeral oldlevel) ++
-                             "}{" ++ show (start - 1) ++ "}"
-                        else empty
-  return $ text ("\\begin{enumerate}" ++ inc) <> exemplar $$ resetcounter $$
-           vcat items $$ "\\end{enumerate}"
+  let tostyle x = case numstyle of
+                       Decimal     -> "\\arabic" <> braces x
+                       UpperRoman  -> "\\Roman" <> braces x
+                       LowerRoman  -> "\\roman" <> braces x
+                       UpperAlpha  -> "\\Alph" <> braces x
+                       LowerAlpha  -> "\\alph" <> braces x
+                       _           -> x
+  let todelim x = case numdelim of
+                       OneParen    -> x <> ")"
+                       TwoParens   -> parens x
+                       Period      -> x <> "."
+                       _           -> x <> "."
+  let enum = text $ "enum" ++ map toLower (toRomanNumeral oldlevel)
+  let stylecommand = if numstyle == DefaultStyle && numdelim == DefaultDelim
+                        then empty
+                        else "\\def" <> "\\label" <> enum <>
+                              braces (todelim $ tostyle enum)
+  let resetcounter = if start == 1 || oldlevel > 4
+                        then empty
+                        else "\\setcounter" <> braces enum <>
+                              braces (text $ show $ start - 1)
+  return $ text ("\\begin{enumerate}" ++ inc)
+         $$ stylecommand
+         $$ resetcounter
+         $$ vcat items
+         $$ "\\end{enumerate}"
 blockToLaTeX (DefinitionList lst) = do
   incremental <- gets stIncremental
   let inc = if incremental then "[<+->]" else ""
diff --git a/templates b/templates
index 22628ea9a..6d4f0a78a 160000
--- a/templates
+++ b/templates
@@ -1 +1 @@
-Subproject commit 22628ea9aa4e65ac11d489f22be5c765b5107604
+Subproject commit 6d4f0a78a0ed3cfdb98a517a33c7e275db5aa205
diff --git a/tests/writer.latex b/tests/writer.latex
index c3dbcb3fd..7e885400c 100644
--- a/tests/writer.latex
+++ b/tests/writer.latex
@@ -17,13 +17,6 @@
   \newcommand{\euro}{€}
 \fi
 \usepackage{fancyvrb}
-% Redefine labelwidth for lists; otherwise, the enumerate package will cause
-% markers to extend beyond the left margin.
-\makeatletter\AtBeginDocument{%
-  \renewcommand{\@listi}
-    {\setlength{\labelwidth}{4em}}
-}\makeatother
-\usepackage{enumerate}
 \usepackage{graphicx}
 % We will generate all images so they have a width \maxwidth. This means
 % that they will get their normal width if they fit onto the page, but
@@ -127,7 +120,8 @@ sub status {
 
 A list:
 
-\begin{enumerate}[1.]
+\begin{enumerate}
+\def\labelenumi{\arabic{enumi}.}
 \item
   item one
 \item
@@ -249,7 +243,8 @@ Minuses loose:
 
 Tight:
 
-\begin{enumerate}[1.]
+\begin{enumerate}
+\def\labelenumi{\arabic{enumi}.}
 \item
   First
 \item
@@ -260,7 +255,8 @@ Tight:
 
 and:
 
-\begin{enumerate}[1.]
+\begin{enumerate}
+\def\labelenumi{\arabic{enumi}.}
 \item
   One
 \item
@@ -271,7 +267,8 @@ and:
 
 Loose using tabs:
 
-\begin{enumerate}[1.]
+\begin{enumerate}
+\def\labelenumi{\arabic{enumi}.}
 \item
   First
 \item
@@ -282,7 +279,8 @@ Loose using tabs:
 
 and using spaces:
 
-\begin{enumerate}[1.]
+\begin{enumerate}
+\def\labelenumi{\arabic{enumi}.}
 \item
   One
 \item
@@ -293,7 +291,8 @@ and using spaces:
 
 Multiple paragraphs:
 
-\begin{enumerate}[1.]
+\begin{enumerate}
+\def\labelenumi{\arabic{enumi}.}
 \item
   Item 1, graf one.
 
@@ -323,7 +322,8 @@ Multiple paragraphs:
 
 Here's another:
 
-\begin{enumerate}[1.]
+\begin{enumerate}
+\def\labelenumi{\arabic{enumi}.}
 \item
   First
 \item
@@ -343,7 +343,8 @@ Here's another:
 
 Same thing but with paragraphs:
 
-\begin{enumerate}[1.]
+\begin{enumerate}
+\def\labelenumi{\arabic{enumi}.}
 \item
   First
 \item
@@ -379,7 +380,8 @@ Same thing but with paragraphs:
 
 \subsection{Fancy list markers}
 
-\begin{enumerate}[(1)]
+\begin{enumerate}
+\def\labelenumi{(\arabic{enumi})}
 \setcounter{enumi}{1}
 \item
   begins with 2
@@ -388,14 +390,16 @@ Same thing but with paragraphs:
 
   with a continuation
 
-  \begin{enumerate}[i.]
+  \begin{enumerate}
+  \def\labelenumii{\roman{enumii}.}
   \setcounter{enumii}{3}
   \item
     sublist with roman numerals, starting with 4
   \item
     more items
 
-    \begin{enumerate}[(A)]
+    \begin{enumerate}
+    \def\labelenumiii{(\Alph{enumiii})}
     \item
       a subsublist
     \item
@@ -406,20 +410,24 @@ Same thing but with paragraphs:
 
 Nesting:
 
-\begin{enumerate}[A.]
+\begin{enumerate}
+\def\labelenumi{\Alph{enumi}.}
 \item
   Upper Alpha
 
-  \begin{enumerate}[I.]
+  \begin{enumerate}
+  \def\labelenumii{\Roman{enumii}.}
   \item
     Upper Roman.
 
-    \begin{enumerate}[(1)]
+    \begin{enumerate}
+    \def\labelenumiii{(\arabic{enumiii})}
     \setcounter{enumiii}{5}
     \item
       Decimal start with 6
 
-      \begin{enumerate}[a)]
+      \begin{enumerate}
+      \def\labelenumiv{\alph{enumiv})}
       \setcounter{enumiv}{2}
       \item
         Lower alpha with paren
@@ -540,7 +548,8 @@ computer
 \item[orange]
 orange fruit
 
-\begin{enumerate}[1.]
+\begin{enumerate}
+\def\labelenumi{\arabic{enumi}.}
 \item
   sublist
 \item
@@ -900,7 +909,8 @@ note.\footnote{This is \emph{easier} to type. Inline notes may contain
 Notes can go in quotes.\footnote{In quote.}
 \end{quote}
 
-\begin{enumerate}[1.]
+\begin{enumerate}
+\def\labelenumi{\arabic{enumi}.}
 \item
   And in list items.\footnote{In list.}
 \end{enumerate}