rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.75k stars 1.27k forks source link

Single document with pdf output on Windows fails to find generated figures #121

Closed benmarwick closed 8 years ago

benmarwick commented 8 years ago

Not sure what I'm doing wrong with this. Here is my Rmd (between the horizontal lines, the chunk options are hidden by github's markdown renderin)



---
title: 'Bookdown: A single document'
author: "Ben Marwick"
date: "May 23, 2016"
output: bookdown::pdf_document2

---

Can we cross-reference before the figure? Figure \@ref(fig:nice-fig)

```{r nice-fig, fig.cap='Here is a nice figure!'}
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)

How about cross-referencing after the figure? Figure \@ref(fig:nice-fig)

What about tables? Here is Table \@ref(tab:nice-tab).

knitr::kable(
  head(iris, 10), caption = 'Here is a nice table!',
  booktabs = TRUE
)

---

And here is the output from the Rmarkdown console, it is the same for `pdf_document2` and `tufte_handout2`

---

processing file: tmp.Rmd |................ | 25% ordinary text without R code

|................................ | 50% label: nice-fig (with options) List of 1 $ fig.cap: chr "Here is a nice figure!"

|................................................. | 75% ordinary text without R code

|.................................................................| 100% label: nice-tab (with options) List of 1 $ tidy: logi FALSE

output file: tmp.knit.md

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS tmp.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output tmp.tex --table-of-contents --toc-depth 2 --template "C:\emacs\R\win-library\3.3\rmarkdown\rmd\latex\default-1.17.0.2.tex" --number-sections --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" latexmk.exe: Invalid argument. latexmk.exe: Data: 4294967295 ! Package pdftex.def Error: File `tmp_files/figure-latex/nice-fig-1.pdf' not fo und.

! Undefined control sequence. l.166 \toprule

! Undefined control sequence.

\midrule ! Undefined control sequence. \addlinespace ! Undefined control sequence. \bottomrule Error: Failed to compile tmp.tex. See tmp.log for more info. In addition: Warning messages: 1: running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\latexmk.exe" -pdf -latexoption=-halt-on-error -interaction=batchmode -pdflatex="pdflatex" "tmp.tex"' had status 1 2: running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\latexmk.exe" -v' had status 1 Execution halted ``` --- Here is my session info: ``` > devtools::session_info() Session info ------------------------------------------------------------ setting value version R version 3.3.0 (2016-05-03) system x86_64, mingw32 ui RStudio (0.99.1187) language (EN) collate English_United States.1252 tz Australia/Sydney date 2016-05-23 Packages ---------------------------------------------------------------- package * version date source bookdown * 0.0.71 2016-05-23 Github (rstudio/bookdown@d819dc7) curl 0.9.7 2016-04-10 CRAN (R 3.2.5) devtools 1.11.1 2016-04-21 CRAN (R 3.2.5) digest 0.6.9 2016-01-08 CRAN (R 3.2.3) evaluate 0.9 2016-04-29 CRAN (R 3.2.5) git2r 0.15.0 2016-05-11 CRAN (R 3.2.5) htmltools 0.3.5 2016-03-21 CRAN (R 3.2.4) httpuv 1.3.3 2015-08-04 CRAN (R 3.2.3) httr 1.1.0 2016-01-28 CRAN (R 3.2.4) knitr 1.13.1 2016-05-23 Github (yihui/knitr@ce40cbf) magrittr 1.5 2014-11-22 CRAN (R 3.2.0) memoise 1.0.0 2016-01-29 CRAN (R 3.2.4) mime 0.4 2015-09-03 CRAN (R 3.2.2) miniUI 0.1.1 2016-01-15 CRAN (R 3.2.5) R6 2.1.2 2016-01-26 CRAN (R 3.2.4) Rcpp 0.12.5 2016-05-14 CRAN (R 3.2.5) rmarkdown 0.9.6.8 2016-05-18 Github (rstudio/rmarkdown@a39fd80) rsconnect 0.4.3 2016-05-02 CRAN (R 3.2.5) shiny 0.13.2 2016-03-28 CRAN (R 3.2.4) stringi 1.0-1 2015-10-22 CRAN (R 3.2.3) stringr 1.0.0 2015-04-30 CRAN (R 3.2.0) withr 1.0.1 2016-02-04 CRAN (R 3.2.4) xtable 1.8-2 2016-02-05 CRAN (R 3.2.4) yaml 2.1.13 2014-06-12 CRAN (R 3.2.0) ```
ccolonescu commented 8 years ago

