I am currently working with R Studio to create PDFs with R / knitr in LaTex. Inside these documents, I want to present some of my results in the tables that I want to refer to in the text. I create these tables using the xxtable package inside R, and it works fine and gives me the correct tables. So far so good, but when it comes to links, I run into some problems. The resulting PDF output currently refers to the section in which the table lies. I searched in some LaTex help files, and says that this happens when a floating-point label is placed before the header. However, when I checked the xxtable output, it was placed after the header. Does anyone have a solution for this? Here is an example code similar to the one I used, which gives me the same result in PDF (I left all the packages,used in LaTex and R, to make sure that this is not something caused by some interference with them).
\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>>=
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)
library(mgcv)
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 , , 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: , , PDF, .