2015-06-02 75 views
4

我目前與R Studio一起在LaTex中使用R/knitr生成PDF文檔。在這些文件中,我想在我想在文中引用的表格中呈現部分結果。我使用R中的xtable包生成這些表,並且它工作正常,給我正確的表。 到目前爲止這麼好,但當涉及到引用我遇到了一些麻煩。目前產生的PDF輸出指的是表格所在的部分。我在一些LaTex幫助文件中進行了搜索,並指出,當浮動對象的標籤位於標題之前時會發生這種情況。但是,當我檢查xtable的輸出時,它被放置在標題之後。 有沒有人有這個解決方案? 下面是一個與我使用的代碼類似的示例代碼,它給了我PDF中相同的結果(我保留了在LaTex和R中使用的所有包,以確保它不是由某些干擾引起的)。LaTex中使用knitr和xtable交叉引用的問題

\documentclass[preprint,authoryear]{elsarticle} 
\usepackage[a4paper]{geometry} 
\usepackage{natbib} 
\usepackage{mathpazo} 
\usepackage{booktabs} 
\usepackage{dcolumn} 
\usepackage[breaklinks]{hyperref} 
\usepackage{hyperref,url} 
\usepackage{lscape} 
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} 
\usepackage{amssymb} 
\usepackage{gensymb} 
\usepackage[allow-number-unit-breaks=true]{siunitx} 
\usepackage[T1]{fontenc} 
\usepackage[utf8]{inputenc} 
\usepackage[version=3]{mhchem} 
\usepackage[section]{placeins} 
\usepackage[colorinlistoftodos]{todonotes} 
\usepackage{float} 
\usepackage{lineno} 
\begin{document} 
\section{Test} 

This is a test. You can see the result in Tab. \ref{tab:Test}. 
\section{Table} 
<<Ini,eval=TRUE,include=FALSE,echo=FALSE,warning=FALSE,error=FALSE,cache=FALSE>>= 
#set chunk options 
opts_chunk$set(echo=FALSE,warning=FALSE,message=FALSE,dev.args=list(pointsize=12),fig.pos="!h",dpi=500) 

require(stringr) 
require(reshape2) 
require(lubridate) 
library(plyr) 
library(lattice) #Needed for multi-panel graphs 
library(mgcv)  #Needed for smoothing curves in scatterplots 
library(ggplot2) 
library(ggmap) 
require(gridExtra) 
library(scales) 
library(xtable) 
require(nlme) 
library(zoo) 
@ 

<<results='asis',echo=FALSE>>= 
Test<-data.frame(Test=c(1:3),a=c(4:6)) 
print(xtable(Test,caption="Test table",label="tab:Test", 
      digits=2), 
     include.rownames=FALSE, 
     caption.placement="top", 
     latex.environments = "left") 

@ 



\end{document} 

從xtable碼結果看起來是這樣的:

% latex table generated in R 3.0.3 by xtable 1.7-4 package 
% Tue Jun 02 09:27:38 2015 
\begin{table}[ht] 
\begin{left} 
\caption{Test table} 
\label{tab:Test} 
\begin{tabular}{rr} 
    \hline 
Test & a \\ 
    \hline 
    1 & 4 \\ 
    2 & 5 \\ 
    3 & 6 \\ 
    \hline 
\end{tabular} 
\end{left} 
\end{table} 

當我試圖運行此我得到這似乎有點奇怪,但還是有PDF輸出4個警告消息。所有4個警告都圍繞着xtable生成的代碼。這裏來自R Studio內的Log輸出:

! Missing $ inserted. 
<inserted text> 
       $ 
l.81 \begin{left} 

I've inserted a begin-math/end-math symbol since I think 
you left one out. Proceed, with fingers crossed. 

! Missing delimiter (. inserted). 
<to be read again> 
        \global 
l.82 \caption 
      {Test table} 
I was expecting to see something like `(' or `\{' or 
`\}' here. If you typed, e.g., `{' instead of `\{', you 
should probably delete the `{' by typing `1' now, so that 
braces don't get unbalanced. Otherwise just proceed. 
Acceptable delimiters are characters whose \delcode is 
nonnegative, or you can use `\delimiter <delimiter code>'. 

! Missing $ inserted. 
<inserted text> 
       $ 
l.82 \caption{Test table} 

I've inserted a begin-math/end-math symbol since I think 
you left one out. Proceed, with fingers crossed. 

! Missing \right. inserted. 
<inserted text> 
       \right . 
l.82 \caption{Test table} 

I've inserted something that you may have forgotten. 
(See the <inserted text> above.) 
With luck, this will get me unwedged. But if you 
really didn't forget anything, try typing `2' now; then 
my insertion and my current dilemma will both disappear. 

我希望有人能夠幫助我!

PS:請注意,我無法發佈我的PDF輸出的屏幕截圖,因爲我沒有足夠的聲望。

回答

2

left環境似乎搞亂了參考。我試過

% latex table generated in R 3.2.0 by xtable 1.7-4 package 
% Tue Jun 02 09:53:12 2015 
\begin{table}[ht] 
\caption{Test table} 
\label{tab:Test} 
\begin{left} 
\begin{tabular}{rr} 
    \hline 
Test & a \\ 
    \hline 
    1 & 4 \\ 
    2 & 5 \\ 
    3 & 6 \\ 
    \hline 
\end{tabular} 
\end{left} 
\end{table} 

它會產生一個正確的參考。

enter image description here

+0

完美的作品,謝謝!我真的不明白爲什麼,但是啊... – Sarina