4

What is the right way to get knitr to do cross-references? The recommended approach in the documentation (see below) doesn't work for me. I am writing a book with knitr and the tufte_book template. I can't seem to get the cross references to figures (or tables) to work correctly. I've enclosed a small clip that consistently fails to get the cross refs updated in the document. Im running R 3.2.3 on x86_64-apple-darwin13.4.0. I know this question has been answered before, but all the answers fail in the same way. No, I don't want to use bookdown.

---
title: "Cross-references SNAFU"
output:
  tufte::tufte_book:
    citation_package: natbib
    latex_engine: xelatex
---
```{r ch3-inits, include=FALSE}
library(tufte)
library(knitr)
knitr::opts_chunk$set(eval.after = 'fig.cap') 

```
\chapter{Not Working Cross References, the beginning \label{chap:alpha} }

```{r t-test, fig.cap=paste('$2 \\times 2$ is', 2*2), fig.height=2.5}
x = rnorm(30)
par(mar=c(4, 4, .1, .1))
hist(x, main='')
```


Here is the reference to that Figure \ref{fig:t-test}.  NOT.

Here is a clip of the output: resultant pdf output

MD Ward
  • 41
  • 3
  • Your example works on my system. Please try to erase all auxiliary LaTeX files (`filename.aux` and friends). – CL. Aug 23 '16 at 06:05
  • Thanks, I am now convinced that it is a configuration problem. But, I haven't figured out where, yet. – MD Ward Aug 23 '16 at 16:17
  • You could start with updating R, RStudio and your LaTeX distribution. – CL. Aug 23 '16 at 16:54
  • I did They are all up-to-date. And, it fails -- for me -- in a clean directory. But works fine on other computers. This is the new version of DLL-Hell. – MD Ward Aug 23 '16 at 20:48
  • If it works on some but not all of your systems, you could check the following: Compare the generated TEX files. If they're identical, it must be a LaTeX problem. If not, the issue lies earlier in the toolchain. – CL. Aug 23 '16 at 21:38
  • Thanks, but knitr, rstudio "cleans up" all of these files, even when I have no-opt that option in the preferences. I learned that (on an Ubuntu system) Latex needed to be updated to get this program to work. Thanks, by the way, for the clues. – MD Ward Aug 23 '16 at 22:46
  • I am really not sure why, but this is now working if I put it in a child document. There is something with versions of this and that, but it does seem that installing the most recent (2016, July) version of TeX and friends seems to be necessary (but not necessarily sufficient). For now, thanks. I won't close this, but I'm on to other things, while this all percolates. – MD Ward Aug 23 '16 at 23:20
  • You can also try the bookdown package, which introduced new syntax for cross-references: https://bookdown.org/yihui/bookdown/figures.html The output format will be `bookdown::tufte_book2` https://bookdown.org/yihui/bookdown/a-single-document.html – Yihui Xie Oct 13 '16 at 20:55

1 Answers1

0

The solution to this problem is to set keep_tex: true in YAML and then to separately run the (La)Tex file outside of Rstudio. Dumb I know, but it works. Still don't know why it gets confused with multiple child documents, but it seems to for me.

MD Ward
  • 41
  • 3