Rendering an Rmarkdown with bookdown as output works OK with: bookdown::html_document2 but seems to miss cross referencing (e.g. \#ref(someID) when output is set to html_notebook.
Is there a way to include this into an r-notebook?
Related
I have a series of .png images that I would like to add notes to in an Rmarkdown document that I am knitting to a .pdf. The basic code for each image looks like this:
```{r certs_coefplot, out.width="100%", fig.cap=fig.4_cap}
knitr::include_graphics("certs_coefplot.png")
```
With beamer slides, I have just inserted some basic latex like this:
\tiny \emph{Notes}: Put Notes here \normalsize
below each code chunk.
But if I try this in the context of a larger document, the notes do not appear below the figure.
A solution involving a custom hook was proposed to a similar question asked here about adding notes to figures in an .Rnw file. In particular, the version where you put the code for the hook at the beginning and then write:
notes = "Notes to explain the plot"
sources = "Explain the sources"
in each chunk seems really convenient.
Is it possible to apply a similar solution an RMarkdown file?
Based on the answer to this question, I was able to get 2-column papaja with listings wrapping (rather than overflowing column width). But the listings package turns off various features that help code listings and R output stand out relative to the main text.
A simple solution would be if I could globally change the font faces and/or sizes selectively for code and R output. Is there a way to do that in papaja? I haven't been able to figure this out from papaja or Rmarkdown documentation. Thank you!
When you use the listings package in a papaja (or bookdown) document, what is technically happening is that all code is wrapped into an lstlisting LaTeX environment that comes with its own capabilities of customizing code appearance. Hence, you don't see the syntax highlighting that you would otherwise see if you would not use the listings package. The documentation of the listings package with instructions how to style your code can be found here.
To make use of this, you can extend the YAML header of your papaja document like this:
documentclass : "apa6"
classoption : "jou"
output :
papaja::apa6_pdf:
pandoc_args: --listings
header-includes:
- \lstset{breaklines=true,language=R,basicstyle=\tiny\ttfamily,frame=trB,commentstyle=\color{darkgray}\textit}
Here, I first specify the code's language, and use a tiny monospace font. With frame, I add a frame around the code block, and with commentstyle I set comments in italic and gray.
As discussed here one can produce tab-ed outputs like this using RMarkdown:
## Results {.tabset .tabset-pills}
Can something similar also be achieved with markdown? Thanks!
My problem is with use of relative links and "compiling" (knitting).
I'm writing a book using RMarkdown. Since the file will be relatively large, I need to split into subfiles and directories.
I have a masterfile which only includes links to chapters(chap1, chap2, ...). There is a file for each chapter and each needs references to some figures (fig1,...).
I would like to be able to knit masterfile.Rmd and chap1.Rmd independently from each others so to get one file for chapters, and one file for the whole book.
Here is how my files are organized (MVE). Directories are displayed in upper case.
ROOT
masterfile.Rmd
FIGURES
fig1.pdf
fig2.pdf
CHAPTERS
chap1.Rmd
chap2.Rmd
...
The code of masterfile.Rmd is as follows:
{r child = '/chapters/chap1.Rmd'}
The code of chap1.Rmd is as follows:
![](../figures/fig1.pdf)
As I knit from chap1.Rmd, everything is fine. As I knit from masterfile, I get the following message:
[WARNING] Could not fetch resource '../figures/fig1.pdf': PandocResourceNotFound "../figures/fig1.pdf"
It seems that fig1.pdf cannot be found. I guess that this is because the include statement in master file only "copies" the code of chap1.Rmd and "executes" it in the ROOT directory (not in the CHAPTERS directory) so ".." drives to location that does not exist (before ROOT) when knitted from masterfile.
I could change the setting and write ![](/figures/fig1.pdf) in chap1.Rmd file. It would work fine when knitting from masterfile but no longer from chap1.Rmd.
Do you know how to have both?
Welcome to the RMarkdown community!
Due to KnitR, Rmd, and pandoc inter-workings, this is not as easy of a solution as you'd think, but a directory restructure would almost be easier.
Even if you look at Rmarkdown author's newest book RMarkdown CookbookHERE. They have all the chapters in the ROOT directory, and only sub directories for images(and possibly figures).
resources:
1. Other SO similar question
2. Github options knitr question
3. similar question from Rstudio community
You can make use of the Lua filter feature to rewrite image paths when knitting the main file. E.g.
function Image (img)
img.src = img.src:gsub('^%.%./', './')
return img
end
This will replace a path like ../figures/fig1.pdf with ./figures/fig1.pdf.
Use the code writing it to a file and calling adding it via pandoc_args (in the main file only).
---
output:
html_document:
pandoc_args: ["--lua-filter=filter.lua"]
---
I`m aware how to use additional Latex-packages for pdf-format output from .Rmd files using
---
header-includes:
- \usepackage{mathtools}
---
in the YAML header.
However, this does (of course) not work if one specifies output: html_document.
---
output: html_document
header-includes:
- \usepackage{mathtools}
---
Using additional Latex-Packages could be of interest for output: html_document, too - especially in math expressions (MWE below)
---
title: "MWE"
output: html_document
header-includes:
- \usepackage{mathtools}
---
## Use "Defined by" Symbol
$$sin(x) \coloneqq \frac{opposite}{hypothenuse}$$
MathJax offer a number of extensions, and there are third-party extensions as well. If your desired package is not available in this way, then things get difficult.
Simple commands, such as \coloneqq, can be recreated using \newcommand. The simplest way is to add these via the include-before option. Using your MWE with a solution from Mathematics Meta SE, one gets:
---
title: "MWE"
output:
html_document: default
include-before:
- '$\newcommand{\coloneqq}{\mathrel{=}}$'
---
## Use "Defined by" Symbol
$$sin(x) \coloneqq \frac{opposite}{hypothenuse}$$
Output:
Background
RMarkdown is build around pandoc, which performs most of the format conversions. Pandoc creates PDF via LaTeX (by default), and will simply include any raw LaTeX commands which are given in the source. When seeing \usepackage{mathtools}, the package is not parsed, but the command is simply added verbatim to the intermediate LaTeX. However, when exporting to HTML, it wouldn't make sense to pass through LaTeX commands, so any such command will simply be omitted from the output, so any \usepackage in your document won't effect the HTML output.
Alternative solution
If you are using very complex LaTeX-packages, then you could consider setting up a complex pipeline to still use it: E.g, one could use a pandoc filter to extract all equations, compile each equation as a separate document, and then convert the resulting PDF to SVG. Finally, that SVG can then be included in the HTML output. This is non-trivial and probably not worth the effort. A similar approach is recommended to include TikZ pictures.