Quarto: `project: type: book`-like project organization, but for articles? - r-markdown

I'm writing a lengthy article using quarto and multiple profiles (think full & short versions), which use different subsets of content provided in individual *.qmd files.
The quarto project type book seems perfect for this, as different book: chapters: * definitions in profile-specific _quarto-<PROFILE>.yml nicely do the job of profile-individual input file selection and produce joined output.
The PDF/LaTeX output, however is distinctly book-like (uses KOMA-Script's scrreprt) and not easily forced towards an article layout - see demonstration code below.
Includes are a way out but distinctly less elegant then in-header input file definition.
Is there currently a way to achieve book-like project organization for articles? Is that on any roadmap? May it be put there?
Demonstration/Solution Attempts
Base line behavior:
Create book project:
SHELL> quarto create-project mybook --type book
Render:
SHELL> cd mybook
SHELL> quarto render --to=pdf
Inspect pdf in _book subdirectory - nice but bookish.
Attempt to force style change by switching to KOMA-Script's scrartcl document class:
Edit _quarto.yml as follows:
format:
html:
theme: cosmo
pdf:
# documentclass: scrreprt
documentclass: scrartcl
Render:
SHELL> quarto render --to=pdf
ERROR:
...
compilation failed- error
Undefined control sequence.
<argument> \chapter
*{Preface}\label {preface}
l.187 \chapter*{Preface}\label{preface}}
see /tmp/mybook/index.log for more information.
--> chapter is undefined in the context of scrartcl
Setting project: type: default instead of book leads to loss of chapter integration into a single output PDF and all *.qmds are being rendered into individual output files.

Related

Bookdown: Password protect a *single* page/chapter in HTML

I am producing a Tutorial Workbook for my class using bookdown. I have a large class (500+), and so have a couple of other people helping me with the course.
So I would like to produce the answers for these tutorial questions.
I can produce a whole new document... but then it would be tricky to (automatically) cross-reference the exercise numbers.
So I wondered: Is there a way to password-protect a single page, or a single chapter, in bookdown? (Thinking HTML here; in PDF I can just not include that page/chapter.)
Is this possible? If so how? If not... any other ideas...?
P.
This solution does not use a password, but since you say that for PDF you can simply distribute a version that does not include the material in question, perhaps the following simple approach might help
Inspired by this question on how to conditionally input material as well as the option to use parameters in Rmarkdown, consider two Rmarkdown files:
main.Rmd, which contains what you want to show everyone.
protected.Rmd, which should only be shown to some people.
These files look as follows:
main.Rmd:
---
output: html_document
params:
include:
label: "Include extra material?"
value: ""
input: select
choices: [True, False]
---
```{r, include=FALSE}
print(params)
show_all <- as.logical(params$include)
```
```{r conditional_print, child="protected.Rmd", eval = show_all}
```
protected.Rmd:
Hello World!
Assuming you are in RStudio, if you choose "Knit with parameters" on main.Rmd, you will be asked to select either TRUE or FALSE from an interactive dropdown. If and only if you choose TRUE, the output will include "Hello World". More generally, code blocks with eval = show_all will only be displayed when selecting to include extra material. Therefore, you can of course have multiple sections (each contained in a separate .Rmd file) that are only conditionally included.
In this way, you could knit the same document twice: once with questions only, and once with questions and answers both. Since this is the same for both pdf and html, it also gives you a consistent workflow for both of these output types.

Tufte RMarkdown Document - Self Contained Option?

I would like to distribute the HTML variant for the theme to be a self-contained HTML file. Is that possible? The following YAML header built the gorgeous file, but appears to have dependencies via the form of cache and files folders.
---
title: "Tufte Handout"
subtitle: "An implementation in R Markdown"
author: "JJ Allaire and Yihui Xie"
date: "`r Sys.Date()`"
output:
tufte::tufte_html:
self_contained: true
---
I think you may not be looking closely enough at this, or looking from the wrong angle:
Self-contained asks for just that;
what your question notes are cache files
which are "merely" used to produced said self-contained file more easily.
Should work for tufte, tint and everything else running through the same machinery.

Chapter(s) before table of contents in Bookdown PDF output

I'd like for a chapter to appear before the table of contents (but after the title page) in the pdf_book output of Bookdown.
One way to do this is to add the chapter to a .tex file and and link it using before_body:. However, this means the chapter will not appear in gitbook (which I also need). I'd prefer not to keep both a .tex and .Rmd version of the same chapter.
An ideal solution would be if the chapter could be kept in a .Rmd file, and its contents extracted into the before_body for pdf_book. That way it's still available for gitbook. Though I'm not sure how I might do that, or indeed if it's possible?
Is there a solution? Or is it exceeding the limits of Bookdown's flexibility?
Any help would be greatly appreciated, thanks!
One can trigger ToC creation manually in the document, which gives more control over its placement. Of course, automatic table of contents creation should be disabled:
---
title: "MWE"
output:
bookdown::pdf_book:
toc: False
---
```{r child = 'file-you-want-to-include.Rmd'}
```
```{=latex}
% Trigger ToC creation in LaTeX
\tableofcontents
```
# Rest of your document starts here
The downside is that this only works with PDF output, not HTML.

Use additional Latex packages for math expressions in RMarkdown `output = "html_document"`

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.

Compiling reveal.js-based R markdown presentation without RStudio

Fairly recently, RStudio has added support for beautiful reveal.js-based HTML presentations generated from RMarkdown (with some extensions). These are different from earlier HTML presentation formats provided by the rmarkdown R package, which relied on ioslides or Slidy.
Is it possible to compile such a presentation to HTML without having recourse to RStudio? I.e. is there a pure R function which will, given an R presentation source file, generate the same result as the IDE?
P.S. I suppose the underlying R package doing the conversion is revealjs by JJ Allaire, but on its own, it doesn't recognize some of the syntax extensions (e.g. those for customizing appearance by putting css: custom.css under the title of the first slide), which makes me think there must be an additional wrapper around it.
You can use the standard rmarkdown::render() function with the revealjs::revealjs_presentation format in the YAML header. Resources like custom.css are referenced relative to the Rmd location so there is no need to specify these within the render() step.
User notes on specific implementation diffs between native RStudio & scripted version
It's not a drop-in replacement though. The revealjs package, as it's available from CRAN, ships with a different (newer) version of the reveal.js library than the one used internally by RStudio (3.2 vs 2.4 as of March 8th, 2016). The default settings (e.g. transitions) are also different, so they need tweaking.
Circumspection is also warranted with the version of pandoc used as the workhorse for the Markdown → HTML conversion, as RStudio may internally be using an older one with different templates. All of this means that you may need to rework your customizations (e.g. css tweaks).
There are also syntax differences -- the metadata can't go under the first heading of the presentation (i.e. the title):
Presentation title
==================
author: Foo Bar
css: custom.css
Instead, they have to be put in a traditional RMarkdown YAML header:
---
author: Foo Bar
css: custom.css
---
I'm not sure whether per-slide special settings like incremental: true (which are put under the respective slide's heading in the RPresentation format) are recognized at all.
I use a script like this that I source to render my reveal.js markdown slides:
library(rmarkdown)
library(revealjs)
file.name <- "index"
path.to.file <- "."
rmarkdown::render(file.path(path.to.file, paste0(file.name, ".Rmd")),
revealjs_presentation(theme="white", highlight="tango", slideNumber = TRUE),
encoding = "UTF-8")
# Open the generated HTML file in the browser
browseURL( file.path(path.to.file, paste0(file.name, ".html")))