Issues with different citation packages in bookdown - r-markdown

I am creating a pdf-book using bookdown, with a .bib file that holds my citations. I call the citations with the standard [#citation] used in bookdown. Citing works correctly, but the bibliography doesn't seem to be how I wanted it.
Here are the things that I need to work:
The bibliography should be included in the toc, at the end of \mainmatter, but before the \backmatter, which I use to attach pdf's as an appendix, where the appendix header is listed in the toc
The in-text citations should be in number format, with possible nested citations, like this: [1,2]
Bibliography should be in the order they appear in the text, with the possibility of removing DOI/URLs etc as I see fit
For these things to work so far, I've tried the natbib package, which seem to work fine. However, I do not seem to be able to remove DOI/URLs from specific bibliography entries, such as journal articles. Therefore, I tried using biblatex or the built-in pandoc, but neither seem to work properly (With pandoc the bibliography is gone from the toc, and the bibliography entries are messy. With biblatex, i get multiple errors that the program can't find specific entries in the .bib file, which are there)
This is my YAML with natbib:
site: bookdown::bookdown_site
geometry: "left=4cm,right=3cm,top=3cm,bottom=3cm"
subparagraph: true
output:
bookdown::pdf_book:
latex_engine: xelatex
fig_caption: yes
toc: false
citation_package: natbib
includes:
before_body: frontpage.tex
after_body: after_body.tex
in_header: preamble.tex
fontsize: 11pt
linestretch: 1.2
documentclass: book
bibliography: [packages.bib, libraryzotero.bib]
link-citations: yes
---
And this is in my preamble.tex:
\usepackage{titlesec}
\usepackage{pdfpages}
\titleformat{\chapter}
{\normalfont\LARGE\bfseries}{\thechapter}{1em}{} % set title format
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\AtBeginDocument{\let\maketitle\relax}
\usepackage{makeidx}
\makeindex
\setcitestyle{numbers,square,comma}
\usepackage{url}
\usepackage[nottoc]{tocbibind}
\usepackage{caption}
\captionsetup[figure]{textfont={small,it}, labelfont={normalsize,bf,it}} % set figure caption font size and style
\usepackage{graphicx}
\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
\expandafter\origfigure\expandafter[H]
} {
\endorigfigure
}
\raggedbottom
\usepackage{fancyhdr,blindtext}
\fancyhf{}
\fancyhead[LO]{\slshape \rightmark} %section
\fancyhead[RO]{\thepage}
\fancyhead[RE]{\slshape \leftmark} % chapter
\fancyhead[LE]{\thepage}
\setlength{\headheight}{27.7pt} % as requested by fancyhdr's warning
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\makeatletter
\renewcommand{\chaptermark}[1]{% % change settings for including headers
\if#mainmatter
\markboth{Chapter \thechapter{}: #1}{}%
\else
\markboth{#1}{}%
\fi
}
\makeatother
\usepackage{epigraph}
\setcounter{secnumdepth}{1}
\setcounter{tocdepth}{1}
Where \setcitestyle{numbers, square, comma} is for the in-text references.
Does anyone have any suggestions how to get these things to work with bookdown?

For the natbib based solution you could patch the used bibliography style:
You are not setting any bibliography style. Therefore plainnat.bst is used.
Copy plainnat.bst from your TeX installation (typically TEXMF/bibtex/bst/natbib/) to your working directory with a new name.
Add biblio-style: <new-name> to your YAML header.
Edit your BST file:
Find FUNCTION {article}.
Within that block remove format.doi output and format.url output.
Repeat for other types.

Related

Bookdown / Knitr / Kable Outputting Perfectly to HTML but No Table to PDF

I am using the bookdown package to produce a large document with tables using the knitr::kable function. I can get the document to produce tables in HTML perfectly, but in PDF, the table contents just appear as a list of numbers. This is also true for the output to Word. The compiled LaTeX just appears to be a list of numbers as well.
This is also true of output from other packages like sjplot.
The same problem also appears when the sample code from the bookdown book.
knitr::kable(
list(
head(iris[, 1:2], 3),
head(mtcars[, 1:3], 5)
),
caption = 'A Tale of Two Tables.', booktabs = TRUE
)
Produces the expected output in HTML:
But, produces the following in PDF:
The YAML header in index.rmd are:
#output ~~~~~~~~~~~ [see _output.yml]
site: bookdown::bookdown_site
output: [bookdown::gitbook, bookdown::pdf_book, bookdown::word_document2, bookdown::html_document2]
documentclass: book
The output settings in the _output.yml are:
bookdown::pdf_book:
keep_tex: true
latex_engine: xelatex
includes:
in_header: tex/biblio.tex
number_sections: yes
pandoc_args: ["--top-level-division=chapter"]
It turns out the problem arises from some interaction with the kableExtra package
So, even if you specify (as I did in my sample code) the knitr namespace (knitr::kable), there is still some downstream interference if kableExtra is loaded. So, this will not work when rendering to PDF:
library(kableExtra, warn.conflicts = TRUE)
knitr::kable(head(iris, 20), caption = 'Here is a nice table!', booktabs = TRUE)
While everything works for the HTML output, the rendering to PDF does not work for the tables and you just end up with a list of the table cell values (for both PDF and Word) - as shown in the image above.
To solve, remove any library statements
Then, ensure you unload the package:
detach("package:kableExtra", unload = TRUE)
And, finally, for me, I found that I needed to "Restart R and Clear All Outputs" or "Terminate R" from the R-Studio "Session" menu.

Text vanishes in Gitbook using Bookdown

Iw as working on a gitbook using bookdown in R several months ago. Today, I resumed my work again, but when I rendered the book the font of the output changed dramatically. It vanishes! I don't remember having changed anything on the TAML of the book. This is how it looks now. Right after the word WELCOME, the text becomes lighter and lighter until it literally disappears. It has a sort of vanishing effect that I have no idea how I got.
Thank you for your help.
Best,
this is my _output.yml
bookdown::gitbook:
config:
toc:
collapse: section
scroll_highlight: yes
before: |
<li>PovcalNet Internal Guidelines</li>
after: |
<li>Published with bookdown</li>
download: ["pdf", "epub"]
toolbar:
position: fixed
edit : null
search: yes
fontsettings:
theme: white
family: sans
size: 2
sharing:
facebook: no
github: no
twitter: no
linkedin: no
weibo: no
instapaper: no
vk: no
info: yes
bookdown::pdf_book:
includes:
in_header: preamble.tex
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
bookdown::epub_book:
stylesheet: style.css
And this is my _bookdown.yml
book_filename: "Povcalnet_internal_guidelines"
repo: https://github.com/xxxxxx/Povcalnet_internal_guidelines/
output_dir: "docs"
delete_merged_file: true
language:
label:
fig: "Figure "
tab: "Table "
ui:
edit: "Edit"
chapter_name: "Chapter "
rmd_files: [
"index.Rmd",
"intro.Rmd",
"Folder_structure.Rmd",
"Collaboration_in_Git.Rmd",
"DM_Group_data.Rmd",
"Handover.Rmd",
"references.Rmd",
]
before_chapter_script: "_common.R"
And, I am rendering the book using the following instruction,
bookdown::render_book("index.Rmd", "bookdown::gitbook")
I found the problem. Basically, in one of my .Rmd files I create a diagram using the DiagrammeR package. When one of my colleagues removed the diagram, the issue was gone, but we had no diagram. However, I created a separate .mmd (mermaid) file and updated both packages glue and DiagrammeR. All of this solved the problem.
thanks.

Which RMarkdown theme/style is used to get the TOC shown below?

The default floating table-of-contents looks like below using theme 'united' I think.
How does one create the TOC theme/style shown below?
Here is the page.
Here is another example:
Link here.
This question is not about how to create the floating TOC or about the general page style/theme, but where does the specific TOC style come from? Is it an argument somewhere? Perhaps some variation to tocify?
html_document output supports TOC, you just need to turn it on.
Try this:
---
title: "example yaml"
author: "you"
date: "8/20/2018"
output:
html_document:
toc: true
toc_float: true
---
More settings of TOC, please refer to Rmarkdown: the definitive guide
Edit:
I checked the gh-page for your link, however I didn't know how the author generated those pages. If you want the same appearance of TOC in that page, maybe ask the author?

Add a link to the title of references

I'm creating a document with rmarkdown (pandoc), which includes some bibliography in a .bib file. What I'd like to do is add a link to the title of the references, so that each of them links to a page of the form http://sample.com/citation-key.html, like this:
Author. (2017). Sample Title. Journal, 1(1), 1–2.
I've tried modifying the .csl file by adding prefixes and suffixes to the title, but everything I put in there is escaped, whether I use markdown or HTML syntax. Unfortunately, I can't change the .bib file. The relevant part of the .csl file is this:
<text variable="title"/>
Sample files are:
literature.Rmd:
---
output: html_document
bibliography: literature.bib
csl: literature.csl
---
#author2017word says this doesn't work.
## References
literature.bib
#article{author2017word,
author = {Author},
journal = {Journal},
number = {1},
pages = {1--2},
title = {{Sample Title}},
volume = {1},
year = {2017}
}
literature.csl: I'm using the APA style from here (line 231).
My pull request to citeproc adding support this feature was recently merged, and as a result, the latest release of Pandoc (v2.14.2) now hyperlinks titles by default when the citation does not already show a raw URL! From the citeproc readme:
When linkBibliography=True automatically linkifies any identifiers (DOI, PMCID, PMID, or URL) appearing in a bibliography entry. When an entry has a DOI, PMCID, PMID, or URL available but none of these are rendered by the style, add a link to the title (or, if no title is present, the whole entry), using the URL for the DOI, PMCID, PMID, or URL (in that order of priority). See Appendix VI of the CSL v1.0.2 spec.
In pandoc, this option is controlled by the link-bibliography metadata field, which is True by default.

Creating a footer for every page using R markdown

I'm writing a document in R Markdown and I'd like it to include a footer on every page when I knit a PDF document. Does anyone have any idea on how to do this?
Yes, this question has been asked and answered here: Adding headers and footers using Pandoc. You just need to sneak a little LaTeX into the YAML header of your markdown document.
This markdown header does the trick:
---
title: "Test"
author: "Author Name"
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{This is fancy header}
- \fancyfoot[CO,CE]{And this is a fancy footer}
- \fancyfoot[LE,RO]{\thepage}
output: pdf_document
---
Works for me with an Rmd file in RStudio Version 0.98.1030 for Windows.
Another option would be to use the argument includes provided by rmarkdown::pdf_document() (documentation). This allows you to keep the footer in a separate file. If your footer is defined in footer.tex, the header of your R Markdown file would look like this:
---
output:
pdf_document:
includes:
after_body: footer.tex
---
This also assumes that footer.tex is in the same directory as the R Markdown file.
Update: The file footer.tex can contain any valid LaTeX that you want to be inserted at the end of your PDF document. For example, footer.tex could contain the following:
This \textbf{text} will appear at the end of the document.
To manage the height of the footer, you can use the following:
date: '`r paste("Date:",Sys.Date())`'
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \setlength{\footskip}{-50pt} # set the footer size
Keep Coding!