This seems like a basic question so it's probably out there, but I couldn't find an answer.

I'm trying to figure out how to move paragraph text to a new line after a header. I've read that you need to include two spaces after the end of the header in order for it to register as a new line. I tried adding two spaces after the header and it didn't make any difference. I also tried just adding an extra line space in the code between the header and the body, and that didn't add a space here.

Here's a reproducible example of my problem. Notice that I only have this problem when header text is followed by body text. I have no problem getting the text to display on a new line/new paragraph when it's just two lines of body text.

---
title: "test"
date: "10/2/2020"
output: pdf_document
---

Paragraph text [normal format]

Title [normal format]

Paragraph text [normal format]


After knitting to PDF from RStudio, this is what I get:

How can I get the body text after the header to display on a new line?

Thank you!

you can insert <br>
to get a line break.
I prefer this to (adding 4 spaces), because I can see
the code and the resulting line break

I agree that adding spaces is annoying, for that exact reason. Didn't know it was supposed to be 4, not 2. I did try adding spaces (tried 2 and also 4), but that didn't solve the problem.

, but it doesn't seem to work:

---
title: "test"
date: "10/2/2020"
output: pdf_document
---

Paragraph text [normal format]

Title [normal format]

Paragraph text [normal format]



I also tried putting it below the line instead of immediately after it, like this:

---
title: "test"
date: "10/2/2020"
output: pdf_document
---

<br>

Paragraph text [normal format]

Title [normal format]

Paragraph text [normal format]



...but that still didn't add a space.

Does that above code work for you if you try knitting it to pdf? I wonder if this is some weird thing with my computer or my version of Tex.

You can have more than three header levels for HTML output, but the conversion to latex for PDF output results in only three header levels being recognized (see my follow-up comment below). Below is an example of a document with various headers. I've also added latex tag examples in case you want to change the amount of vertical space after a header.

---
title: "test"
date: "10/2/2020"
output: pdf_document
---

# Title level 1

Paragraph text [normal format]

## Title level 2

Paragraph text [normal format]

### Title level 3

Paragraph text [normal format]

#### Title level 4

Paragraph text [normal format]

### Title A

Paragraph text [normal format]

Title [normal format]

Paragraph text [normal format]

### Title B
\vspace{0.5cm}
Paragraph text

### Title C
\vspace{-0.4cm}
Paragraph text


3 Likes

Sorry - I should have asked
<br> works great for html.
For pdf, try
\hfill\break
and see this StackOverflow post

Thanks for clarifying! Unfortunately, I tried \hfill\break as well, and RMarkdown doesn't seem to recognize that as anything but normal text: instead of adding a space, it just prints the "\hfill\break" into the final document as text. I tried putting it at the end of the line and/or below the header and the next line.

I had run across that SO post as well--that's where I got the idea to add two spaces after the line. But I tried that and it doesn't work.

I'm curious whether this could be a bug in the new preview version 1.4 release of RStudio, which I just downloaded today (I didn't try this in the older version, so I'm not sure if the problem happens there too). I'd be interested to hear whether others can also reproduce the line break problem in their RStudio sessions.

FWIW I'm using the new visual RMarkdown editor, but I tried also with the regular RMarkdown view and got the same results, so I don't think that's the problem.

This seems to be the key! I tried switching the headers to 3rd level instead of 4th, and the spaces now show up, without having to add any spaces or <br> or anything.

I'm a little confused about this, though. If the issue is the distinction between header vs. not, then why do the line breaks between the non-header lines work perfectly fine in my example above? It's just between the 4th-level "header" and the main text that they don't work.

The other odd thing is that #### is clearly an option in RMarkdown--I guess maybe it just doesn't translate to PDF? It's even one of the choices in the dropdown menu in the new visual RMarkdown editor.

Anyway, I can get by with 3rd-level headers for now, and I'll try your latex tags if I end up needing to use 4th-level ones. Thanks for the help!

I'm afraid I don't use a lot of pdf, and have reached the limits of my knowledge. Any help from @technocrat or @apreshill ?

1 Like

No worries! Thank you for your responses. Looks like I've got a working fix by just switching to 3rd-level headers (suggested by @joels above), though I'd still be curious to hear more about why this happens!

Based on this Pandoc issue, for PDF output the rendering of header levels beyond the third seems to be based on how Pandoc renders to latex and how latex header and paragraph levels are defined.

Huh, okay. I guess I should have guessed it was a latex/pandoc issue, not a markdown issue per se.

I just tried your \vspace{-0.4cm} fix with my 4th-level header, was surprised when it didn't work, and then went back and actually looked at the code you sent and realized you used that to adjust the spacing on a 3rd-level header. So just to clarify for anyone else reading this: I think we still don't have a fix for adding spacing with 4th-level headers, because of Pandoc limitations.

I'm just going to stick with 3rd-level headers. Ah well.

Thank you all!

1 Like

Yes, AFAIK, to get headers of 4th level or greater with PDF output, you'd need to create some new definitions in a latex preamble. I haven't tried this, but here's one example. With rmarkdown, you'd probably need to put this or something similar in a latex document that would be referenced with the before_body tag in the YAML at the beginning of your rmarkdown doc.

2 Likes

I agree with @joels that it's possible to accomplish multilevel headers in LaTeX by some combination of jumping through the hoops of creating a custom template and the use of tex syntax in place of markdown for the headers or just biting the bullet and save to tex and edit for output there. Perfect project for the year 2020.

1 Like

Quick note. Did you try it without the square brackets? Since markdown links are [like this](http://link.com). Perhaps you using [normal text] in the title is messing up formatting as it may be expecting the next part of the link.

That's a really good thought! And yeah, I probably shouldn't have chosen that for my example. I did try without the brackets--my actual titles have no brackets and were behaving the same way.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.