I have the same problem. When I make pdf using render_book("~/inputdir", pdf_document()), the figures show up in the output.pdf (but of course the cross references do not), but when I try to use render_book("~/inputdir", pdf_document2()) it does not find the figures and the process exits with errors. image

yihui commented 8 years ago

I cannot reproduce this issue on Windows. Might be an issue with MikTeX (are you using the latest version?). Could you provide the output of

system('pdflatex --version')
system('latexmk --version')
ccolonescu commented 8 years ago

system('pdflatex --version') MiKTeX-pdfTeX 2.9.5900 (1.40.17) (MiKTeX 2.9 64-bit) Copyright (C) 1982 D. E. Knuth, (C) 1996-2016 Han The Thanh TeX is a trademark of the American Mathematical Society. compiled with zlib version 1.2.8; using 1.2.8 compiled with libpng version 1.6.21; using 1.6.21 compiled with poppler version 0.42.0 compiled with jpeg version 8.4 system('latexmk --version') latexmk.exe: MiKTeX encountered an internal error. Warning message: running command 'latexmk --version' had status 1

benmarwick commented 8 years ago

A relevant issue seems to be that when I knit the Rmd, it's looking for latexmk in the wrong location. In the R Markdown console I see this:

output file: tmp.knit.md

latexmk.exe: MiKTeX encountered an internal error.
Error: Failed to compile tmp.tex.
In addition: Warning messages:
1: running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\latexmk.exe" -pdf -latexoption=-halt-on-error -interaction=batchmode -pdflatex="pdflatex" "tmp.tex"' had status 1 
2: running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\latexmk.exe" -v' had status 1 
Execution halted

Similarly, if I run '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\latexmk.exe" -pdf -latexoption=-halt-on-error -interaction=batchmode -pdflatex="pdflatex" "tmp.tex"' in a git bash terminal, I get latexmk.exe: MiKTeX encountered an internal error.

So I had a look to see exactly where latexmk.exe is located on my computer, and the location looks slightly different to what I see in the Rmd console. If I run the latekmk.exe command in my bash terminal, with the location I can identify manually, I get a bit more information:

marwick@MARWICK-MOBILE MINGW64 /c/Users/marwick/Desktop
$ "C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\latexmk.exe" -pdf -latexoption=
-halt-on-error -interaction=batchmode -pdflatex="pdflatex" "tmp.tex"
Latexmk: This is Latexmk, John Collins, 22 April 2016, version: 4.45.
Latexmk: applying rule 'pdflatex'...
Rule 'pdflatex': File changes, etc:
   Changed files, or newly in use since previous run(s):
      'tmp.aux'
   Non-existent destination files:
      'tmp.pdf'
------------
Run number 1 of rule 'pdflatex'
------------
------------
Running 'pdflatex  -halt-on-error -interaction=batchmode -recorder  "tmp.tex"'
------------
This is pdfTeX, Version 3.14159265-2.6-1.40.17 (MiKTeX 2.9)
entering extended mode
Latexmk: Missing input file: 'tmp_files/figure-latex/nice-fig-1.pdf' from line
  'LaTeX Warning: File `tmp_files/figure-latex/nice-fig-1.pdf' not found on inpu
t '
Latexmk: Missing input file: 'tmp_files/figure-latex/nice-fig-1.pdf' from line
  'LaTeX Warning: File `tmp_files/figure-latex/nice-fig-1.pdf' not found on inpu
