8

I'd like to cross-reference a table that I generated with xtable in R. I cannot able to. Here is what I did:

\documentclass{article}
\begin{document}
Look at \ref{tab:mytable}.
<<echo=FALSE,results='asis'>>=
  library(xtable, car)
  print(xtable(x=mtcars[1:5,1:5]), label = "tab:mytable")
@
\end{document}

Any ideas?

  • Nice question HBat (complete, simple example). The only thing missing (I know, I'm picky) is that in general it is helpful to show what output you get. e.g. post your PDF or if you encountered an error show which error you get. "I cannot able to" does not describe the output that you got. Welcome to SE! – scottkosty Jul 9 '14 at 21:02
  • 1
    By the way, in my opinion xtable should give you a warning because you specified the label argument but no caption argument. I would normally submit a patch, but the xtable authors haven't been too responsive lately so I'm hesitant to spend the time to do so. – scottkosty Jul 9 '14 at 21:04
  • @scottkosty I didn't know that I can able to post pdf's. Next time I'll try to do that. Thanks for the tip. – HBat Jul 9 '14 at 23:08
  • Actually I post a screen shot because that way it's zoomed in. I'm not sure if you can post PDFs (sorry for the unclear wording of my message). – scottkosty Jul 10 '14 at 2:50
13

If you look at your example, the table does not have a number. I assume you want to reference the table number, so first we must give the table a number. To do this, use the caption argument.

Second, when using xtable, you have to be careful with which arguments you want to give to xtable and which arguments to print.xtable. You are currently passing the label argument to print.xtable but if you look at ?print.xtable there is no label argument.

Taking into account both of the above points gives us:

\documentclass{article}
\begin{document}
Look at \ref{tab:mytable}.
<<echo=FALSE,results='asis'>>=
  library(xtable, car)
  print(xtable(x=mtcars[1:5,1:5], caption = "example", label = "tab:mytable"))
@
\end{document}

The above code gives me the following output (note that you may have to run your latex/pdflatex command more than once):

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.