如何在R markdown中将表“明确地”保留在这里

时间:2019-03-23 19:53:16

标签: r latex r-markdown knitr figure

我想保留在rmarkdown文档中创建的表格,该表格在输出到pdf时锁定在适当的位置,并且不能灵活地插入文本之间。

例如,我的代码是:

---
title: "Rest"
author: "Dowdy"
date: "3/23/2019"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

```

## R Markdown

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 cars}
summary(cars)
library(stargazer)
library(data.table)
```

Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. 


Hello friend. 


\begin{center}
```{r star, results='asis',echo=F,eval=T, message=FALSE}

star = capture.output(stargazer(data.table(cars)[1:15],digits=3,type = "latex",summary=F,digits.extra = 2,header = F))

star = gsub("& \\$([0-9]+)\\$", "& \\$ 0.000 \\$", star)

cat(star)

```
\end{center}

Welcome to my world of toys.  Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.
Welcome to my world of toys.
Welcome to my world of toys. gs I am testing heloo. 
Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. 

Of all the things I am testing heloo. Of all the things I am testing heloo. 

1. Test hi hihhihihih 

2. why is this hiiii

3. why test test test 


4. test test test

不幸的是,输出是这样的:

enter image description here

我不希望图像以此方式拆分编号列表和文本。我希望文本和图像的顺序与我在rmarkdown文档中键入的顺序完全相同。特别是,该编号列表和有关玩具的段落应在标记后的表格中全部出现在表格之后。

对于数字,this问题试图做我相信的事情。当然,我要的是表格而不是数字,因此解决方案对我没有帮助。

2 个答案:

答案 0 :(得分:2)

根据您提供的链接中的建议,包括

header-includes:
   - \usepackage{float}
YAML标头中的

。然后,在according to the the stargazer manual中,您可以使用参数table.placement = "H"设置浮动权限,其中

  

将浮点数精确地放置在LaTeX代码中的位置。

参考:LaTeX/Floats, Figures and Captions

您的情况应该是

star = capture.output(stargazer(data.table(cars)[1:15], digits=3,
                      type = "latex",summary=F,digits.extra = 2,
                      header = F, table.placement = "H"))

答案 1 :(得分:1)

我在更新tex时必须检查这一点,我可能需要更多有关您想要的内容的信息,但是如果您想在文本中间使用表格,您要做的就是删除\begin{center}\end{center}。 R块实际上可以用作任何位置的复制粘贴,甚至可以用作Web链接。此外,您可以将knitr中的kable()用于表,这有时有时会使情况变得更糟。当我有更好的互联网时,我会稍后再检查。