@@ -63,7 +63,7 @@ data WriterState =
6363 , stUrl :: Bool -- true if document has visible URL link
6464 , stGraphics :: Bool -- true if document contains images
6565 , 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
6767 , stCsquotes :: Bool -- true if document uses csquotes
6868 , stHighlighting :: Bool -- true if document has highlighted code
6969 , stIncremental :: Bool -- true if beamer lists should be displayed bit by bit
@@ -88,10 +88,10 @@ startingState options = WriterState {
8888 , stUrl = False
8989 , stGraphics = False
9090 , stLHS = False
91− , stBook = case writerTopLevelDivision options of
92− TopLevelPart -> True
93− TopLevelChapter -> True
94− _ -> False
91+ , stHasChapters = case writerTopLevelDivision options of
92+ TopLevelPart -> True
93+ TopLevelChapter -> True
94+ _ -> False
9595 , stCsquotes = False
9696 , stHighlighting = False
9797 , stIncremental = writerIncremental options
@@ -128,7 +128,6 @@ pandocToLaTeX options (Pandoc meta blocks) = do
128128 isInternalLink _ = []
129129 modify $ \s -> s{ stInternalLinks = query isInternalLink blocks' }
130130 let template = fromMaybe "" $ writerTemplate options
131− -- set stBook depending on documentclass
132131 let colwidth = if writerWrapText options == WrapAuto
133132 then Just $ writerColumns options
134133 else Nothing
@@ -138,17 +137,25 @@ pandocToLaTeX options (Pandoc meta blocks) = do
138137 (fmap render' . blockListToLaTeX)
139138 (fmap render' . inlineListToLaTeX)
140139 meta
141− let bookClasses = ["memoir","book","report","scrreprt","scrbook"]
142− let documentClass = case P.parse pDocumentClass "template" template of
143− Right r -> r
144− Left _ -> ""
145− case lookup "documentclass" (writerVariables options) `mplus`
146− fmap stringify (lookupMeta "documentclass" meta) of
147− Just x | x `elem` bookClasses -> modify $ \s -> s{stBook = True}
148− | otherwise -> return ()
149− Nothing | documentClass `elem` bookClasses
150− -> modify $ \s -> s{stBook = True}
151− | otherwise -> return ()
140+ let chaptersClasses = ["memoir","book","report","scrreprt","scrbook"]
141+ let frontmatterClasses = ["memoir","book","scrreprt","scrbook"]
142+ -- these have \frontmatter etc.
143+ beamer <- gets stBeamer
144+ let documentClass =
145+ case lookup "documentclass" (writerVariables options) `mplus`
146+ fmap stringify (lookupMeta "documentclass" meta) of
147+ Just x -> x
148+ Nothing ->
149+ case P.parse pDocumentClass "template" template of
150+ Right r -> r
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 }
152159 -- check for \usepackage...{csquotes}; if present, we'll use
153160 -- \enquote{...} for smart quotes:
154161 let headerIncludesField :: FromJSON a => Maybe a
@@ -163,7 +170,6 @@ pandocToLaTeX options (Pandoc meta blocks) = do
163170 else case reverse blocks' of
164171 Header 1 _ il : _ -> (init blocks', il)
165172 _ -> (blocks', [])
166− beamer <- gets stBeamer
167173 blocks''' <- if beamer
168174 then toSlides blocks''
169175 else return blocks''
@@ -197,17 +203,13 @@ pandocToLaTeX options (Pandoc meta blocks) = do
197203
198204 let context = defField "toc" (writerTableOfContents options) $
199205 defField "toc-depth" (show (writerTOCDepth options -
200− if stBook st
206+ if stHasChapters st
201207 then 1
202208 else 0)) $
203209 defField "body" main $
204210 defField "title-meta" titleMeta $
205211 defField "author-meta" (intercalate "; " authorsMeta) $
206− defField "documentclass" (if beamer
207− then ("beamer" :: String)
208− else if stBook st
209− then "book"
210− else "article") $
212+ defField "documentclass" documentClass $
211213 defField "verbatim-in-note" (stVerbInNote st) $
212214 defField "tables" (stTable st) $
213215 defField "strikeout" (stStrikeout st) $
@@ -215,7 +217,8 @@ pandocToLaTeX options (Pandoc meta blocks) = do
215217 defField "numbersections" (writerNumberSections options) $
216218 defField "lhs" (stLHS st) $
217219 defField "graphics" (stGraphics st) $
218− defField "book-class" (stBook st) $
220+ defField "has-chapters" (stHasChapters st) $
221+ defField "has-frontmatter" (documentClass `elem` frontmatterClasses) $
219222 defField "listings" (writerListings options || stLHS st) $
220223 defField "beamer" beamer $
221224 (if stHighlighting st
@@ -991,7 +994,7 @@ sectionHeader unnumbered ident level lst = do
991994 else braces (text "\\texorpdfstring"
992995 <> braces txt
993996 <> braces (text plain))
994− book <- gets stBook
997+ book <- gets stHasChapters
995998 opts <- gets stOptions
996999 let topLevelDivision = if book && writerTopLevelDivision opts == TopLevelDefault
9971000 then TopLevelChapter