Line breaks in R Markdown text (not code blocks)
Asked Answered
K

6

25

Using the tufte_template rmarkdown file, I am trying to make a new paragraph (like \newthought{}, but no caps.) I use two spaces, denoted here by *:

# Introduction

The Tufte-\LaTeX\ [^tufte_latex] document**
**
classes define a style similar to the style Edward Tufte uses in his books...

but get this result:

enter image description here

I have tried \n in place of the second pair of spaces (**) as well, but pandoc throws an error.

pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43

Finally, I tried using a <br> tag, but that seems to have no effect whatsoever - it doesn't print the text or a break to the PDF.

I would like a new paragraph, without the indentation, similar to \newthought{}, but without the capitalization...is there a way?

Update 1 with sessionInfo():

> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] digest_0.6.8    htmltools_0.2.6 rmarkdown_0.5.1 tools_3.1.2     yaml_2.1.13

Update 2

This seems to be a problem I encounter specifically when using the Tufte template:

enter image description here

Karalee answered 5/5, 2015 at 15:23 Comment(3)
Your code works for me, no indentation, no capitalization.Recover
@Recover you have a full, empty line between the "Tufte LaTex Document" and "classes define..."?Karalee
@Recover just ran it again from scratch, same output as above...?Karalee
R
27

I tried these tests, it seems to be working:

test.Rmd

---
output: pdf_document
---

# test 1
No spaces used   

line1
line2


# test 2
2spaces at the end of line1  

line1  
line2

# test 3
2spaces at the end of line1, then 2 spaces on next line

line1  

line2

enter image description here

sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] htmltools_0.2.6 tools_3.2.0     yaml_2.1.13     rmarkdown_0.5.1 digest_0.6.8 
Recover answered 5/5, 2015 at 16:0 Comment(1)
it would seem that it's the tufte_template that is causing these different outcomes... see the new image I updated with -- Why would it be doing that?Karalee
Y
24

It's a rather old question, but I post an answer since it's the first hit when googling for "line breaks in rmarkdown".

If compiling to pdf, you can use latex macros. Substitute the ** in the new line with \hfill\break:

# Introduction

The Tufte-\LaTeX\ [^tufte_latex] document**
\hfill\break
classes define a style similar to the style Edward Tufte uses in his books...
Yahiya answered 30/3, 2017 at 9:7 Comment(1)
I couldn't use the accepted answer to add space between text and a following code chunk, but \hfill did the trick for me.Leporide
B
5

The best way I have found to add blank lines is:

# First title
<br><br><br><br><br>  

# Second title with 5 blank spaces above it

You can try this, hopefully it helps. I have tested in html_documents only, but presumably it would work in pdf as well.

Barron answered 24/9, 2019 at 18:56 Comment(1)
Sadly, the <br> gets silently eaten for PDF targets.Annmaria
C
3

R markdown has builtin support for "mathdisplay" you may use that for inserting new line it just works.

code

---
title: "testdoc"
author: ""
date: ""
output: pdf_document
---

# Title

this is a test text $\\$ this is a text after newline

too many newlines $\\ \\ \\ \\ \\$ foo $\\$ bar

\begin{paragraph}{with newline}
$\\$
How much wood could a woodchuck chuck if a
woodchuck could chuck wood?  A woodchuck $\\$
would chuck all the wood that a woodchuck
could chuck, if a woodchuck could chuck wood.
\end{paragraph}

\begin{paragraph}{without newline}
  How much wood could a woodchuck chuck if a
woodchuck could chuck wood?  A woodchuck
would chuck all the wood that a woodchuck
could chuck, if a woodchuck could chuck wood.
\end{paragraph}

output

file : test.pdf

Circulate answered 21/11, 2022 at 15:7 Comment(1)
This is a very concise and good solution, works like a charm. (How come not more upvotes.) Certainly more efficient than e.g. &nbsp;\linebreak.Ninnetta
C
2

You can also use native LaTeX instructions if you use Knit to generate the PDF output. This would break the other output formats such as HTML though... :

e.g. :

In Markdown part

---
output: pdf_document
---

# test 1
No spaces used   

line1\linebreak
line2

line3\linebreak line4

\linebreak and \newline seem to both work...

As R expression
r paste0("test","\\linebreak ", "test2")

Output is :
test
test2

Don't forget to add a trailing space after "\linebreak "...

This also allows to center paragraphs.

See also : Centering image and text in R Markdown for a PDF report

Castiron answered 19/4, 2021 at 10:25 Comment(0)
E
1

Use linebreak with an empty character (ALT+255) before.

Example:

.\linebreak

The empty character (here shown as a period) prevents the error message "LaTeX Error: There's no line here to end."

NB: The empty character is above shown as a period, since I could not include an empty character here on Stackoverflow). However you must not use period, but press ALT + 255 on you keyboard. I the .Rmd file it will show as as symbol similar to a period, but this symbol will be invisible in the output (e.g. PDF file).

Extensile answered 2/7, 2021 at 4:46 Comment(2)
when you say "ALT + 255 on you keyboard" do you mean hold down alt while pressing 2,2, and then 5? if so that does nothing on mine...Karalee
Yes, exactly. Hold ALT all the time while pressing 2,5,5 in a sequence rather quickly and then releasing ALT. (255 not 225.) Often need to give it a couple of tries before it hits in correctly.Possibly the empty character will not show in your file, even if I think it should show. If so, try to copy paste to another text editor to see if it might show there.Extensile

© 2022 - 2024 — McMap. All rights reserved.