aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2019-09-02 22:14:00 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2019-09-02 22:14:00 -0700
commit0fe635d3ecdc362f11c380c2e0b9518aa03424e9 (patch)
treee7aacfaafb6f4b963ee5365b78d14a5b5e3fab9d
parentd0bddaf7b9f7ecec8a1ac55d7266cd021bdcb731 (diff)
downloadpandoc-0fe635d3ecdc362f11c380c2e0b9518aa03424e9.tar.gz
LaTeX writer: use `cslreferences` environment for csl bibliographies.
this allows bibliographies to receive special formatting. The template now contains definition of this environment (enabled only when CSL is used). It also defines a `\cslhangindent` length. This is set to 2em by default when the bibliography style specifies a hanging indent. To override the length, you can use e.g. \setlength{\cslhangindent}{7em} in header-includes. Closes jgm/pandoc-citeproc#410.
-rw-r--r--data/templates/default.latex7
-rw-r--r--src/Text/Pandoc/Writers/LaTeX.hs21
2 files changed, 25 insertions, 3 deletions
diff --git a/data/templates/default.latex b/data/templates/default.latex
index 4e8911e12..3dc787038 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -359,6 +359,13 @@ $for(bibliography)$
\addbibresource{$bibliography$}
$endfor$
$endif$
+$if(csl-refs)$
+\newlength{\cslhangindent} % set up new length
+\setlength{\cslhangindent}{$if(csl-hanging-indent)$2em$else$0em$endif$}
+\newenvironment{cslreferences}%
+ {\everypar{\setlength{\hangindent}{\cslhangindent}}}%
+ {\par} % by default, this env does not change anything
+$endif$
$if(title)$
\title{$title$$if(thanks)$\thanks{$thanks$}$endif$}
diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs
index f880b0f12..3c952c2d1 100644
--- a/src/Text/Pandoc/Writers/LaTeX.hs
+++ b/src/Text/Pandoc/Writers/LaTeX.hs
@@ -72,6 +72,8 @@ data WriterState =
, stInternalLinks :: [String] -- list of internal link targets
, stBeamer :: Bool -- produce beamer
, stEmptyLine :: Bool -- true if no content on line
+ , stHasCslRefs :: Bool -- has a Div with class refs
+ , stCslHangingIndent :: Bool -- use hanging indent for bib
}
startingState :: WriterOptions -> WriterState
@@ -100,7 +102,9 @@ startingState options = WriterState {
, stIncremental = writerIncremental options
, stInternalLinks = []
, stBeamer = False
- , stEmptyLine = True }
+ , stEmptyLine = True
+ , stHasCslRefs = False
+ , stCslHangingIndent = False }
-- | Convert Pandoc to LaTeX.
writeLaTeX :: PandocMonad m => WriterOptions -> Pandoc -> m Text
@@ -237,6 +241,8 @@ pandocToLaTeX options (Pandoc meta blocks) = do
then id
else defField "dir" ("ltr" :: Text)) $
defField "section-titles" True $
+ defField "csl-refs" (stHasCslRefs st) $
+ defField "csl-hanging-indent" (stCslHangingIndent st) $
defField "geometry" geometryFromMargins $
(case T.unpack . render Nothing <$>
getField "papersize" metadata of
@@ -456,10 +462,11 @@ toSlides bs = do
concat `fmap` mapM (elementToBeamer slideLevel) (hierarchicalize bs')
elementToBeamer :: PandocMonad m => Int -> Element -> LW m [Block]
-elementToBeamer _slideLevel (Blk (Div attr bs)) = do
+elementToBeamer _slideLevel (Blk (Div attrs bs)) = do
-- make sure we support "blocks" inside divs
bs' <- concat `fmap` mapM (elementToBeamer 0) (hierarchicalize bs)
- return [Div attr bs']
+ return [Div attrs bs']
+
elementToBeamer _slideLevel (Blk b) = return [b]
elementToBeamer slideLevel (Sec lvl _num (ident,classes,kvs) tit elts)
| lvl > slideLevel = do
@@ -547,6 +554,14 @@ blockToLaTeX (Div (identifier,classes,kvs) bs)
modify $ \s -> s{ stIncremental = oldIncremental }
return result
else blockToLaTeX $ Div (identifier,classes',kvs) bs
+ | identifier == "refs" = do
+ modify $ \st -> st{ stHasCslRefs = True
+ , stCslHangingIndent =
+ "hanging-indent" `elem` classes }
+ contents <- blockListToLaTeX bs
+ return $ "\\begin{cslreferences}" $$
+ contents $$
+ "\\end{cslreferences}"
| otherwise = do
beamer <- gets stBeamer
linkAnchor' <- hypertarget True identifier empty