Skip to content

Commit

Permalink
LaTeX writer: use cslreferences environment for csl bibliographies.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jgm committed Sep 3, 2019
1 parent d0bddaf commit 0fe635d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
7 changes: 7 additions & 0 deletions data/templates/default.latex
Original file line number Diff line number Diff line change
Expand Up @@ -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$}
Expand Down
21 changes: 18 additions & 3 deletions src/Text/Pandoc/Writers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

2 comments on commit 0fe635d

@ttxtea
Copy link

@ttxtea ttxtea commented on 0fe635d Dec 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In pandoc 2.8. adding

$if(csl-refs)$
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newenvironment{cslreferences}%
  {$if(csl-hanging-indent)$\setlength{\parindent}{0pt}%
  \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces$endif$}%
  {\par}
$endif$

to ones own latex tempaltes would solve the CSLReferences environment not found issues. WIth pandoc 2.11.3.
this no longer works. It first treats the $if(csl-refs)$ as false even if the is a csl file in the YAML block. But even when removing the if condition. There are complains about the \newlength command. Replacing this with \def leads to a Undefined control sequence. l.565 \CSLLeftMargin

@jgm
Copy link
Owner Author

@jgm jgm commented on 0fe635d Dec 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ttxtea if you're using 2.11.3, you should be using the default latex template, which already includes the needed definitions for CSL (defined somewhat differently than before). Or, if you want to make changes, use what's in that template as your basis.

Please sign in to comment.