git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer/template: better handling of front/main/backmatter.
ebdcf75John MacFarlane
data/templates/default.latex+5−5
@@ -383,10 +383,10 @@ $endif$
383$endif$
383$endif$
384
384
385\begin{document}
385\begin{document}
386−$if(title)$
386+$if(has-frontmatter)$
387−$if(book-class)$
388\frontmatter
387\frontmatter
389$endif$
388$endif$
389+$if(title)$
390$if(beamer)$
390$if(beamer)$
391\frame{\titlepage}
391\frame{\titlepage}
392$else$
392$else$
@@ -430,18 +430,18 @@ $endif$
430$if(lof)$
430$if(lof)$
431\listoffigures
431\listoffigures
432$endif$
432$endif$
433−$if(book-class)$
433+$if(has-frontmatter)$
434\mainmatter
434\mainmatter
435$endif$
435$endif$
436$body$
436$body$
437
437
438−$if(book-class)$
438+$if(has-frontmatter)$
439\backmatter
439\backmatter
440$endif$
440$endif$
441$if(natbib)$
441$if(natbib)$
442$if(bibliography)$
442$if(bibliography)$
443$if(biblio-title)$
443$if(biblio-title)$
444−$if(book-class)$
444+$if(has-chapters)$
445\renewcommand\bibname{$biblio-title$}
445\renewcommand\bibname{$biblio-title$}
446$else$
446$else$
447\renewcommand\refname{$biblio-title$}
447\renewcommand\refname{$biblio-title$}
src/Text/Pandoc/Writers/LaTeX.hs+29−26
@@ -63,7 +63,7 @@ data WriterState =
63 , stUrl :: Bool -- true if document has visible URL link
63 , stUrl :: Bool -- true if document has visible URL link
64 , stGraphics :: Bool -- true if document contains images
64 , stGraphics :: Bool -- true if document contains images
65 , stLHS :: Bool -- true if document has literate haskell code
65 , stLHS :: Bool -- true if document has literate haskell code
66− , stBook :: Bool -- true if document uses book or memoir class
66+ , stHasChapters :: Bool -- true if document has chapters
67 , stCsquotes :: Bool -- true if document uses csquotes
67 , stCsquotes :: Bool -- true if document uses csquotes
68 , stHighlighting :: Bool -- true if document has highlighted code
68 , stHighlighting :: Bool -- true if document has highlighted code
69 , stIncremental :: Bool -- true if beamer lists should be displayed bit by bit
69 , stIncremental :: Bool -- true if beamer lists should be displayed bit by bit
@@ -88,10 +88,10 @@ startingState options = WriterState {
88 , stUrl = False
88 , stUrl = False
89 , stGraphics = False
89 , stGraphics = False
90 , stLHS = False
90 , stLHS = False
91− , stBook = case writerTopLevelDivision options of
91+ , stHasChapters = case writerTopLevelDivision options of
92− TopLevelPart -> True
92+ TopLevelPart -> True
93− TopLevelChapter -> True
93+ TopLevelChapter -> True
94− _ -> False
94+ _ -> False
95 , stCsquotes = False
95 , stCsquotes = False
96 , stHighlighting = False
96 , stHighlighting = False
97 , stIncremental = writerIncremental options
97 , stIncremental = writerIncremental options
@@ -128,7 +128,6 @@ pandocToLaTeX options (Pandoc meta blocks) = do
128 isInternalLink _ = []
128 isInternalLink _ = []
129 modify $ \s -> s{ stInternalLinks = query isInternalLink blocks' }
129 modify $ \s -> s{ stInternalLinks = query isInternalLink blocks' }
130 let template = fromMaybe "" $ writerTemplate options
130 let template = fromMaybe "" $ writerTemplate options
131− -- set stBook depending on documentclass
132 let colwidth = if writerWrapText options == WrapAuto
131 let colwidth = if writerWrapText options == WrapAuto
133 then Just $ writerColumns options
132 then Just $ writerColumns options
134 else Nothing
133 else Nothing
@@ -138,17 +137,25 @@ pandocToLaTeX options (Pandoc meta blocks) = do
138 (fmap render' . blockListToLaTeX)
137 (fmap render' . blockListToLaTeX)
139 (fmap render' . inlineListToLaTeX)
138 (fmap render' . inlineListToLaTeX)
140 meta
139 meta
141− let bookClasses = ["memoir","book","report","scrreprt","scrbook"]
140+ let chaptersClasses = ["memoir","book","report","scrreprt","scrbook"]
142− let documentClass = case P.parse pDocumentClass "template" template of
141+ let frontmatterClasses = ["memoir","book","scrreprt","scrbook"]
143− Right r -> r
142+ -- these have \frontmatter etc.
144− Left _ -> ""
143+ beamer <- gets stBeamer
144+ let documentClass =
145− case lookup "documentclass" (writerVariables options) `mplus`
145+ case lookup "documentclass" (writerVariables options) `mplus`
146− fmap stringify (lookupMeta "documentclass" meta) of
146+ fmap stringify (lookupMeta "documentclass" meta) of
147− Just x | x `elem` bookClasses -> modify $ \s -> s{stBook = True}
147+ Just x -> x
148− | otherwise -> return ()
148+ Nothing ->
149− Nothing | documentClass `elem` bookClasses
149+ case P.parse pDocumentClass "template" template of
150− -> modify $ \s -> s{stBook = True}
150+ Right r -> r
151− | otherwise -> return ()
151+ Left _
152+ | beamer -> "beamer"
153+ | otherwise -> case writerTopLevelDivision options of
154+ TopLevelPart -> "book"
155+ TopLevelChapter -> "book"
156+ _ -> "article"
157+ when (documentClass `elem` chaptersClasses) $
158+ modify $ \s -> s{ stHasChapters = True }
152 -- check for \usepackage...{csquotes}; if present, we'll use
159 -- check for \usepackage...{csquotes}; if present, we'll use
153 -- \enquote{...} for smart quotes:
160 -- \enquote{...} for smart quotes:
154 let headerIncludesField :: FromJSON a => Maybe a
161 let headerIncludesField :: FromJSON a => Maybe a
@@ -163,7 +170,6 @@ pandocToLaTeX options (Pandoc meta blocks) = do
163 else case reverse blocks' of
170 else case reverse blocks' of
164 Header 1 _ il : _ -> (init blocks', il)
171 Header 1 _ il : _ -> (init blocks', il)
165 _ -> (blocks', [])
172 _ -> (blocks', [])
166− beamer <- gets stBeamer
167 blocks''' <- if beamer
173 blocks''' <- if beamer
168 then toSlides blocks''
174 then toSlides blocks''
169 else return blocks''
175 else return blocks''
@@ -197,17 +203,13 @@ pandocToLaTeX options (Pandoc meta blocks) = do
197
203
198 let context = defField "toc" (writerTableOfContents options) $
204 let context = defField "toc" (writerTableOfContents options) $
199 defField "toc-depth" (show (writerTOCDepth options -
205 defField "toc-depth" (show (writerTOCDepth options -
200− if stBook st
206+ if stHasChapters st
201 then 1
207 then 1
202 else 0)) $
208 else 0)) $
203 defField "body" main $
209 defField "body" main $
204 defField "title-meta" titleMeta $
210 defField "title-meta" titleMeta $
205 defField "author-meta" (intercalate "; " authorsMeta) $
211 defField "author-meta" (intercalate "; " authorsMeta) $
206− defField "documentclass" (if beamer
212+ defField "documentclass" documentClass $
207− then ("beamer" :: String)
208− else if stBook st
209− then "book"
210− else "article") $
211 defField "verbatim-in-note" (stVerbInNote st) $
213 defField "verbatim-in-note" (stVerbInNote st) $
212 defField "tables" (stTable st) $
214 defField "tables" (stTable st) $
213 defField "strikeout" (stStrikeout st) $
215 defField "strikeout" (stStrikeout st) $
@@ -215,7 +217,8 @@ pandocToLaTeX options (Pandoc meta blocks) = do
215 defField "numbersections" (writerNumberSections options) $
217 defField "numbersections" (writerNumberSections options) $
216 defField "lhs" (stLHS st) $
218 defField "lhs" (stLHS st) $
217 defField "graphics" (stGraphics st) $
219 defField "graphics" (stGraphics st) $
218− defField "book-class" (stBook st) $
220+ defField "has-chapters" (stHasChapters st) $
221+ defField "has-frontmatter" (documentClass `elem` frontmatterClasses) $
219 defField "listings" (writerListings options || stLHS st) $
222 defField "listings" (writerListings options || stLHS st) $
220 defField "beamer" beamer $
223 defField "beamer" beamer $
221 (if stHighlighting st
224 (if stHighlighting st
@@ -991,7 +994,7 @@ sectionHeader unnumbered ident level lst = do
991 else braces (text "\\texorpdfstring"
994 else braces (text "\\texorpdfstring"
992 <> braces txt
995 <> braces txt
993 <> braces (text plain))
996 <> braces (text plain))
994− book <- gets stBook
997+ book <- gets stHasChapters
995 opts <- gets stOptions
998 opts <- gets stOptions
996 let topLevelDivision = if book && writerTopLevelDivision opts == TopLevelDefault
999 let topLevelDivision = if book && writerTopLevelDivision opts == TopLevelDefault
997 then TopLevelChapter
1000 then TopLevelChapter