I am writing a report in R markdown (using pdflatex to convert it into pdf) and I am trying to set the style of the different headers (color, size, interline before and after).
I am using this formalisation :
#H1
##H2
###H3
I have the basics of Markdown and LaTeX but I'm far to be an expert. Is there a way to do it without creating a LaTeX template ? For exemple with some YAML code in the head of the document ?
Mine for now :
---
title: "Guide"
output:
pdf_document:
fig_caption: yes
highlight: tango
toc: yes
toc_depth: 2
documentclass: report
---
Or by changing the R options ?
```{r set-options, echo=FALSE, cache=FALSE}
options(some stuff)
```
I've found some indications while searching that suggest it may be possible, but I could'nt manage to find any understandable exemple ...
Thanks for any feedback.
If you do not want to do any LaTeX, you could add your own CSS (see documentation) and set it that way, as in this answer.
You would then need to do: How to convert Markdown + CSS -> PDF?
However, your results may not be as faithfully rendered as if you create a LaTeX template.
Related
I use Bookdown with a pdf output.
In my document, I include images generally using the method
\![\label{imagelabel}image title](image_path.png).
I would like to know if it is possible, in addition to a title, to add comments to the image. I would like to see "Figure #: Image Title. My comments (e.g. this figure shows that...)", but that the comments are not displayed in the List of Figures.
Is this possible and if so, how?
Thank you in advance!
I don't use bookdown, but it's a close relative of pdf_document in rmarkdown. This works there:
---
title: "image.Rmd"
output:
pdf_document:
keep_tex: true
toc: true
---
```{r}
knitr::opts_chunk$set(dev='pdf')
```
\newcommand{\comment}[1]{}
\listoffigures
```{r theplot,fig.show="hide"}
plot(rnorm(1000))
```
![\label{thefig}This is the caption\comment{this is the comment}](image_files/figure-latex/theplot-1.pdf)
Interestingly, the comment doesn't show up in the .tex file, it was removed by Pandoc. If you actually do want to see the comment in the output, you can turn it on using something like
\newcommand{\comment}[1]{\textit{#1}}
in place of the definition above.
I need to update the color and font of the header (only header) in a R Markdown PDF file. I have found recourses on how to do this for the whole document, but can't find an answer for changing the headers only.
Thank you kindly!
---
title: "Untitled"
output: pdf_document
---
Simplified version of the solution provided by Grada Gukovic:
You can add simple LaTeX statements to your document via the YAML header header-includes, e.g.:
---
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage{sectsty}
- \allsectionsfont{\color{cyan}}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for
authoring HTML, PDF, and MS Word documents. For more details on using
R Markdown see <http://rmarkdown.rstudio.com>.
Result:
This is most useful for small additions like the one seen here. If you want to add more than a few lines of LaTeX code it is often easier to save them to an external file, say preamble.tex and include that via
---
output:
pdf_document:
includes:
in_header: preamble.tex
---
Other possible places are before_body and after_body, c.f. ?rmarkdown::includes.
There is no option to do this in rmarkdown::pdf_document. You can do this by modifying the .tex template being used using the sectsty package for latex.
For example the following changes the color of all headers to cyan:
Download the default latex template from here:
tex template
Open the template in Notepad and add the following lines on an appropriate place in the document preamble(I have them as lines nr. 200 and 201):
\usepackage{sectsty}
\allsectionsfont{\color{cyan}}
Save the modified file with the extension .tex (my file is called "Cyansections.tex") and put it in R's working directory.
Modify the header of the .rmd document:
---
title: "Untitled"
output:
pdf_document:
template: Cyansections.tex
---
If you want a different color or font consult this answer
and sectsty's manual Especially section 4 of the manual for chanhing fonts
I'm trying to build a book with R/bookdown and I'm facing a problem when producing a html version using tufte_html_book or html_book:
Error in split_chapters(output, page_builder, number_sections, split_by, :
The document must start with a first (#) or second level (##) heading
The book is correctly produced in gitbook or tufte_book2 output.
The document has four files index.Rmd, 01-Chap1.Rmd, 02-Chap2.Rmd, 03-Chap3.Rmd. I like to keep things clear, so index.Rmd only contains the YAML headers, plus some R code but no heading, which is probably what yields the error. Is there a way to make the html outputs of bookdown behave similarly to gitbook or pdf output?
Here is a small reproducible example:
Index.Rmd:
---
title: "Tufte Handout"
subtitle: "An implementation in R Markdown"
author: "JJ Allaire and Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
bookdown::tufte_html_book:
toc: yes
bookdown::tufte_book2:
toc: yes
---
```{r setup1, include=FALSE}
library(tufte)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(tidy = FALSE, cache.extra =
packageVersion('tufte'))
options(htmltools.dir.version = FALSE)
```
01-Chap1.Rmd
# Introduction
The Tufte handout style is a style that Edward Tufte uses in his books and handouts. Tufte's style is known for its extensive use of sidenotes, tight integration of graphics with text, and well-set typography. This style has been implemented in LaTeX and HTML/CSS^[See Github repositories [tufte-latex](https://github.com/tufte-latex/tufte-latex) and [tufte-css](https://github.com/edwardtufte/tufte-css)], respectively. We have ported both implementations into the [**tufte** package](https://github.com/rstudio/tufte). If you want LaTeX/PDF output, you may use the `tufte_handout` format for handouts, and `tufte_book` for books.
For HTML output, use `tufte_html`. These formats can be either specified in the YAML metadata at the beginning of an R Markdown document (see an example below), or passed to the `rmarkdown::render()` function. See #R-rmarkdown more information about **rmarkdown**.
I'm answering my own question.
The difference in behaviour between gitbook and (tufte_)html_book is in the split_by argument, which is split_by="chapter" in gitbook and split_by="section" in (tufte_)html_book. Adding split_by: chapter to the bookdown::tufte_html_book: yaml section allows to build the html without error.
I´m not a LaTex user...actually started learning something about it as consequence of dynamic document I´m working with for a book document class I´m writing in R and knitr.
I´ve tried to customize the chapter headings, but meanwhile I did not get....I found the way to do this by LaTex (https://tex.stackexchange.com/questions/73421/how-to-create-specific-chapter-style-in-book-documentclass) however I´m not knowing how to address by RMardkown.
Does anybody could help me with that? How can I arrange the below LaTex commands to be triggered by RMarkdown (by .sty file?) or something similar way to customize the chapter heading in output pdf?
\documentclass{book}
\usepackage{titlesec}
\usepackage{lipsum} % just to generate text for the example
\titleformat{\chapter}[display]
{\bfseries\Large}
{\filright\MakeUppercase{\chaptertitlename} \Huge\thechapter}
{1ex}
{\titlerule\vspace{1ex}\filleft}
[\vspace{1ex}\titlerule]
Thanks
Fabio
You can put the stuff you want in the header, except the documentclass line, in a seperate tex file (I use header.tex below). You can then specify in the header of your R-markdown file that you want to include this file in the header. The documentclass van also be set in the header of your R-markdown file. More info on this can be found on the rstudio site.
Below an example:
---
title: "Untitled"
output:
pdf_document:
includes:
in_header: header.tex
documentclass: book
---
\chapter{Introduction}
Section
=======
This is an R Markdown document.
header.tex looks like:
\usepackage{titlesec}
\usepackage{lipsum} % just to generate text for the example
\titleformat{\chapter}[display]
{\bfseries\Large}
{\filright\MakeUppercase{\chaptertitlename} \Huge\thechapter}
{1ex}
{\titlerule\vspace{1ex}\filleft}
[\vspace{1ex}\titlerule]
I would like to be able to specify multiple output formats at the same time, for instance html_document and a pdf_document. I know that this can be done very simply with something like
---
output: [html_document, pdf_document]
---
I might have some of that syntax off, but I can not seem to find the documentation anywhere. I have recently discovered knitr-bootstrap and love it. It is what I have been looking for to be able to dynamically hide my code and output blocks.
Unfortunately, by default, the YAML block for the knitr-bootstrap invocation is quite complex and I do not know how to specify multiple outputs for this.
I have looked at the YAML spec and tried a few different things but I am at a loss. Below is my current YAML frontmatter.
---
title: "Beta Regression Comparison"
opset: bootstrap
output:
knitrBootstrap::bootstrap_document:
title: "Beta Regression Comparison"
theme: Simplex
highlight: Solarized - Light
theme.chooser: FALSE
highlight.chooser: FALSE
menu: FALSE
pdf_document
---
The solution is to change pdf_document to pdf_document: default. I can't unfortunately find a reference for this syntax in the official documentation. If however you open a RMarkdown document in a recent version of RStudio, click Knit HTML and then Knit PDF, it uses this : default syntax.
The syntax is:
---
output:
html_document:
keep_md: yes
pdf_document: default
---
In my case, I tried to knit multiple output-documents using bookdown and found this post which allowed me to get the desired result.
You can write the output-definition in your YAML header as follows:
---
output:
bookdown::pdf_document2:
template: "path-to-my-template"
bookdown::word_document2:
default
knit: (function(inputFile, encoding){
rmarkdown::render(inputFile, encoding = encoding,
output_dir = "my-output-path", output_format = "all") })
---