bookdown
bookdown copied to clipboard
placing references before appendix does not work with natbib
I have followed the cookbook suggestion for putting appendix after bibliography. However, that does not seem to work with natbib as a citation package.
What is in _output.yml
?
bookdown::gitbook:
css: style.css
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>
edit: https://github.com/USERNAME/REPO/edit/BRANCH/%s
download: ["pdf", "epub"]
bookdown::pdf_book:
includes:
in_header: preamble.tex
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
bookdown::epub_book: default
All files are populated by the bookdown-demo, see below for a snapshot. I have not done anything other than adding an appendix chapter and adding a div with id refs. Here is what I have done:
- created a bookdown project in rstudio
- created
08-appendix.Rmd
and added the following lines
# Appendix {.unnumbered}
# (APPENDIX) Appendix {.unnumbered}
- added to
07-references.Rmd
<div id="refs"></div>
`r if (knitr::is_html_output()) ' # References {-} '`
<div id="refs"></div>
- Build Book: bookdown::pdf_book
Bibliography chapter comes after Appendix
- commented out the citation_package: natbib in the _output.yml file Now, the Bibliography chapter comes before Appendix, as intended.
What should I do to get the appendix after the references with natbib as a citation package?
Additional info
Session info
xfun::session_info('bookdown')
R version 4.2.2 (2022-10-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Ventura 13.2, RStudio 2022.12.0.353
Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
Package version:
base64enc_0.1.3 bookdown_0.32 bslib_0.4.2.9000
cachem_1.0.6.9000 cli_3.6.0.9000 digest_0.6.31
ellipsis_0.3.2.9000 evaluate_0.20.1 fastmap_1.1.0.9000
fs_1.6.0.9000 glue_1.6.2 graphics_4.2.2
grDevices_4.2.2 highr_0.10 htmltools_0.5.4.9000
jquerylib_0.1.4 jsonlite_1.8.4 knitr_1.42
lifecycle_1.0.3.9000 magrittr_2.0.3 memoise_2.0.1.9000
methods_4.2.2 mime_0.12 R6_2.5.1.9000
rappdirs_0.3.3.9000 rlang_1.0.6.9000 rmarkdown_2.20
sass_0.4.5.9000 stats_4.2.2 stringi_1.7.12
stringr_1.5.0 tinytex_0.44.1 tools_4.2.2
utils_4.2.2 vctrs_0.5.2.9000 xfun_0.37
yaml_2.3.7
Checklist
When filing a bug report, please check the boxes below to confirm that you have provided us with the information we need. Have you:
-
[x] formatted your issue so it is easier for us to read?
-
[x] included a minimal, self-contained, and reproducible example?
-
[x] pasted the output from
xfun::session_info('bookdown')
in your issue? -
[x] upgraded all your packages to their latest versions (including your versions of R, the RStudio IDE, and relevant R packages)?
-
[ ] installed and tested your bug with the development version of the bookdown package using
remotes::install_github("rstudio/bookdown")
?
However, that does not seem to work with natbib as a citation package.
Yes this works only when using Pandoc's citation processing, as it relies on a Pandoc feature. https://pandoc.org/MANUAL.html#placement-of-the-bibliography
What should I do to get the appendix after the references with natbib as a citation package ?
This would require providing a custom (tweaked) latex template (https://bookdown.org/yihui/rmarkdown-cookbook/latex-template.html) as in default one, natbib is place at the end (https://github.com/jgm/pandoc/blob/main/data/templates/default.latex#L535)
Maybe there is way to set natbib: false
for template variable while style triggering natbib processing with Pandoc. This would require to add manually the step to style and include natbib (in header and in body).
Probably tweaking a template is easier for this.
Hope it helps finding a custom solution for this.
Thank you, @cderv. Although I have no idea about latex template scripting, I tried to tweak the default.latex template. My futile attempt is just moving the natbib block to the end of the file.
$if(natbib)$
$if(bibliography)$
$if(biblio-title)$
$if(has-chapters)$
\renewcommand\bibname{$biblio-title$}
$else$
\renewcommand\refname{$biblio-title$}
$endif$
$endif$
$if(beamer)$
\begin{frame}[allowframebreaks]{$biblio-title$}
\bibliographytrue
$endif$
\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}
$if(beamer)$
\end{frame}
$endif$
yeah that is tricky in fact because you'll have your appendix in the body... 🤔 I did not give you all details. It won't work easily indeed.
What we do in articles format is having appendix content passed using a variable in YAML
-
Template with appendix after bib https://github.com/rstudio/rticles/blob/45660fe7a4bdd92ebebd12c6cff54bf5a898a254/inst/rmarkdown/templates/tf/resources/template.tex#L131-L133
-
Appendix pass as a tex file in YAML (not as great as supporting markdown) : https://github.com/rstudio/rticles/blob/45660fe7a4bdd92ebebd12c6cff54bf5a898a254/inst/rmarkdown/templates/tf/skeleton/skeleton.Rmd#L29
This is one example. Some other in that repo will allow passing text directly to appendix
YAML variable (you can pass markdown to be parsed there)
Not as ideal to pass appendix in body but probably something like this Lua filter for Abstract would be required https://github.com/pandoc-ext/abstract-section
No easy solution for now. It requires some tweaks and a bit of skill. 😅 Please do share what you'll make work.
Maybe we'll try to make that more easy in the future.
Thank you so much for the explanation, @cderv! For now I switched to citation_package: default + hard-coding some citations.
```{=latex}
\begin{figure}[H]
\centering
```
```{r plot-ts}
# plot ts
plot(...)
```
```{=latex}
\caption{Caption...label{fig:plot-ts}
\medskip
\begin{minipage}{0.9\textwidth}
\footnotesize
\textit{Notes:} Data obtained from the Regional Database Germany (2022).
\end{minipage}
\end{figure}
```
It was merely interested in using natbib inside pure latex block like the above case, such as using \citet{atlas-de-2022} instead of ~Regional Database Germany (2022)~. I can see from the pandoc generated citation results that \hyperlink{ref-atlas-de-2022}{label} would work.
Thank you once again for helping out.
Ok good to know you found a solution. I don't know if Pandoc citeproc has a \citet
equivalent, put probably. I think you can use this kind of things in caption with bookdown using the Text reference feature I believe https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#text-references
This makes the text process by pandoc, and then HTML or PDF post process is done to put in the right place.
I think it would work
Thank you very much. That is indeed a cool trick and exactly what I wanted.