Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

r - use csl-file for pdf-output in bookdown

I would like to use a .csl-file for formatting references with bookdown. Adding csl: some-style.csl to index.Rmd affects the output to gitbook, but not to pdf_book. I know that I can specify biblio-style, but this only accepts some standard styles and not csl-files. Is there a proper workaround?

Steps to reproduce:

  1. Create new project with RStudio and choose "Book Project using bookdown" as option.
  2. Download some .csl file from https://www.zotero.org/styles and copy to root of project.
  3. Add csl: my_csl_file.csl to the header in index.Rmd.
  4. Build the book to pdf and html, and observe the differences in the references (either in the references section, or in the introduction)

Header in index.Rmd:

--- 
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: [book.bib, packages.bib]
csl: american-sociological-review.csl
link-citations: yes
description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."
---

HTML output (correct): enter image description here


PDF output (incorrect): enter image description here

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same problem. The following procedure worked for me:

  1. Create new project with RStudio and choose "Book Project using bookdown" as option.
  2. Download some .csl file from https://www.zotero.org/styles and copy to root of project. In my case: chicago-author-date-de.csl
  3. Set in _output.yml citation_package: none
  4. Add in all formats (gitbook, pdf_book, epub_book) in _output.yml the line pandoc_args: [ "--csl", "chicago-author-date-de.csl" ]
  5. Delete or comment out in index.Rmd the line biblio-style: apalike
  6. Replace the content of 06-references.Rmd with # References {-}

Here is my _output.yml file:

bookdown::gitbook:
  css: style.css
  pandoc_args: [ "--csl", "chicago-author-date-de.csl" ]
  config:
    toc:
      before: |
        <li><a href="./">A Minimal Book Example</a></li>
      after: |
        <li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
    download: ["pdf", "epub"]
bookdown::pdf_book:
  includes:
    in_header: preamble.tex
  latex_engine: xelatex
  citation_package: none
  pandoc_args: [ "--csl", "chicago-author-date-de.csl" ]
  keep_tex: yes
bookdown::epub_book:
  pandoc_args: [ "--csl", "chicago-author-date-de.csl" ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...