LaTex 中与 knitr 和 xtable 交叉引用的问题

Problems cross referencing in LaTex with knitr and xtable

我目前正在使用 R Studio 在 LaTex 中使用 R/knitr 生成 PDF 文档。在这些文档中,我想在 tables 中展示我想在文本中引用的部分结果。我使用 R 中的 xtable 包生成这些 tables,它运行良好,并为我提供了正确的 tables。 到目前为止一切顺利,但在引用时我 运行 遇到了一些麻烦。目前生成的 PDF 输出是指 table 所在的部分。我在一些 LaTex 帮助文件中四处搜索,并指出当浮动 object 的标签放在标题之前时会发生这种情况。但是,当我检查 xtable 的输出时,它被放置在标题之后。 有人对此有解决方案吗? 这是一个示例代码,与我使用的代码类似,它在 PDF 中给出了相同类型的结果(我将 LaTex 和 R 中使用的所有包都留在了其中,以确保它不是由与这些干扰引起的)。

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

当我尝试 运行 时,我收到 4 条警告消息,看起来有点像 st运行ge,但仍然有 PDF 输出。所有 4 个警告都围绕 xtable 生成的代码。这里是 R Studio 中日志的输出:

! 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:请注意,我无法post我的 PDF 输出的屏幕截图,因为我没有足够的声誉。

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}

并且它生成了正确的引用。