class: center, middle, inverse, title-slide .title[ # Output Formats: PDF document ] .author[ ### Ziwei Ma ] .date[ ### 2022-09-19 ] --- class: inverse, middle # PDF document - ## Documents elements - ## Templates and LaTex output - ## Formatting --- ## Documents elements .pull-left[ - Insert page breaks - Update the data automatically - Unnumbered sections - Bibliographies and citations ] .pull-right[ - Omit a heading in the table of content - Cross-referencing - Escape special characters ] <iframe src="https://bookdown.org/yihui/rmarkdown-cookbook/document-elements.html" width="100%" height="400px" data-external="1"></iframe> --- ### Insert page breaks ```r --- title: Breaking pages output: pdf_document: default word_document: default html_document: default odt_document: default --- # The first section \newpage # The second section ``` --- ### Unnumbered sections ```r # This section is unnumbered {-} ``` ### Update the data automatically To add the data on your Rmd document, you can add an the `date` field in YAML. ```r date: "`r Sys.Date()`" ``` There are more data-time format available. ```r date: "`r format(Sys.time(), '%d %B, %Y')`" ``` - `%B %Y` : September 2022 - `%d/%m/%y` : 09/09/22 - `%a/%d/%b` : Fri 09 Sep - [More formatting of date.](https://bookdown.org/yihui/rmarkdown-cookbook/update-date.html) --- ### Omit a heading in the table of content If you do not want certain section headings to be included in the table of contents, you can add the following code to the heading. ```r # Section heading {.unlisted .unnumbered} ``` ### Escape special characters Some characters have special meanings in the Markdown syntax. If you want these characters verbatim, you have to escape them. - For example, a pair of underscores surrounding text usually makes the text italic. You need to escape the underscores if you want verbatim underscores instead of italic text. - The way to escape a special character is to **add a backslash before it**. - `\# This is not a heading` - `I do not want \_italic text\_ here` --- ### Bibliographies and citations There are two commonly used packages to manage citations and bibliographic references in a document. .pull-left[ - **`natbib`** - **`biblatex`** ] .pull-right[ ```r output: pdf_document: citation_package: natbib bookdown::pdf_book: citation_package: biblatex ``` ] - The bibliographic data can be in several formats. The BibTex format is introduced here. --- ### Bibliographies and citations - A BibTeX database is a plain-text file (with the conventional filename extension .bib) that consists of bibliography entries like this: ```r @Manual{R-base, title = {R: A Language and Environment for Statistical Computing}, author = {{R Core Team}}, organization = {R Foundation for Statistical Computing}, address = {Vienna, Austria}, year = {2016}, url = {https://www.R-project.org/}, } ``` A bibliography entry - starts with `@type{` may be `artile`, `book`, `manual`, and so on - then there is a citation key, like `R-base` in the above example - To cite an entrym use `@key` or `[@key]` --- ### Cross-referencing Cross-referencing is a useful way of directing your readers through your document, and can be automatically done within R Markdown. To use cross-references, you will need: - **A bookdown output format** ```r output: bookdown::html_document2: default bookdown::pdf_document2: default ``` - **A caption to your figure (or equati0n or table)** Figures without a caption will be included directly as images and will therefore **not** be a numbered figure. ```r Also see Equation \@ref(eq:mean). \begin{equation} \bar{X} = \frac{\sum_{i=1}^n X_i}{n} (\#eq:mean) \end{equation} ``` --- class: middle, inverse ## Templates and LaTex output ### Templates ### LaTeX output --- ### Templates - #### `rticles` package There are many LaTeX templates available in package `rticles`, which is very convenient to get started. - #### Custom LaTeX template - Add LaTeX code to the preamble - Pandoc options for LaTeX output - Include additional LaTeX packages - Use a custom Pandoc LaTeX template --- #### `rticles` package To use `rticles` package, we should install it first - Install `rticles` package ```r install.packages("rticles") ``` If you wish to install the development version from GitHub (which often contains new article formats), you can do this: ```r remotes::install_github("rstudio/rticles") ``` --- Using `rticles` - To use rticles from RStudio, you can access the templates through `File -> New File -> R Markdown` This will open the dialog box where you can select from one of the available templates: {width=80%} --- List of available journal names: ```r rticles::journals() ``` ``` ## [1] "acm" "acs" "aea" "agu" ## [5] "ajs" "amq" "ams" "arxiv" ## [9] "asa" "bioinformatics" "biometrics" "copernicus" ## [13] "ctex" "elsevier" "frontiers" "glossa" ## [17] "ieee" "ims" "informs" "iop" ## [21] "isba" "jasa" "jedm" "joss" ## [25] "jss" "lipics" "lncs" "mdpi" ## [29] "mnras" "oup_v0" "oup_v1" "peerj" ## [33] "pihph" "plos" "pnas" "rjournal" ## [37] "rsos" "rss" "sage" "sim" ## [41] "springer" "tf" "trb" "wellcomeor" ``` - [More information](https://github.com/rstudio/rticles) --- class: inverse, middle ## Formatting ### Font color ### Indent text ### Control the size of images/tables output ### More --- ### Font color The Markdown syntax has no built-in method for changing text colors. We can use HTML and LaTeX syntax to change the formatting of words: - For HTML, we can wrap the text in the `<span>` tag and set color with CSS, e.g., `<span style="color: red;">text</span>`. - For PDF, we can use the LaTeX command `\textcolor{}{}`. This requires the LaTeX package **xcolor**, which is included in Pandoc’s default LaTeX template. As an example of changing the color in PDF text: ```r --- output: pdf_document --- Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}. ``` --- ### Indent text Whitespaces are often meaningless in Markdown. Markdown will also ignore spaces used for indentation by default. However, we may want to keep the indentation in certain cases, e.g., in verses and addresses. In these situations, we can use line blocks by starting the line with a vertical bar `(|)`. The line breaks and any leading spaces will be preserved in the output. For example: .pull-left[ ``` | When dollars appear it's a sign | that your code does not quite align | Ensure that your math | in xaringan hath | been placed on a single long line ``` ] .pull-right[  ] --- ### Control the size of plots/images - The size of plots made in R can be controlled by the chunk option fig.width and fig.height (in inches). - Also, you can choose to display a different size in the output using chunk options, `out.width` and `out.height`, e.g., `out.width = "50%"` .pull-left[ ``` <img src="r-logo.png" width="50%" /> ``` <img src="r-logo.png" width="50%" /> ] .pull-right[ ``` <img src="r-logo.png" width="80%" /> ``` <img src="r-logo.png" width="80%" /> ] --- ## More LaTex output <iframe src="https://bookdown.org/yihui/rmarkdown-cookbook/latex-output.html" width="100%" height="400px" data-external="1"></iframe>