33

I recently opened a standard Rmd file without editing anything. The default file looks like this:

Untitled.rmd

---
title: "myfile"
author: "Me"
date: "May 25, 2015"
output: html_document
fontsize: 12pt
---

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>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
summary(cars)
```

You can also embed plots, for example:

```{r, echo=FALSE}
plot(cars)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

I wanted to create an html file corresponding to the above file so in a separate R script I did the following:

knit('Untitled.Rmd', 'doc.md')
markdownToHTML('doc.md', 'testing.html',header = TRUE)

For some reason the font size doesn't work and the header information that I was hoping for doesn't appear in my testing.html. Anyone know why this is happening?

1
  • 3
    I believe fontsize is a LaTeX-only metadata variable in rmarkdown (not knitr), as demonstrated on rmarkdown.rstudio.com. (My version of Rstudio -- 0.98.1103 -- does not include that option, did you modify the YAML header of the document or get the template from a different package?) Perhaps you can look at customizing the CSS.
    – r2evans
    May 25, 2015 at 22:46

1 Answer 1

84

This is what I used to control font size and color in an R-markdown file. It basically overrides the CSS style sheets without having to create a new file. The example changes the sizes of the headers and titles, as well as the inline text and the R-code text, and sets some colors as well.

In my case I needed to pack more information into a document that had a specified number of pages so I made everything smaller.

---
title: "This is a title"
date: 25 May 2015
output:
html_document:
theme: cerulean
---

<style type="text/css">

body{ /* Normal  */
      font-size: 12px;
  }
td {  /* Table  */
  font-size: 8px;
}
h1.title {
  font-size: 38px;
  color: DarkRed;
}
h1 { /* Header 1 */
  font-size: 28px;
  color: DarkBlue;
}
h2 { /* Header 2 */
    font-size: 22px;
  color: DarkBlue;
}
h3 { /* Header 3 */
  font-size: 18px;
  font-family: "Times New Roman", Times, serif;
  color: DarkBlue;
}
code.r{ /* Code block */
    font-size: 12px;
}
pre { /* Code block - determines code spacing between lines */
    font-size: 14px;
}
</style>


# H1 Header

Some body text

## H2 Header

More body text

### H3 Header

blah blah blah

```{r echo=T}
n <- 100
df <- data.frame(x=rnorm(n),y=rnorm(n))
```

### Another H3

Update:

Added more more styles, comments, and a bit of color to make this answer more useful. And a screen shot:

enter image description here

7
  • code.r and pre aren't working for me; have the codes changed? Apr 17, 2018 at 21:27
  • Not sure, I will have a look in the morning, but what OS and Browser are you using?
    – Mike Wise
    Apr 17, 2018 at 21:29
  • 1
    Just figured it out, I had another theme loaded which was apparently getting in the way. Sorry about that, and thanks for the quick response. Apr 17, 2018 at 21:31
  • 1
    I'm using it in May 2019. There is a warning message for the title: "Element (h1.title) is overqualified, just use .title without element name". There is an additional warning for h1 element: "Heading h1 has already been defined." Both warnings go away once you change h1.title to .title. May 20, 2019 at 19:14
  • 1
    I have a question: does this hold for power point presentations?
    – teogj
    Aug 11, 2020 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.