簡體   English   中英

R Markdown 文本中的換行符(不是代碼塊)

[英]Line breaks in R Markdown text (not code blocks)

使用 tufte_template rmarkdown 文件,我試圖制作一個新段落(如\newthought{} ,但沒有大寫。)我使用兩個空格,在這里用 * 表示:

# Introduction

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

但得到這個結果:

在此處輸入圖像描述

我也嘗試用\n代替第二對空格 (**),但 pandoc 會拋出錯誤。

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

最后,我嘗試使用<br>標簽,但這似乎沒有任何效果——它不會打印文本或中斷 PDF。

我想要一個沒有縮進的新段落,類似於 \newthought{},但沒有大寫...有辦法嗎?

使用 sessionInfo()更新 1

> 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

更新 2

這好像是我在使用Tufte模板時專門遇到的一個問題:

在此處輸入圖像描述

我嘗試了這些測試,它似乎有效:

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

在此處輸入圖片說明

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 

這是一個相當古老的問題,但我發布了一個答案,因為它是谷歌搜索“rmarkdown 中的換行符”時的第一次命中。

如果編譯為 pdf,您可以使用 latex 宏。 \\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...

我發現添加空行的最佳方法是:

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

# Second title with 5 blank spaces above it

你可以試試這個,希望它有幫助。 我只在 html_documents 中測試過,但大概它也可以在 pdf 中工作。

如果您使用 Knit 生成 PDF 輸出,您還可以使用原生 LaTeX 指令。 雖然這會破壞其他輸出格式,例如 HTML...:

例如:

在 Markdown 部分

---
output: pdf_document
---

# test 1
No spaces used   

line1\linebreak
line2

line3\linebreak line4

\\linebreak\\newline似乎都有效......

作為 R 表達式
r paste0("test","\\\\linebreak ", "test2")

輸出是:
測試
測試2

不要忘記在"\\linebreak "之后添加一個尾隨空格...

這也允許將段落居中。

另請參閱: 在 R Markdown 中居中圖像和文本以獲取 PDF 報告

之前使用帶有空字符 (ALT+255) 的換行符。

例子:

.\linebreak

空字符(此處顯示為句點)可防止出現錯誤消息“LaTeX 錯誤:此處沒有要結束的行”。

注意:空字符在上方顯示為句點,因為我無法在 Stackoverflow 上包含空字符)。 但是,您不能使用句點,而是按鍵盤上的 ALT + 255。 我的 .Rmd 文件將顯示為類似於句點的符號,但該符號在輸出中將不可見(例如 PDF 文件)。

R markdown 內置了對“mathdisplay”的支持,您可以使用它來插入新行,它可以正常工作

代碼

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

文件:測試.pdf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM