如何使用knitr,rstudio和Sweave将数字和表放在正确的位置以生成pdf?

时间:2016-11-25 06:59:26

标签: r pdf rstudio knitr

我正在尝试使用knitr和RStudio来生成pdf。我的问题是:我不能把数字和表格放在正确的位置。请看一下这个简单的例子:

\documentclass{article}
\linespread{1}
\usepackage{longtable}
\usepackage{caption}

\title{\textbf{\large{Reports: }}}
\author{\large{***}}

\begin{document}
\maketitle

<<echo= F,message = F,warning = F>>=
# Set the global chunk options
opts_chunk$set(echo = F, # Do NOT repeat code in final document
               message = F, # Do NOT print R messages in document
               warning = F # Do NOT print warnings
               )
library(dplyr)
library(ggplot2)
library(xtable)
library(stringr)
@

\section*{\large{Overview}}  %with *, then no number
<<>>=
myWhole <- data.frame(x=c(1,2,3,4),y=c(2,3,4,5))
@
\begin{figure}
<<echo = F, fig.width = 4, fig.height = 1.5, fig.align = 'center'>>=
ggplot(myWhole,aes(x=x,y=y)) + 
  geom_point()
@
\setlength{\abovecaptionskip}{-1pt}
\captionsetup{justification=centering}
\caption{****}
\end{figure}
\section*{\large{Query 6: ***}}
just try...just try...just try...just try...just try...just try...just     try...just try...
\begin{table}
\setlength{\abovecaptionskip}{-10pt}
\captionsetup{justification=centering}
\caption{}
<<echo=FALSE,results='asis',message=FALSE>>=
xt=xtable(myWhole)
print(xt,tabular.environment="longtable",floating=F)
@
\end{table}
\end{document}

您可以看到该图属于第一部分,而该表属于第二部分。我希望订单应该是:

section title: Overview
words
figure
words
section title: query 6
words
table
words

但生成的PDF看起来像这样:

enter image description here

即使这张照片不清楚,你仍然可以看到它们不在正确的位置。虽然第一页上有足够的空间,但表格位于第二页。有人可以帮我吗?

0 个答案:

没有答案