I would like to have a custom numbering scheme in a header in bookdown. That is, I have a (sub) section that has the chapter number, a section number, and maybe an additional word or phrase. So, I would like to have this number for the fifth element in chapter 2 be
HW 2.5 Header title
where the phrase is HW, as in homework problems for a textbook. I would like to generate this with the original markdown ##### Header title, for instance. The problem that I have is that the default markdown numbering continues from the previous section. For example, the MWE of the RMD file below:
---
title: "A Book"
author: "Frida Gomam"
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::gitbook:
css: "style.css"
---
# Chapter 1
## Content
This is where I write my content
## More content
Even more
## Homework {-}
##### Named problem
Problem text
##### Another problem
I have been able to edit the CSS file style.css to give me a few different wrinkles on this, but still haven't been able to figure out the correct syntax, etc. Here's a CSS that I know makes the text blue and adds the necessary appendage to the front:
h5 {
color: blue;
content: ;
}
h5:before {
counter-increment: 5;
content: "HW " counter(h1)": ";
}
but I haven't been able to figure out how to remove the original numbers, or why the counter on h1 seems to be zero instead of 1.
Related
I'm trying to generate a html with a table of contents using the YAML header in R Notebook. I've found answers to this question (e.g., Adding a table of contents in an r notebook) but the code still isn't generating a toc on the html. I have specified headers using #, ##, etc
---
title: "CMiller_SentencesTask_Analysis"
author: "Chantal Miller"
output:
html_notebook:
toc: yes
---
Resolved! I forgot to place a space between the heading names and the hashtag e.g.,
Heading 1
rather than
#Heading 1
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)
```
I'm sure this is already out there but I can't seem to find it. How can I change the font size and spacing for the title in an R markdown document compiled as a pdf?
Thanks!
I'm not sure exactly how you want the document to look, but here are some ways to control spacing and fontsize with Latex tags. In the rmd document below:
The initial \vspace{5cm} adds space above the first line of the title. \vspace{0.5cm} adds space between the two lines of the title.
\LARGE and \Large give different font sizes on different lines of the title.
| at the beginning of each line of the title allows a multi-line title.
If you want a separate cover page, \newpage at the beginning of the main document will start the main document text on a new page after the title page.
---
title: |
| \vspace{5cm} \LARGE My Title is really long and takes up several lines of text
| \vspace{0.5cm} \Large My Title is really long and takes up several lines of text
author: "eipi10"
date: "5/16/2017"
output: pdf_document
---
\newpage
Document text here.
For a smaller title section, the following may be helpful. It builds on eipi10's answer but with two modifications:
the vspace{} commands include negative values to shrink white space
the fontsize code uses the more cumbersome begin{} and end{} syntax because, with simpler code like \normalsize{}, I found extraneous braces appeared around the title, name, etc.
---
title: \vspace{-0.75cm} \begin{normalsize} My Title \end{normalsize} \vspace{-0.5cm}
author: \begin{normalsize} My Name \end{normalsize}
date: \begin{normalsize} 5/16/2017 \end{normalsize}
output: pdf_document
---
I'm creating a RMarkdown document that I want to export in MS Word with RStudio.
I want a table of contents and numbered headings. Here is my sample markdown document:
---
title: "Test"
author: "Ben"
date: "`r format(Sys.time(), '%d/%m/%Y')`"
output:
word_document:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Header 1
## Header 2
## Header 2
### Header 3
This produces the following word doc:
That's a good start. Following this tutorial, I edited the heading styles of the output document in Word to make them numbered.
I also changed the Table of contents title heading so that it's based on normal text and not another heading, otherwise the table of contents title gets numbered as well.
I saved the modified document in a template folder and added it as reference in the markdown header:
---
title: "Test"
author: "Ben"
date: "`r format(Sys.time(), '%d/%m/%Y')`"
output:
word_document:
toc: yes
reference_docx: "../templates/word-styles-reference-01.docx"
---
Here is the output:
Now, I want a page break after my table of contents, so I followed this other tutorial and changed my Heading 6 so that it is white, very small, based on the normal style and adds a page break afterwards.
The new markdown file looks like this:
---
title: "Test"
author: "Ben"
date: "`r format(Sys.time(), '%d/%m/%Y')`"
output:
word_document:
toc: yes
reference_docx: "../templates/word-styles-reference-01.docx"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
###### Page break after table of contents
# Header 1
## Header 2
## Header 2
### Header 3
And here is the output:
I now have my page break but the Heading 6 title is numbered by Word and thus my first title is numbered 2.
In the end, it's either:
a word question: how do I modify the style of Heading 6 so it's not numbered?
an R question: how can I add a page break without using word headings?
Following on from the Blockquote debate. I found the following solution:
R Markdown
add a block quote with a space
>
Word template
The block quote is formatted as Block Text style in Word.
Edit the Block Text style
small font (I use size 3)
change the paragraph settings
no space before/after
tick "page break before"
There is a few needs for you : first, page break AND unumbered title (see steps 3 & 4), then, maybe you want to adress the size of your titles (see bonus steps) and maybe there is a necessary last stuff to indicate to MS word (see steps 6). In every case, you should manage your word template properly for made a page-break with header 6 and I guess you've miss a step (I translate from the French interface of MS word, maybe there is a few differences in the English version) :
1) First, open your 'reference_docx' in MS word (which is, for you : "../templates/word-styles-reference-01.docx"). It's a good idea to save a copy of that by security.
2) Define a style for header 6 by right-clicking on it in the 'ribon of styles', then select 'modify'.
3) In the dedicated windows for indicate your 'header 6 style' (first case will be 'name : Title 6'), click on 'Format' button ==> then 'numbering' (or 'numbers') ==> then select 'none' ==> Then validate your choice.
4) In the same dedicated windows for modifying styles of 'title 6', click again on 'Format' ==> then on the 'paragraph' button ==> then the second tab named 'chaining' (or 'linking', in french 'enchaînement') ==> click on 'page break before' ==> then validate.
5) Validate your modifications and save your reference word document.
Try at this point to knit your rmardkown to a word output. In your Rmarkdown document, you should made your first notes page with this : ###### [and a space].
Note that if you properly manage your word_template, you gain access to the possibility of a page break with an unumbered title like that : "###### NOTES^[Page left empty.]", wich is better if your page break is a "notes-pages" and not a page-break. If your page-break is only a single 'note-page', you can't define a police-size of 1 for the title.
Bonus step : If you want ONLY a page-break without title, you should define a size of 1 for the police of the title 6. Indicate this in the first screen of 'modifying your title 6' (the one wich begin by 'name : Title 6', after you right-click on your title 6 and then 'modifying'). If you want a note page with a title like 'Notes', you can't define a police size of 1 for your page break.
If the 'title style 6' don't working for made an unumbered page-break, go in the final step (good luck).
5) It's sometimes necessary to go in the list-tools which is in the 'paragraph' ribbon (left to your 'styles ribon') and click on the symbol with a list of '1 / a / i' (the one for define a 'list of several levels') ==> then click on 'define a new list of several levels' ==>
define precisely every title(s) styles with this tools, by clicking on every number of titles at the left (1 to 6 cause you're working in rmarkdown so you had only 6 choices of titles), then click on 'plus' (or 'more') ==> then indicate precisely what you want for this title in the right-part of the 'list of several levels' panel (typically you want to 'restart the list' with every levels, and the page-break 'title 6' maybe need to adress that, you maybe want to center or made indentation for your titles at every steps of the list).
Congratulations, you are free for ms word for a while (except that your titles are in the microsoft blue, and you should write your title in black by playing with every single styles by clicking on 'modifying' for every of them, you should had the same police styles in most of your titles, etc.). Microsoft torture is so elaborate...
As suggested by Frost_Maggot in the comments,
Maybe try modifying a different style in Word that is shared with R
Markdown. If you are not using block quotes in your R markdown this
might relate to quote in the MS Word stylings?
This indeed works for me as I don't use the quote style in my document.
I am creating a presentation with Rstudio using knitr and I want to use the types of the slides to generate a table of contents slide in the beginning. So if I have the following code:
Some fun lecture
========================================================
author: some guy
date: in the future
Table of content slide
========================================================
Here I want the table of content based on the following slide types
Some stuff 1
========================================================
type:section
Some very important stuff
More detail on stuff 1
========================================================
type:subsection
Did not get enough of stuff 1!? Here is more :D
Stuff 2
========================================================
type:section
There are other kinds of stuff?
Prompt slide
========================================================
type:prompt
Do not display prompt or alert types in TOC
So basically I want a table of content slide after the title slide looking something like:
Some stuff 1
More detail on stuff 1
Stuff 2
Is this available by default or do I need some extra CSS style in the beginning to handle this?
To generate a table of contents, include a document header with the option "toc:true" at the beginning of the document. Level 1 headings are indicated by #, level 2 headings by ##, and so on. Below is an example . The "prompt" option is only relevant for R code chunks. R code chunks are not listed in the table of contents.
This will, however, only produce a html document. If you want a Latex-like presentation with individual slides, you can replace html_document with beamer_presentation. In the beamer presentation, only level 1 headings are listed in the table of contents.
---
title: "Sample Document"
author: "Author"
output:
html_document:
toc: true
---
# Some stuff 1
Some very important stuff
## More detail on stuff 1
Did not get enough of stuff 1!? Here is more :D
# Stuff 2
There are other kinds of stuff?
```{r, prompt=TRUE}
summary(iris[, "Sepal.Length"])
```
For more info on Beamer presentations in Rmarkdown, see http://rmarkdown.rstudio.com/beamer_presentation_format.html. A similar question has been answered here.