在LaTex中使用Knitr和XTable交叉引用的问题 [英] Problems cross referencing in LaTex with knitr and xtable

查看:531
本文介绍了在LaTex中使用Knitr和XTable交叉引用的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与R Studio合作,在LaTex中使用R/knitr生成PDF文档.在这些文档中,我想将部分结果显示在文本中要引用的表中.我使用R中的xtable包来生成这些表,它可以正常工作并为我提供正确的表. 到目前为止,一切都很好,但是当涉及到引用时,我遇到了一些麻烦.目前,生成的PDF输出引用该表所在的部分.我在一些LaTex帮助文件中进行了搜索,结果表明,当将浮动对象的标签放在标题之前时,就会发生这种情况.但是,当我检查xtable的输出时,它位于标题之后. 有人对此有解决方案吗? 这是与我使用的示例代码相似的示例代码,在PDF中为我提供了相同的结果(我将LaTex和R中使用的所有软件包都留在了其中,以确保它不是由对它们的某些干扰引起的).

I am currently working with R Studio to produce PDF documents with R/knitr in LaTex. Within these documents I want to present part of my results in tables that I want to refer to in the text. I produce these tables using the xtable package within R and it is working perfectly and giving me the right tables. So far so good but when it come to referencing I ran into some troubles. At the moment the resulting PDF output refers to the section in which the table lies. I searched around in some LaTex help files and it is stated that this happens when the label of the floating object is placed before the caption. However when I checked the output of xtable it was placed after the caption. Does anyone have a solution for this? Here is an example code similar to the one I used that gives me the same kind of result in the PDF (I left all packages used in LaTex and R in to make sure it is not something caused by some interference with these).

\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代码的结果如下:

The result from the xtable code looks like this:

% 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条警告消息,看起来有些奇怪,但仍然有PDF输出.所有4条警告均与xtable生成的代码有关.这里是R Studio中Log的输出:

When I try to run this I get 4 warning messages that seem kind of strange but there is still a PDF output. All 4 warnings are around the code that is produced by xtable. Here the output from the Log within 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.

我希望有人能够帮助我!

I hope there is someone that is able to help me!

PS:请注意,由于信誉不佳,我无法发布PDF输出的屏幕截图.

PS: Please note that I am not able to post a screen shot of my PDF output because I don't have enough reputation.

推荐答案

left环境似乎弄乱了引用.我尝试过

The left environment seems to be messing up the referencing. I tried

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

它会产生正确的参考.

这篇关于在LaTex中使用Knitr和XTable交叉引用的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