class: center, middle, inverse, title-slide # Managing Citation In R Bookdown ## with Zotero ### Lin Yu ### University of Alberta ### 2021/07/31 --- class: inverse, center, middle # Get Started --- # Create R Bookdown Project If you do not have a R bookdown project yet, you will need to build one first. -- Create a new R bookdown<sup>1</sup> project from the menu `File - New Project -> New Directory -> Book Project using bookdown` - The contents of the book are wrote with R markdown, by default, the name of each R markdown file has the following format: chapter number and chapter title. For example, _01_introduction_. -- - The bibliographies are stored in the .bib file(s), in this case, book.bib and packages.bib. -- .footnote[ [1] See the [official tutorial](https://bookdown.org/yihui/bookdown/get-started.html) for more details ] --- class: inverse, center, middle # Have Zotero ready ### Install Zotero is quite simple, so I decided to skip this part:) --- class: inverse, middle, center # Better-bibtex extension --- # better-bibtex addin After downloading Zotero, you need to install the 'better-bibtex' extension, which help create **citekeys** for your bibliographies. [Here](https://github.com/retorquere/zotero-better-bibtex/releases/tag/v5.4.28) is where you can get the better-bibtex addin. then -- - Go to Tools on Zotero's main menu. Then click on Add-ons, select ‘Extensions’ -- - Click on the gear in the top-right corner and choose ‘Install Add-on From File…’ -- - Choose .xpi file that you have just downloaded, click ‘Install’ -- - Restart Zotero -- You can [customize](https://retorque.re/zotero-better-bibtex/citing/) the format of citekey as you like:) - You can think of the _citekey_ as the name or identifier of the bibliography - The _citekey_ is used in R markdown files for citation --- # How to Cite? In R markdown, there are a few different ways to cite a bibliography.: -- The simplest method is to use the visual mode's *insert citation* option. - Swith to the [visual mode](https://rstudio.github.io/visual-markdown-editing/), then you can find the insert button, where you can click on **citation** -- Alternatively, use *insert citation* addin in the **citr** R pacakge - why?: You may prefer source mode; you do not have visual mode in your current R environment. - install [citr](https://github.com/crsh/citr) package (you may want to use `devtools::install_github("crsh/citr")`) - afte installation, you can find it in the *Addins*, and you can even define a [keyboard shortcut](http://rstudio.github.io/rstudioaddins/#keyboard-shorcuts) to call the addin. -- --- # How to Cite? (Continued) The third method for citation: -- Manually export bibliographies into the R project. - [Export the collection](https://www.zotero.org/support/kb/exporting) as Better BibTex format, put the .bib file under the root directory of the R bookdown project; - in the R markdown file, use syntax `\cite{@citekey}` to cite - Not recommended: (i) you need to **manually** update the .bib file (ii) using `\cite{@citekey}` is not efficient --- class: inverse, center, middle # Almost there:) --- # One Last Step -- Change the bibliography setting in the index.Rmd file: (see below codes) ```r documentclass: book bibliography: [YOUR_REFERENCE.bib] ``` If you use the *insert citation* option, then .bib file with a defualt name of "reference.bib" will be generated and updated every time you add new citations. -- You are all set now! The default citation style is (author year) However, you can change the citation style as you like! --- # Change Citation Style [Two easy steps](https://stackoverflow.com/questions/48965247/use-csl-file-for-pdf-output-in-bookdown) are required if you want to change the citation style: -- Step 1: Download .csl file - Download the desired .csl file (e.g., the-lancet.csl), .csl file can be downloaded from the [Zotero Style Repo](https://www.zotero.org/styles), then you need to store it under the root directory of the R bookdown project - A trick here: by default, the .csl file will be downloaded to the Zotero folder. -- Step 2: modify index.Rmd and _output.yml - comment the biblio-style and specify the csl as follow: ```r bibliography: [book.bib, packages.bib,testbib.bib, references.bib] #biblio-style: apalike csl: the-lancet.csl ``` - add `pandoc_args: [ "--csl", "the-lancet.csl" ]` to the _output.yml (see next page) --- # Change Citation Style (Cont'd) - add `pandoc_args: [ "--csl", "the-lancet.csl" ]` to the _output.yml (see next page) ```r bookdown::gitbook: css: style.css pandoc_args: [ "--csl", "the-lancet.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: default pandoc_args: [ "--csl", "the-lancet.csl" ] keep_tex: yes bookdown::epub_book: pandoc_args: [ "--csl", "the-lancet.csl" ] ``` --- background-image: url(data:image/png;base64,#https://media4.giphy.com/media/fGU4lCGuH1sSffgNIE/200w.gif?cid=82a1493btnd325t1jxs7tfuglukb10rdhy9jzydo7b6h1b9n&rid=200w.gif&ct=g) background-position: 50% 40% class: center # I am so happy --- class: center, middle, inverse # Thank you