t line 144.'
Latexmk: List of undefined refs and citations:
  Reference `fig:nice-fig' on page 1 undefined on input line 133
Latexmk: Summary of warnings:
  Latex failed to resolve 1 reference(s)
Latexmk: Errors, so I did not complete making targets
Collected error summary (may duplicate other messages):
  pdflatex: Command for 'pdflatex' gave return code 256
Latexmk: Use the -f option to force complete processing,
 unless error was exceeding maximum runs of latex/pdflatex.

It cannot find the figures, the same problem I saw in the Rmd console.

If I remove the arguments to latexmk.exe, I get a more verbose output, with the same errors:

marwick@MARWICK-MOBILE MINGW64 /c/Users/marwick/Desktop
$ "C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\latexmk.exe" -pdf "tmp.tex"
Latexmk: This is Latexmk, John Collins, 22 April 2016, version: 4.45.
Latexmk: applying rule 'pdflatex'...
Rule 'pdflatex': Rules & subrules not known to be previously run:
   pdflatex
Rule 'pdflatex': The following rules & subrules became out-of-date:
      'pdflatex'
------------
Run number 1 of rule 'pdflatex'
------------
------------
Running 'pdflatex  -recorder  "tmp.tex"'
------------
This is pdfTeX, Version 3.14159265-2.6-1.40.17 (MiKTeX 2.9)
entering extended mode
(tmp.tex
LaTeX2e <2016/03/31> patch level 1
Babel <3.9r> and hyphenation patterns for 75 language(s) loaded.
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\article.cls"
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\size10.clo"))
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\lm\lmodern.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\amsfonts\amssymb.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\amsfonts\amsfonts.sty"))
(C:\Users\marwick\AppData\Roaming\MiKTeX\2.9\tex\latex\amsmath\amsmath.sty
For additional information on amsmath, use the `?' option.
(C:\Users\marwick\AppData\Roaming\MiKTeX\2.9\tex\latex\amsmath\amstext.sty
(C:\Users\marwick\AppData\Roaming\MiKTeX\2.9\tex\latex\amsmath\amsgen.sty))
(C:\Users\marwick\AppData\Roaming\MiKTeX\2.9\tex\latex\amsmath\amsbsy.sty)
(C:\Users\marwick\AppData\Roaming\MiKTeX\2.9\tex\latex\amsmath\amsopn.sty))
("C:\Program Files (x86)\MiKTeX 2.9\tex\generic\ifxetex\ifxetex.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\generic\oberdiek\ifluatex.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\fixltx2e.sty"

Package fixltx2e Warning: fixltx2e is not required with releases after 2015
(fixltx2e)                All fixes are now in the LaTeX kernel.
(fixltx2e)                See the latexrelease package for details.

) ("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\fontenc.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\t1enc.def"))
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\inputenc.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\utf8.def"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\t1enc.dfu")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\ot1enc.dfu")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\omsenc.dfu")))
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\upquote\upquote.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\textcomp.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\ts1enc.def"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\ts1enc.dfu"))))
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\microtype\microtype.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\graphics\keyval.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\microtype\microtype-pdftex.def")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\microtype\microtype.cfg"))
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\geometry\geometry.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\generic\oberdiek\ifpdf.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\generic\oberdiek\ifvtex.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\geometry\geometry.cfg"))
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\hyperref\hyperref.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\generic\oberdiek\hobsub-hyperref.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\generic\oberdiek\hobsub-generic.sty"))
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\oberdiek\auxhook.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\oberdiek\kvoptions.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\hyperref\pd1enc.def")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\00miktex\hyperref.cfg")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\url\url.sty"))

Package hyperref Message: Driver (autodetected): hpdftex.

("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\hyperref\hpdftex.def"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\oberdiek\rerunfilecheck.sty"))
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\hyperref\puenc.def")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\graphics\color.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\00miktex\color.cfg")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\pdftex-def\pdftex.def"))
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\fancyvrb\fancyvrb.sty"
Style option: `fancyvrb' v2.7a, with DG/SPQR fixes, and firstline=lastline fix
<2008/02/07> (tvz))
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\framed\framed.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\graphics\graphicx.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\graphics\graphics.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\graphics\trig.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\00miktex\graphics.cfg")))
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\oberdiek\grffile.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\ltxmisc\parskip.sty")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\titling\titling.sty") (tmp.aux)
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\ts1cmr.fd")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\lm\t1lmr.fd")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\microtype\mt-cmr.cfg")
*geometry* driver: auto-detecting
*geometry* detected driver: pdftex
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\hyperref\nameref.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\generic\oberdiek\gettitlestring.sty"))
("C:\Program Files (x86)\MiKTeX 2.9\tex\context\base\supp-pdf.mkii"
[Loading MPS to PDF converter (version 2006.09.02).]
) ("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\oberdiek\epstopdf-base.sty"
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\oberdiek\grfext.sty"))

LaTeX Warning: Reference `fig:nice-fig' on page 1 undefined on input line 133.

