I am preparing a tutorial for a course and I want to change the colour of the error to be red. I am using BookDown and gitbook as my output format. But I found that the option class.output is not working. I want to add a class to the output for the error message I get. How can I do that? You can use this as an example:
---
title: "Test Book"
author: "therimalaya"
site: bookdown::bookdown_site
output: bookdown::gitbook
---
# Hello World
```{r, error = TRUE, class.output="red"}
rnorm(-10)
```
This works if there is no error.
class.output is not applied to errors (see here).
Following this answer, I suggest you to use an error hook:
```{r error-hook, echo=FALSE}
knitr::knit_hooks$set(error = function(x, options) {
paste0(
"```{",
ifelse(is.null(options$class.error),
"",
paste0(" .", gsub(" ", " .", options$class.error))
),
"}\n",
x,
"\n```"
)
})
```
Now, you can use a "new" class.error option in your chunk.
```{r, error = TRUE, class.error="red"}
rnorm(-10)
```
Feel free to open a feature request here.
The ability to use custom CSS classes for errors, warnings, and messages was just added to knitr, so you will be able to use the following syntax.
```{r error = TRUE, class.error = "bg-danger text-danger"}
rnorm(-10)
```
Here I'm using Bootstrap classes, but you can pass any class(es) you need to class.error. The chunk options class.message and class.warning also work. Note that class.output is applied only to standard code outputs.
Related
This Github repo, hosts a .qmd file of my dissertation template. In config/preamble.tex I've set \onehalfspacing and \linespread{1.5} which I thought would affect only plain text and not also code blocks.
Is it possible to change monofont spacing individually (or inversely, set space for mainfont only)?
More quarto way to do this actually modifying the knitr chunk hook.
---
title: ""
format:
pdf:
include-in-header:
text: |
\usepackage{lipsum}
\usepackage{setspace}
\onehalfspacing
\linespread{2}
df-print: kable
highlight-style: zenburn
fontsize: 12pt
geometry: margin=1in
---
```{r}
#| label: setup
#| include: false
chunk_hook <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
x <- chunk_hook(x, options)
paste0("\\linespread{0.5}\n", x, "\n\n\\linespread{2}")
})
```
## Different linespacing for text and code
When you click the **Render** button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
```{r}
library(dplyr, quietly = TRUE)
mtcars %>%
group_by(am) %>%
summarise(
disp = mean(disp),
mpg = mean(mpg)
)
```
\lipsum[1]
Here I have used linespace 0.5 for code and linespace 2 for text. Change these as you need.
Code blocks are defined in Shaded environment. So, the fix was simply redefining it in preamble.tex using singlespace environment and \linespread{1}:
\renewenvironment{Shaded}
{\begin{snugshade}
\begin{singlespace}
\linespread{1}
}
{\end{singlespace}
\end{snugshade}
}
Basically i want to build a random multiple choice question generator with R Markdown. For this task there need to be equations in the code chunks of the markdown.
The following works like a charm and gives the equation "greekbeta = 1"
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
"$\beta = 1$"
```
In contrast, this will not work when some other math symbol is used, for example:
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
"$\sum_{n=1}^{\infty}$"
```
After pressing knit, an error occurs (unfortunately the error message is in german, basically this: "'\s' is an unknown escape-sequence within the string starting with "$/s"").
I am very puzzled by this, especially because for example \frac{1}{2} works, but \hat{x} does not. Equations in the "normal" markdown text are no problem at all. But for my task, the equations have to be in the code chunk sections.
Does someone has a workaround for this problem? I tried using "$\hat{x}$" or even "$$\hat{x}$", but the error message is still the same.
I am using pandoc 2.11.4, R 4.1.0 and knitr 1.33
Use cat() and escape the escapes.
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
cat("$\\beta = 1$", '\n\n')
cat("$a^2+b^2 = c^2$", '\n\n')
cat("$\\sum_{n=1}^{\\infty}x_i$", '\n\n')
```
I have successfully included in an R/exams .Rmd file several graphics made in TikZ. The same does not happen when I try to include a plot under pgfplots using include_tikz(). Whenever \begin {axis} and \end {axis} are included, beware of the error "! LaTeX Error: Environment axis undefined".
In the RStudio console the legend appears: "This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format = pdflatex) restricted \ write18 enabled.entering extended mode", even having enabled in TexStudio write-18. None of these messages appear when I include other TikZ graphs other than pgfplots.
Any TikZ graph works when run in TexMaker or TexStudio, which indicates that it is not a problem of the absence of LaTeX libraries or packages.
I include a part of my code, adapted from https://www.latex4technics.com/?note=1HCT:
```{r datos1, echo = FALSE, results = "hide"}
library(exams)
typ <- match_exams_device()
image01<-'
\\begin{tikzpicture}
\\begin{axis}[legend pos=south east]
\\addlegendimage{empty legend}
\\addplot {sqrt(x)};
\\addplot {ln(x)};
\\addlegendentry{\\hspace{-.6cm}\\textbf{A title}}
\\addlegendentry{$\\sqrt{x}$}
\\addlegendentry{$\\ln{x}$}
\\end{axis}
\\end{tikzpicture}
'
```
```{r grafica01, echo = FALSE, results = "asis"}
include_tikz(image01, name = "grafiko1", markup =
"markdown",format = typ,library = c("arrows"),packages =
"booktabs",width = "7cm",header = "\\usepackage{/home/r-
exams/Documentos/NuevoRStudio/Rmarkdowns/
Esqueleto/exercises/schoice/
LaboratorioTikZ/3dplot}")
```
The answer is right there in your question title. You need to include the pgfplots package:
include_tikz(image01, packages = "pgfplots", ...)
The other packages, library, and header arguments from the call in your question are not needed.
The reason is that for include_tikz() you just use the {tikzpicture} code while in the full .tex file that you linked you additionally have:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
...
\end{tikzpicture}
\end{document}
Note the \usepackge{pgfplots} in the second line!
I have added a custom language engine on Knitr according the documentation.
require(knitr)
knit_engines$set(upper = function(options) {
code <- paste(options$code, collapse = "\n")
if (options$eval)
toupper(code) else code
})
And when I add text a following command to Rmd-file I can run a current chunk in RStudio.
```{upper}
Hello, **knitr** engines!
```.
HELLO, KNITR ENGINES!
But If I try to Knit a whole file to a html-page I can't see the result of chunked code. And I will get the following warning message on R Markdown tab.
Warning message:
In get_engine(options$engine) :
Unknown language engine 'upper' (must be registered via knit_engines$set()).
So how can I register the engine so that the program will see it later on?
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::knit_engines$set(upper = function(options) {
code <- paste(options$code, collapse = "\n")
if (options$eval)
toupper(code) else code
})
```
```{upper}
Hello, **knitr** engines!
```
I'm compiling a report using Papaja and Rmarkdown, and I want to highlight various text throughout. Doing so with latex or pure Rmarkdown is easy, but I'm receiving an "undefined control sequence" error when I compile the report to PDF using Papaja's application of Knitr.
A similar question about text highlighting within a single Rmarkdown file was answered here: RMarkdown / pandoc fails to knit Pdf with latex color commands. I'd like to know if an answer exists for multiple Rmarkdown files using Papaja.
I'll include a minimal example below.
1) File called papaja_file.Rmd
---
# https://crsh.github.io/papaja_man/index.html
title : "Some Title"
shorttitle : "HEADER"
author:
- name : "Name R Here"
affiliation : "1"
corresponding : yes # Define only one corresponding author
address : "Include addresss"
email : "randomemail#usa.edu"
affiliation:
- id : "1"
institution : "Any University"
author_note: |
....
abstract: |
Text here for abstract.
keywords : "Keyword1, keyword2, keyword3"
bibliography : ["references.bib"]
figsintext : no
figurelist : no
tablelist : no
footnotelist : no
lineno : yes
lang : "english"
class : "man"
output : papaja::apa6_pdf
---
```{r load_packages, include = FALSE}
library("papaja")
```
```{r analysis_preferences}
# Seed for random number generation
set.seed(42)
```
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.path = 'figures/', echo = TRUE, warning = FALSE, message = FALSE)
```
```{r child = 'child_document.Rmd'}
```
\newpage
# References
```{r create_references, echo = F}
r_refs(file = "references.bib")
```
\setlength{\parindent}{-0.5in}
\setlength{\leftskip}{0.5in}
Notice that it references a single child Rmarkdown document.
2) The child document with text, called child_document.Rmd
---
output:
pdf_document:
includes:
in_header: preamble.tex
---
One sentence without highlighting. \hl{Another with highlighting.}
3) The preamble, called preamble.tex
\usepackage{color}
\usepackage{soul}
\definecolor{lightblue}{rgb}{0.90, 0.95, 1}
\sethlcolor{lightblue}
Knitting the main papaja file produces the error. Removing the \hl command within the child document allows the pdf to compile without issue.
Result after putting YAML header within the main papaja document rather than the child:
The YAML header in your child document is not evaluated, c.f.
The master document, for example, consists of the YAML front matter and includes the children, which are themselves R Markdown documents without a YAML front matter.
https://crsh.github.io/papaja_man/tips-and-tricks.html#splitting-an-r-markdown-document
But you can include your preamble.tex in you master file using:
output:
papaja::apa6_pdf:
includes:
in_header: preamble.tex
c.f. https://github.com/rstub/stackoverflow/commit/a92a67d4721ee9c06e995b08adbf8cb89daebcb4
Result: