rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.75k stars 1.26k forks source link

placing references before appendix does not work with natbib #1403

Open eyayaw opened 1 year ago

eyayaw commented 1 year ago

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:

  1. created a bookdown project in rstudio
  2. created 08-appendix.Rmd and added the following lines
# Appendix {.unnumbered}

# (APPENDIX) Appendix {.unnumbered}
  1. added to 07-references.Rmd <div id="refs"></div>
    
    `r if (knitr::is_html_output()) ' # References {-} '`

4. Build Book: bookdown::pdf_book

Bibliography chapter comes after Appendix

5. 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

![snapshot](https://user-images.githubusercontent.com/49317723/217543352-d909f990-17e9-4701-b0f2-e2ad5ce97b5f.png)

### Session info
```r
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:

cderv commented 1 year ago

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.

eyayaw commented 1 year ago

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$
cderv commented 1 year ago

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

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.

eyayaw commented 1 year ago

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
# plot ts
plot(...)
\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. 
cderv commented 1 year ago

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

eyayaw commented 1 year ago

Thank you very much. That is indeed a cool trick and exactly what I wanted.