[Tex/LaTex] knitr – double spacing in an html document

formattingknitr

If my knitr output was a pdf, then I can begin my document as follows to have the text double spaced:

title: My Title
author: "My Name"
date: "Today's Date"
header-includes:
   - \usepackage{setspace}
   - \doublespacing
output: pdf_document

However, if I change the output to html_document, the text remains single spaced. Is there a way to get it to be double spaced?

Best Answer

I assume you are using RStudio, so I am writing from this perspective. There are two aspects to this question. First, you need to understand how linespacing is defined for HTML documents. Then we need to discuss how knitr applies style information.

Line spacing in HTML documents can be adjusted locally by applying a style attribute to elements , or globally, with an external cascading stylesheet, a .css file, or an internal style definition.

For the local approach, you can use the following style attribute to the <p> tag, to set a single paragraph doublespaced <p style="line-height: 2em;"> ... </p> (one paragraph); or multiple paragraphs (even the whole document) you can use a <div> tag instead: <div style="line-height: 2em;"> ... </div> (spanning whatever you need).

Globally you can apply the line-height definition e.g. to all <p> elements via a style-sheet by specifying p {line-height: 2em;} in a file e.g. mystyle.css and loading it into your HTML document with the declaration <link rel="stylesheet" type="text/css" href="mystyle.css"> in the <head>...</head> block of the HTML document. This would be considered the most principled solution that cleanly separates formatting from contents. But you no longer have a standalone file, rather your external stylesheet needs to be located somewhere under the webroot directory, where your server can access it and serve it together with your document. However you can achieve the same thing with an internal stylesheet, by placing the following block:

    <style>
        p {line-height: 2em;}
    </style>

in the header of your HTML file, or adding this line to whatever style information is already there. This will give you an entirely double-spaced HTML document .

So far the HTML. There are several ways to get knitr to produce such a double-spaced HTML file:

  • you can tell knitr to include your own external stylesheet by specifying this in the YAML header of your .Rmd document.

    output:
      html_document:
        css: mystyle.css
    

    (Note indentation! The file needs to be located in your home directory - or you need to add a path.) This requires the use of two files.

  • You can add the internal style information to the pandoc template used for making html files. This would only be reasonable if there is much more style information that you want to customize. See here for more information.

  • Or, since markdown allows raw HTML, you can simply add <div style="line-height: 2em;"> ... </div> as the first respectively last line of your .Rmd document. This makes the double-space self-contained and has the added advantage that you can skip over parts of the document that should be single-spaced after all. This is probably what I would do.

  • For even more ideas, you can refer to the documentation here.