I'm working in Rmarkdown and knitting to PDF. I want to start a paragraph with (a) without creating a list. Adding \(a) works, but it highlights the rest of my Rmd file text, making it hard to read. Is there another way to indicate that I do not want to create a list in Rmarkdown?
---
title: "Untitled"
output:
pdf_document: default
html_document: default
---
This is the first paragraph.
```{r, include=FALSE}
1+1 # nice highlighting before problem
```
(a) This should be the second paragraph. (b) But it is interpreted as a list.
\(a) This slash works to keep the text as a paragraph, but it highlights all remaining text in my Rmd file, which makes it hard to read. (b) Is there another method to use (a) without creating a list?
Here is a good list, but still wrong color in Rmd:
(a) One
(b) Two
```{r, include=FALSE}
1+1 # bad highlighting after problem
```
Try putting (a) in <span> like
<span> (a) </span> This should be the second paragraph.
It's a bit hacky but it works for html & pdf and it fixes the highlighting problem.
Instead of the element placing a on the previous line works. If there is actual text on the previous line then ending the line with two spaces also works.
Related
Is there a way to use a custom highlighting style in rmarkdown?
Manual is a bit silent regarding that and the closest to that is to make a full blown custom css file for everything, that would however work only for html_document and not for pdf_document (see https://bookdown.org/yihui/rmarkdown/html-document.html#appearance-and-style )
The newer versions of Pandoc support this:
http://pandoc.org/MANUAL.html#syntax-highlighting
but when anything else but one of the default pandoc styles is specified, rmarkdown throws an error.
For example, when I download zenburn.css from highlight.js library, modify it and want to use it:
```
title: Some title
output:
html_document:
theme: readable
highlight: zenburn.css
```
I get:
Error in match.arg(highlight, html_highlighters()) :
'arg' should be one of “default”, “tango”, “pygments”, “kate”, “monochrome”, “espresso”, “zenburn”, “haddock”, “textmate”
Calls: ... -> pandoc_html_highlight_args -> match.arg
Execution halted
For posterity, since this took more time than it should:
The problem
Answers from #martin_schmelzer is correct (didn't test #tarleb's solution, since rmarkdown doesn't supposedly play well with Pandoc > 2.0 see: https://github.com/rstudio/rmarkdown/issues/1471 ). However, when you write echo=TRUE on chunk, the output is code is not tagged as R code and because of that different rules apply for it. In HTML it means that it has white background and with PDF it is formatted through verbatim environment only. For example, the markdown of following:
```{r, echo=TRUE}
foo = "bar"
foo
```
will be:
```r
foo = "bar"
foo
```
```
## [1] "foo"
```
And while the first chunk will be highlighted, the second will follow only in text color, but background will always be white. This is very problematic with darker themes, since they often have very light text color and this does not play nicely with white background. See: https://eranraviv.com/syntax-highlighting-style-in-rmarkdown/ for overview of rmarkdown highlighting styles.
HTML solution
This is made more complicated with switch between highlight.js and pandoc highlighting. If highlighting is not specified, highlight.js is used with associated tags. There the highlighting is done through external css and .js library, which are then (I presume) hashed into the HTML to make it stand-alone. So no luck there.
If some highlighting style is used however, then pandoc highlighting is used. I.e.,:
---
title = "Foo"
output:
html_document:
theme: readable
highlight: zenburn
---
In this case, there IS solution. Looking at the HTML output, there is this structure:
<style typetext/css">
pre:not([class]) {
background-color: white;
}
</style>
This means that whenever there is no style in specific code chunk (applies only to the "echo" chunks since by default rmarkdown assumes R), the background is white. This behaviour can be changed just by including following chunk in the .Rmd file:
```{css, echo = FALSE}
pre:not([class]) {
color: #333333;
background-color: #cccccc;
}
```
and their behaviour can be fully specified accordingly. Here the color and background as reverse of the zenburn style. The output looks like this:
Before:
After:
PDF solution
With PDF, it is a little bit easier to find the problem, but little bit more complicated to solve it. If one looks at the .tex file, you can see that while all the chunks with an actual code have a lot of going on around them, the echo chunks are wrapped only in a simple verbatim environment. The result looks like this:
While it is more readable than the HTML output, since it does not share the text color defined by highlighting style, it kind of blends into the text and creates and breaks the feeling of uniform style across outputs. The solution is, as mentioned in previous answer, to use:
---
title: "Foo"
output:
pdf_document:
highlight: zenburn
includes:
in_header: highlight_echo.tex
---
And a following construct that utilize package framed which is already included:
\usepackage{xcolor}
\definecolor{backgroundecho}{HTML}{cccccc}
\definecolor{textecho}{HTML}{333333}
\let\oldverbatim=\verbatim
\let\oldendverbatim=\endverbatim
\makeatletter
\renewenvironment{verbatim}{
\def\FrameCommand{
\hskip-\fboxsep
\color{textecho}
\colorbox{backgroundecho}
}
\MakeFramed{\#setminipage}
\oldverbatim
}
{
\oldendverbatim
\vskip-2em\#minipagefalse % The size required for this negative space is probably in some variable
\endMakeFramed
}
\makeatother
This redefine the verbatim environment to have colored background and colored text. The result is unified formatting of the "echo" chunks:
So thanks again #tarleb and #martin_schmelzer, I wouldn't be able to solve it without you!
It appears that you are trying to use a CSS file as a highlight style. This won't work (in general), as pandoc expects a highlighting styles to be defined using a special JSON format. To use a modified zenburn, one will have to create a new style file via pandoc --print-highlight-style zenburn > myzenburn.style, and then modify the new file myzenburn.style.
To use the new style, one must circumvent R Markdown by passing the necessary options directly to pandoc.
output:
html_document:
theme: readable
pandoc_args: --highlight-style=myzenburn.style
However, this will only work for non-HTML output formats, as knitr interferes whenever highlight.js can be used.
At least for HTML documents, you can simply include your customized styles using the css YAML option:
---
title: Some title
output:
html_document:
theme: readable
css: zenburn.css
---
Concerning PDF documents, you could check the intermediate TeX file. There you will find a block of commands that look like
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.96,0.35,0.01}{\textit{#1}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.93,0.29,0.53}{\textbf{#1}}}
These are the lines that define the code highlighting. The first one for example defines the color for comments. You could write a header.tex in which you redefine these commands using \renewcommand
\renewcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\renewcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
and include it in your document right before the body.
Here is an example in which we alter the highlighting of comments and keywords within the body:
---
title: Some title
output:
pdf_document:
keep_tex: true
---
```{r}
# This is a test
head(mtcars)
```
\renewcommand{\CommentTok}[1]{\textcolor[rgb]{0.96,0.35,0.01}{\textit{#1}}}
\renewcommand{\KeywordTok}[1]{\textcolor[rgb]{0.93,0.29,0.53}{\textbf{#1}}}
```{r}
# This is a test
head(mtcars)
```
When including code for a figure between two paragraphs in RMarkdown, the float resulting from the code might be placed at a different place of the document, leaving space between the paragraphs in the LaTeX/PDF output file.
The following minimal example illustrates the issue:
---
output: pdf_document
---
This is my first paragraph.
Next in the Rmd file would be the code for the figure.
The figure is correctly treated as a float by LaTeX, meaning that LaTeX will put it to the next suitable position.
```{r pressure, echo=FALSE, fig.cap="Example", out.width='0.5\\textwidth', fig.pos='B'}
plot(pressure)
```
The problem now is, that this leaves space to my second paragraph.
Usually, this should not be the case.
In pure LaTeX, there is a linebreak, of course, but no vertical space in addition.
If you put \vspace{-\parsep} at the start of the following text, you'll get the line break without the extra space. That is,
---
output: pdf_document
---
This is my first paragraph.
Next in the Rmd file would be the code for the figure.
The figure is correctly treated as a float by LaTeX, meaning that LaTeX will put it to the next suitable position.
```{r pressure, echo=FALSE, fig.cap="Example", out.width='0.5\\textwidth', fig.pos='B'}
plot(pressure)
```
\vspace{-\parsep}The problem is now fixed.
I don't see any other way to do this: knitr puts blank lines around the figure environment when it generates the LaTeX.
Is there anyway to format code in the "code" format offered by many websites like Stackoverflow in R Markdown.
I want to be able to do something like the following:
Any model that takes the form income ~ education + parental_income
...
I want to emulate that greyed out text in R markdown for ease of readability when I eventually knit the document.
One can use backticks or indenting by 4 spaces to generate the equivalent of the HTML <code> </code> tag block. Here is an Rmd document illustrating this behavior.
---
title: "Sample Document"
author: "bigNumber"
date: "12/5/2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
here is some example code written in R Markdown. If we want to enter a code block we use the backtick, such as `inc ~ ed + occ`.
If we want a code block with multiple lines, indenting by 4 spaces also works
# first line of code
for(i in 1:100) {
# do something here
}
More text after the code block ends.
...and the output. Unfortunately I have to include an image to show that it renders correctly.
```
If you'd rather not
indent your code block by four spaces, you can use
triple back-ticks on separate lines.
```
https://bookdown.org/yihui/rmarkdown/markdown-syntax.html#block-level-elements
html could work if you want to have a string with gray color as a background
Changing chunk background color in RMarkdown
I'm editing an R markdown file (.Rmd) that has a lot of R code blocks to move groups of those code blocks into "child" documents to simplify rearranging sections (if nothing else). As I convert sections to child documents, I would like to test the new child document without running the rest of the blocks and other children. However, when I use to comment out those sections, the R blocks still run (but RStudio makes the sections "look" as though they were commented out).
If I eliminate the preceding and trailing "```"s (i.e., the code block signifiers), the commenting works fine. However, as I said, I've got lots of code blocks and something like would be more convenient.
So, how do I comment out the R code blocks so they won't run?
In RStudio, if you highlight from (at least) one row above an R code chunk to (at least) the last row of the R code chunk,1 and then type ctrl-shift-C (in OSX or Windows) or command-shift-C (OSX only), RStudio will place html comment tags on the chunk.
For example:
```{r cars}
summary(cars)
plot(pressure)
```
After highlighting this and type ctrl-shift-C, this becomes:
<!-- ```{r cars} -->
<!-- summary(cars) -->
<!-- plot(pressure) -->
<!-- ``` -->
To selectively comment out multiple chunks, you can use the RStudio find/replace tool with the regex option checked. It takes two replacement steps (it can probably be done in one step, but I'm not sure how to do a regex to capture across multiple lines in RStudio).
Step 1: Comment out the first line of one or more chunks:
Find: (```{r.*)
Replace: <!--\1
Step 2: Comment out the last line of one or more chunks:
Find: (```)$
Replace: \1-->
1 You must include the row above the chunk in the highlight. Otherwise, RStudio will place R comment tags (#) at the beginning of each row of the chunk and the commented lines will appear as plain text in the output document.
In an Rmarkdown document, we can apply certain options to each R code chunk that determines whether the code inside will be run, printed, show error messages, etc.
To have a specific code chunk not run, use:
```{r cars, eval=FALSE}
summary(cars)
```
To have a specific code chunk not run or print into the created doc, use:
```{r cars, eval=FALSE, echo=FALSE}
summary(cars)
```
"TRUE" is used for the opposite effects and is the default.
If you have many code chunks you need to comment out, you can take the suggestion from #eipi10 (thanks) and use find/replace with the regex option selected. So, the find would be "(```{r.*)", and the replace would be "\1, eval=FALSE, echo=FALSE}" (without the double quotes).
I am trying to add a cover page which is already created in a PDF.
```{r echo=FALSE,out.width='8.27in',out.height='11.69in'}
knitr::include_graphics('CoverPage.pdf')
```
I want to change the margins of the first page only. Can you please help me with this?
The following was tried but it changes the margin of the whole document:
geometry: "left=3cm,right=3cm,top=2cm,bottom=2cm"
If you are fine with some bare LaTeX in your RMD file, then this can be easily achieved using the geometry package. Just define a newgeometry before the cover is included and restoregeometry afterwards (reads almost like English …).
---
output:
pdf_document
---
```{r, echo = FALSE, results = "asis"}
cat("\\newgeometry{left=3cm,right=3cm,top=2cm,bottom=2cm}")
knitr::include_graphics("CoverPage.pdf")
cat("\\restoregeometry")
```
\clearpage
Note the position of the page number. Restoring the margins was successful!
It is important to use the chunk option results="asis" and cat() to print raw LaTeX and to escape the backslashes with an additional backslash.