("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\lm\t1lmtt.fd")
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\lm\ts1lmtt.fd")

LaTeX Warning: File `tmp_files/figure-latex/nice-fig-1.pdf' not found on input
line 144.

! Package pdftex.def Error: File `tmp_files/figure-latex/nice-fig-1.pdf' not fo
und.

See the pdftex.def package documentation for explanation.
Type  H <return>  for immediate help.
 ...

l.144 ...cs{tmp_files/figure-latex/nice-fig-1.pdf}

? 
! Emergency stop.
 ...

l.144 ...cs{tmp_files/figure-latex/nice-fig-1.pdf}

!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on tmp.log.
Latexmk: Missing input file: 'tmp_files/figure-latex/nice-fig-1.pdf' from line
  'LaTeX Warning: File `tmp_files/figure-latex/nice-fig-1.pdf' not found on inpu
t '
Latexmk: Missing input file: 'tmp_files/figure-latex/nice-fig-1.pdf' from line
  'LaTeX Warning: File `tmp_files/figure-latex/nice-fig-1.pdf' not found on inpu
t line 144.'
Latexmk: List of undefined refs and citations:
  Reference `fig:nice-fig' on page 1 undefined on input line 133
Latexmk: Summary of warnings:
  Latex failed to resolve 1 reference(s)
Latexmk: Errors, so I did not complete making targets
Collected error summary (may duplicate other messages):
  pdflatex: Command for 'pdflatex' gave return code 256
Latexmk: Use the -f option to force complete processing,
 unless error was exceeding maximum runs of latex/pdflatex.

If I run latexmk.exe on a very simple latex doc, with no figures, such as

\documentclass{article}

\begin{document}

my text

\end{document}

then I get a PDF with no problems.

I've got the current version of MiKTeX:

> system('pdflatex --version')
MiKTeX-pdfTeX 2.9.6000 (1.40.17) (MiKTeX 2.9)
Copyright (C) 1982 D. E. Knuth, (C) 1996-2016 Han The Thanh
TeX is a trademark of the American Mathematical Society.
compiled with zlib version 1.2.8; using 1.2.8
compiled with libpng version 1.6.21; using 1.6.21
compiled with poppler version 0.42.0
compiled with jpeg version 8.4
> system('latexmk --version')
latexmk.exe: MiKTeX encountered an internal error.
Warning message:
running command 'latexmk --version' had status 1 

If I run 'latexmk --version in git bash, I get

Latexmk, John Collins, 22 April 2016. Version 4.45
> devtools::session_info()
Session info ----------------------------------------------------------
 setting  value                       
 version  R version 3.3.1 (2016-06-21)
 system   x86_64, mingw32             
 ui       RStudio (0.99.1246)         
 language (EN)                        
 collate  English_United States.1252  
 tz       Australia/Sydney            
 date     2016-06-30                  

Packages --------------------------------------------------------------
 package   * version  date       source                            
 bookdown    0.0.73   2016-06-29 Github (rstudio/bookdown@560153b) 
 devtools    1.11.1   2016-04-21 CRAN (R 3.2.5)                    
 digest      0.6.9    2016-01-08 CRAN (R 3.2.3)                    
 evaluate    0.9      2016-04-29 CRAN (R 3.2.5)                    
 htmltools   0.3.5    2016-03-21 CRAN (R 3.2.4)                    
 httpuv      1.3.3    2015-08-04 CRAN (R 3.2.3)                    
 knitr       1.13.1   2016-05-23 Github (yihui/knitr@ce40cbf)      
 magrittr    1.5      2014-11-22 CRAN (R 3.2.0)                    
 memoise     1.0.0    2016-01-29 CRAN (R 3.2.4)                    
 mime        0.4      2015-09-03 CRAN (R 3.2.2)                    
 miniUI      0.1.1    2016-01-15 CRAN (R 3.2.5)                    
 R6          2.1.2    2016-01-26 CRAN (R 3.2.4)                    
 Rcpp        0.12.5   2016-05-14 CRAN (R 3.2.5)                    
 rmarkdown   0.9.6.14 2016-06-29 Github (rstudio/rmarkdown@cb74ee5)
 rsconnect   0.4.3    2016-05-02 CRAN (R 3.2.5)                    
 shiny       0.13.2   2016-03-28 CRAN (R 3.2.4)                    
 stringi     1.1.1    2016-05-27 CRAN (R 3.3.0)                    
 stringr     1.0.0    2015-04-30 CRAN (R 3.2.0)                    
 withr       1.0.1    2016-02-04 CRAN (R 3.2.4)                    
 xtable      1.8-2    2016-02-05 CRAN (R 3.2.4)                    
 yaml        2.1.13   2014-06-12 CRAN (R 3.2.0)                    
yihui commented 8 years ago

That is really weird. Two things you may try:

  1. Does shell('latexmk -v') work?
  2. You can also temporarily rename latexmk.exe to something else (e.g. latexmk2.exe) so that rmarkdown cannot find latexmk, and falls back to pdflatex/xelatex.

BTW, what is your version of perl? system('perl --version')

benmarwick commented 8 years ago

Thanks for taking a look. latexmk.exe does seem to be part of the fault here. When I run shell('latexmk -v') I get something like an installation process. Then when I run it a second time, it gives the expected result, but the Rmd still has an error that prevents it from knitting.

> shell('latexmk -v')

======================================================================
starting package maintenance...
installation directory: "C:\Program Files (x86)\MiKTeX 2.9"
package repository: http://ctan.unsw.edu.au/systems/win32/miktex/tm/packages/
lightweight database digest: 3b240316a510d51783808e34b23c0f33
going to download 288787 bytes
going to install 25 file(s) (1 package(s))
downloading http://ctan.unsw.edu.au/systems/win32/miktex/tm/packages/latexmk.tar.lzma...
288787 bytes, 233.07 KB/Sec
extracting files from latexmk.tar.lzma...
======================================================================

Latexmk, John Collins, 22 April 2016. Version 4.45
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c latexmk -v' had status 5 
2: In shell("latexmk -v") :
  'latexmk -v' execution failed with error code 5

Second time:

> shell('latexmk -v')

Latexmk, John Collins, 22 April 2016. Version 4.45

Perl is the current version (I also recently reinstalled that, thinking it might be the problem):

> system('perl --version')

This is perl 5, version 24, subversion 0 (v5.24.0) built for MSWin32-x64-multi-thread

Copyright 1987-2016, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

Finally, I deleted latexmk.exe as you suggested, and then I could knit the PDF using output: bookdown::pdf_document2.

But, I can only knit if I did not evaluate the code chunk that has the table. If I include the table chunk, I cannot knit and get an error ! Undefined control sequence. l.161 \toprule. I guess that this is a problem with the LaTeX package booktabs, since the \toprule is the first line of the text file for the table. I've updated my MikTeX install, removed and reinstalled booktabs, but no change.

Looking at the tex file generated by knitting, I see that there is no \usepackage{bookdown} in the preamble. If I add in \usepackage{bookdown}, and remove the code chunk that makes the plot, I can compile the PDF from the tex file and get a nice table. This is my complete tex file, without any changes:

\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
  \newcommand{\euro}{€}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\PassOptionsToPackage{usenames,dvipsnames}{color} % color is loaded by hyperref
\hypersetup{unicode=true,
            pdftitle={Bookdown: A single document},
            pdfauthor={Ben Marwick},
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{{#1}}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{{#1}}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\ImportTok}[1]{{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{{#1}}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{{#1}}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{{#1}}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{{#1}}}}
\newcommand{\BuiltInTok}[1]{{#1}}
\newcommand{\ExtensionTok}[1]{{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{{#1}}}
\newcommand{\RegionMarkerTok}[1]{{#1}}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{{#1}}}}
\newcommand{\NormalTok}[1]{{#1}}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}

%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}

%%% Change title format to be more compact
\usepackage{titling}

% Create subtitle command for use in maketitle
\newcommand{\subtitle}[1]{
  \posttitle{
    \begin{center}\large#1\end{center}
    }
}

\setlength{\droptitle}{-2em}
  \title{Bookdown: A single document}
  \pretitle{\vspace{\droptitle}\centering\huge}
  \posttitle{\par}
  \author{Ben Marwick}
  \preauthor{\centering\large\emph}
  \postauthor{\par}
  \predate{\centering\large\emph}
  \postdate{\par}
  \date{May 23, 2016}

% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

\begin{document}
\maketitle

{
\setcounter{tocdepth}{2}
\tableofcontents
}
Can we cross-reference before the figure? Figure \ref{fig:nice-fig}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mar =} \KeywordTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\DecValTok{4}\NormalTok{, .}\DecValTok{1}\NormalTok{, .}\DecValTok{1}\NormalTok{))}
\KeywordTok{plot}\NormalTok{(pressure, }\DataTypeTok{type =} \StringTok{'b'}\NormalTok{, }\DataTypeTok{pch =} \DecValTok{19}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{figure}[htbp]
\centering
\includegraphics{tmp_files/figure-latex/nice-fig-1.pdf}
\caption{\label{fig:nice-fig}Here is a nice figure!}
\end{figure}

How about cross-referencing after the figure? Figure \ref{fig:nice-fig}

What about tables? Here is Table \ref{tab:nice-tab}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{knitr::}\KeywordTok{kable}\NormalTok{(}
  \KeywordTok{head}\NormalTok{(iris, }\DecValTok{10}\NormalTok{), }\DataTypeTok{caption =} \StringTok{'Here is a nice table!'}\NormalTok{,}
  \DataTypeTok{booktabs =} \OtherTok{TRUE}
\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{table}

\caption{\label{tab:nice-tab}Here is a nice table!}
\centering
\begin{tabular}[t]{rrrrl}
\toprule
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
\midrule
5.1 & 3.5 & 1.4 & 0.2 & setosa\\
4.9 & 3.0 & 1.4 & 0.2 & setosa\\
4.7 & 3.2 & 1.3 & 0.2 & setosa\\
4.6 & 3.1 & 1.5 & 0.2 & setosa\\
5.0 & 3.6 & 1.4 & 0.2 & setosa\\
\addlinespace
5.4 & 3.9 & 1.7 & 0.4 & setosa\\
4.6 & 3.4 & 1.4 & 0.3 & setosa\\
5.0 & 3.4 & 1.5 & 0.2 & setosa\\
4.4 & 2.9 & 1.4 & 0.2 & setosa\\
4.9 & 3.1 & 1.5 & 0.1 & setosa\\
\bottomrule
\end{tabular}
\end{table}

\end{document}

So maybe the tex template for bookdown::pdf_document2 needs \usepackage{booktabs} added to it?

yihui commented 8 years ago

Great. Now there are two issues left:

  1. latexmk has been correctly installed, but there is still an error when you compile the Rmd. What exactly is the error?
  2. The \toprule issue is indeed due to the booktabs package not being loaded, and that can be solved by adding tables: yes to your YAML metadata (it is a top-level option), or includes -> in_header, then include a file that contains \usepackage{booktabs}.
benmarwick commented 8 years ago

Thanks, the error message in the Rmd console is:

! Undefined control sequence.
l.167 \toprule

Error: Failed to compile tmp.tex. See tmp.log for more info.
In addition: Warning message:
running command '"pdflatex" -halt-on-error -interaction=batchmode "tmp.tex"' had status 1 
Execution halted

But, if I add tables: yes to the YAML, as you suggest, then I can get a PDF from the Rmd.

The PDF includes the plot and the table. There are no warnings or errors. So, that seems to solve it.

Thanks very much for your patient help to make this work!

yihui commented 8 years ago

Excellent! Now I can stop scratching my head :) Thanks for bearing with me in the debugging process.

github-actions[bot] commented 3 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.