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}
}
Related
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')
```
title: File Reading
output: html_document
params:
user1: "C:/Users/myDir/Desktop/apples.jpeg"
user2: "C:/Users/myDir/Desktop/oranges.jpeg"
Lets say I have the following file paths set in params in a Rmardown file. Now I set a separate chunk for each file as follows:
```{r}
image_read(params$user1)
```
```{r}
image_read(params$user2)
```
Now lets say I want to knit the document but the path I have specified for user2 is not available. So I updated my chunks and added the following so if path is not available or correct, the chunk is not evaluated.
```{r, eval = file.exists(params$user2)}
image_read(params$user2)
What I want to do is to somehow specify if file does not exist then upload another image from a default path that I have specified in a separate chunk at the top of my file
```{r}
default_image <- "C:/Users/myDir/Desktop/default.jpeg"
```
So essentially whenever a file path is missing, I want to replace it with this default image. Any help would be appreciated
In this case a simple if-else statement would solve it. If you are going to run it multiple times it might be worth it packing it into a function.
---
title: "test conditional chunks"
output: html_document
params:
user1: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts.png"
user2: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts5.png"
default: "path_to_default"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(magick)
```
```{r}
# basic example
if (file.exists(params$user1)) {
image_read(params$user1)
} else {
image_read(params$default)
}
```
```{r}
# packing into a function
image_read_with_default <- function(path, ...) {
if (file.exists(params$user1)) {
img <- magick::image_read(params$user1, ...)
} else {
img <- magick::image_read(params$default, ...)
}
return(img)
}
```
```{r}
image_read_with_default(params$user1)
```
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:
I am preparing R markdown document and I would like to display postgre sql codes nicely in R markdown document of course the codes should only be displayed not runned. How can I do that? . Below I put some code examples.
Thank you.
title: "POSTGRE sql "
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r1, eval=F}
with data as (select * from (values
('03-05-2019'::date,'{"color": true,"view": [181] ,"school":
[805,812,852,856,857]}'::jsonb),
('06-08-2019'::date,'{"color": false,"view": [184,185],"school":
[805,855,859]}'::jsonb),
('04-07-2019'::date,'{"color": true,"view": [184,185,189],"school":
[855,859]}'::jsonb)
) as v(published_date,attributes))
```
In the header add this to your output
output:
html_document:
highlight: pygments
and in the body use this line
{r, engine = 'sql', eval = FALSE}
I am not sure the major differences in SQL and PostgreSQL, but the syntax highlighting does work.
Here is a link to show other languages you can use with pygments
http://pygments.org/languages/
I would like to specify text (specifically a div tag) before and after some R code in a Rmd file using github_document. I can't seem to get both the before and after to work.
For example knitting the following:
---
title: "Untitled"
output: github_document
---
```{r setup, include=FALSE}
library(knitr)
knit_hooks$set(toggle = function(before, options, envir) {
if(options$toggle){
if(before) {
print('<div id="hideMe">')
} else {
print("</div>")
}
}
})
```
## GitHub Documents
```{r pressure, echo=TRUE, toggle=TRUE}
# Here's some
```
Produces:
Untitled
================
GitHub Documents
----------------
``` r
# Here's some
```
If I delete the else part:
---
title: "Untitled"
output: github_document
---
```{r setup, include=FALSE}
library(knitr)
knit_hooks$set(toggle = function(before, options, envir) {
if(options$toggle){
if(before) {
print('<div id="hideMe">')
}
}
})
```
## GitHub Documents
```{r pressure, echo=TRUE, toggle=TRUE}
# Here's some
```
It works as expected, and I get the before section:
Untitled
================
GitHub Documents
----------------
<div id="hideMe">
``` r
# Here's some
```
If I use !before in the if statement, the trailing </div> shows up. But I have been unable to get both the leading and trailing sections to show up. Any suggestions to get both before and after text to show up?
Don't print in chunk hooks, return the text that is to be added to the document:
In knitr, hooks can also be used to insert texts into the output. To do this, the hook function must return a character result. [Source]
The most likely explanation for the behavior you observed is that in R, "if returns the value of the expression evaluated" (see ?Control section "Value").