When I try to render the latest version of the book R for Data Science (R4DS), I get as far as LaTeX compilation, then am stopped by the following error message.
! Text line contains an invalid character.
l.406 #> -- ^^[
[1mAttaching packages^^[[22m --------------------------------...
Error: LaTeX failed to compile _main.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See _main.log for more info.
>
This corresponds to the part of the R4DS book where we are shown how to load the tidyverse and, looking at the _main.tex file, I see many lines with what look like ANSI escape sequences starting on this line. They have the form ^[[1m, ^[[22m, and so on. I manually compiled the LaTeX output using lualatex and found that there are dozens if not hundreds of examples of this throughout the book. I suspected it was because I was using the colorout package in R, but it appears that that package is required, so others who are rendering successfully must be using it too. I believe I have successfully updated all relevant packages.
It looks like I "solved" the problem by changing an option in the _common.R file from crayon.enabled=TRUE to crayon.enabled=FALSE. This removed the ANSI escape sequences from the book. Previously I had tried setting options(crayon.enabled=FALSE) in my R session, but this was evidently being overridden by the setting in _common.R.
Update: 23 Nov 2022
The process for rendering the files is completely different now because of the switch to Quarto. Here's how I did it.
Rscript -e 'update.packages()'
Rscript -e 'install.packages('quarto')'
Rscript -e 'devtools::install_github("hadley/r4ds")
git clone https://github.com/hadley/r4ds.git
cd r4ds
Next, I wrote a small perl script to avoid the error messages I was getting about trying to render html material to pdf. (I'm omitting a lot of dead-ends I encountered in the process.)
#!/usr/bin/perl
use File::Slurp qw(prepend_file);
my #files = glob( '*.qmd' );
my $header = "\n---\nprefer-html: true\n---\n\n";
foreach my $file (#files) {
prepend_file($file, $header);
}
I ran the above script in the r4ds directory.
Next I loaded R and did the following:
library(quarto)
quarto_render("index.qmd", output_format = "pdf")
The above failed with the error message: "\begin{document} not found". Luckily, the aborted process leaves an index.tex file I can process and also gives a line number for the error. I went to that line number in the index.tex file and deleted the block of html I found there.
After that, I ran
lualatex index.tex
twice and got a successful render, minus the cover page. (You could presumably run xelatex index.tex instead.) There are a lot of problems with my render, such as the plots being too large to fit on the page. If I decide to spend time fixing them (unlikely, since Hadley seems to want us to use the online version) I'll modify this answer.
I can build the gitbook of the demo just fine.
If I attempt to build a pdf, the .tex file is made by pandoc, but xelatex compilation bombs. I get a message in the Build window that's incomplete.
If I manually compile in the shell, I get the following output. Short story-- it would seem that there's code somewhere, probably in pandoc, that uses
\xetex_if_engine:F and
\luatex_if_engine:TF
which apparently have been non-functional since 1-1-2017.
But I can't be the first person to try to compile a pdf since January. Any ideas?
The LaTeX log shows LaTeX2e <2011/06/27>, which is too old, and you will need to refresh your filename database (FNDB) as instructed here,
Up front: using pandoc() in knitr, it complains when trying to compile .md or .Rmd into a PDF.
I'm streamlining the process for reproducible research, as has been documented in many places. I'm using pandoc and knitr and producing great documents. I'm also trying to streamline for some co-workers who are not as adept with programming, yet we're trying to use similar files. There are several options for "user friendly" markdown-centric editors, and for several reasons I'm leaning on RStudio (for them, emacs/ess for me, but that's different).
My workflow: give them a markdown (.md or .Rmd) file and have them be able to make changes and optionally re-render it into a PDF. Unfortunately, RStudio does not (AFAICT) allow setting templates or other arbitrary pandoc configuration parameters (e.g., chapters, number-sections), so using pandoc() in R/knitr makes a lot of sense here.
Using whitepaper.Rmd as the input file, I run pandoc('whitepaper.Rmd', 'pdf') in R and immediately get:
> pandoc('whitepaper.Rmd', 'pdf')
executing pandoc -t latex --standalone --smart --number-sections --template=report.tex -f markdown -t pdf -o whitepaper.pdf "whitepaper.Rmd"
pandoc.exe: cannot produce pdf output with pdf writer
Error in (function (input, format, ext, cfg) : conversion failed
I explicitly have "t:latex" in my knitr-specific header, though without it, pandoc() is still adding "-t pdf" to the system call, something that pandoc.exe does not accept.
With troubleshooting, the command works just fine if I remove '-t pdf', so it seems that there is nothing wrong with the input file itself:
> system('pandoc -t latex --standalone --smart --number-sections --template=report.tex -f markdown -o whitepaper.pdf "whitepaper.Rmd"')
There have been numerous other conversations regarding this topic: 14586177, 14508429, 15258233, and the heavily-discussed 11025123. They all resolve to solutions that require command-line work, extra middle-steps, external Makefiles, or knit2pdf() (which uses texi2pdf, not desired).
The constraints as I see them:
operate easily within the R environment;
take advantage of Yihui's "<!--pandoc ... -->" in-file configuration (which allows
me to continue to switch arbitrarily between my templates, for one of several examples);
preferably, execute this with a single "standardized" command (i.e., "pandoc('whitepaper.Rmd', 'pdf')").
... so that, once the parameters are set in-file, editing and re-rendering is relatively brain-dead.
I can patch and overwrite Yihui's knitr:::pandoc_one() to remove the offending addition of '-t' and format, but I wonder what side-effects that might have elsewhere. This solution isn't sustainable nor "The Right Way (tm)".
Suggestions for "Right Ways (tm)" to solve this problem? Am I missing an easy/obvious solution?
BTW: thanks, Yihui Xie, for knitr, and John MacFarlane for pandoc. Awesomeness!
(Perhaps I could submit patch suggestions to either or both to work around for my use-case, though if it's just me then it might not be worthwhile.)
I think all there information you need is there in ?pandoc, which includes the example of running system("pandoc -h") to see possible output formats. From that you learn that
Output formats: asciidoc, beamer, context, docbook, docx, dzslides,
epub, epub3,
fb2, html, html5, json, latex, man, markdown, markdown_github,
markdown_mmd, markdown_phpextra, markdown_strict, mediawiki,
native, odt, opendocument, opml, org, pdf*, plain, revealjs,
rst, rtf, s5, slideous, slidy, texinfo, textile
[*for pdf output, use latex or beamer and -o FILENAME.pdf]
So basically format = "pdf" is invalid, you should use pandoc("tmp.Rmd", format = "latex", ext = "pdf") (and acutally the ext="pdf" part is the default, according to ?pandoc, so all you really need is pandoc("tmp.Rmd", "latex")). As for why pandoc('whitepaper.Rmd', 'pdf') resulted in a call with -t pdf, well, you told it to do that in the second argument to your pandoc() call.
This is more of an annoyance than it is a problem, but is there any way to prevent the line "overflow" that occurs when documentation in R is compiled and a line is too long?
A snippet of some documentation created with R CMD Rd2pdf [options] files:
I can't find mention of this anywhere, and the only options for Rd2pdf are:
Options:
-h, --help print short help message and exit
-v, --version print version info and exit
--batch no interaction
--no-clean do not remove created temporary files
--no-preview do not preview generated PDF file
--encoding=enc use 'enc' as the default input encoding
--outputEncoding=outenc
use 'outenc' as the default output encoding
--os=NAME use OS subdir 'NAME' (unix or windows)
--OS=NAME the same as '--os'
-o, --output=FILE write output to FILE
--force overwrite output file if it exists
--title=NAME use NAME as the title of the document
--no-index don't index output
--no-description don't typeset the description of a package
--internals typeset 'internal' documentation (usually skipped)
Sorry for the spoiler: One solution is not using roxygen2 in extremis for package maintenance. Why not maintain your DESCRIPTION manually? You don't need many changes and it looks so much better...
The 'Collate' is really not needed for the vast majority of packages.
Why not follow the tradition of many bioconductor packages, 'Matrix' etc, of
putting S4 class definitions (including reference classes) in a file 'AllClasses.R' and maybe use 'AllGenerics.R' as well, and for the rest, collation order should not matter.
Running LyX on Windows 7 with MiKTeX. I was trying to export LyX's "Embedded Objects tutorial" to PDF, but got this error:
Undefined control sequence
\hline
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
The offending code seems to be
\let\myHlineC\hline
\renewcommand{\hline}
{\arrayrulecolor{red}\myHlineC\arrayrulecolor{black}}
(section 2.11.2 of the document).
May or may not be relevant: The document's preamble.
You can download the pdf of the manual online here:
http://wiki.lyx.org/LyX/DocumentationDevelopment#EmbeddedObjects
Do you have the following packages installed?
arydshln, colortbl, diagbox, lettrine, marginnote, picinpar and sidecap
Although it says that you should be able to export without them, I've found that sometimes they are required. If this is true in your case, can you post which package was causing the problem so we can try to fix the document? To find this out, install the above packages one by one and test if you can export after each one. Don't forget to reconfigure LyX via Tools > Reconfigure after each install of a new package.