xelatex, luatex issues when building pdf of demo book - r

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,

Related

IDE with LaTeX and R support: Inline output in .Rmd notebooks and weaving LaTeX document with R code

I'm trying to improve my workflow when working with R and generating documentation. I've been going between TeXStudio, JupyterLab and RStudio for a while, and I'm trying to improve my workflow. TeXStudio has limited R support, and RStudio limited support for LaTeX.
VS Code has support for multiple languages, including R and LaTeX. The fact that it can run both Jupyter notebooks, R notebooks, and LaTeX, and has plugins for other languages as well, makes it seem desirable. However, I am unable to find documentation on how to configure it to work with R and LaTeX code in the same file. In addition, I am unable to configure R notebooks to allow inline code execution output.
However, I am unable to (a) set up code execution output under the code for .Rmd notebooks, and (b) I can't figure out how to weave .Rnw (R/LaTeX) documents with Sweave/knitr.
I'm trying to find an IDE that would include features like:
Markdown, code and code execution output in the same document
Auto R and LaTeX code completion
Automatic display of R function documentation
Spell check
Simple R console access
Compile .Rnw
Syntax highlighting for both R code and LaTeX code
I am, primarily, requesting ways to configure VS Code, or, secondly, way to configure another IDE that can meet my requirements. A tutorial on this would be much appreciated.
After a bit of digging around, I found that VS Code does nearly all the things I need.
Auto R and LaTeX code completion, Display of R function documentation in a tab in VS Code, Simple R console access, and Syntax highlighting for both R code and LaTeX code:
The R and LaTeX Workshop extensions, will provide highlighting and autocompletion of code in both languages. By installing R, you can easily open a session in a terminal window in VS Code, and from there open documentation inside VS Code.
Spell check
Code Spell Checker offers spell check for multiple languages. Install the extension and any desired dictionaries, and set the langauges you want to be included in the extension settings.
Compile .Rnw files
Turns out LaTeX Workshop can actually do this by default.
Markdown, code and code execution output in the same document
This is the only thing VS Code doesn't do as far as I can tell. It can compile .Rmd files, however, but the output can only be seen in the compiled PDF. I consider this less important, since I can use Jupyter notebooks instead.

Trying to render R4DS to pdf

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.

bookdown::render_book vs. rmarkdown::render_site to build all outputs

I've got a Bookdown book for which I'd like to build a GitBook site as well as PDF and EPUB downloads. I will use Travis to build all 3 outputs, and the PDF and EPUB will be available for download from the GitBook site.
The bookdown-demo calls bookdown::render_book once for each output in _build.sh.
However, according to the logs in RStudio, the Build Book button, when building All Formats, uses rmarkdown::render_site(encoding = 'UTF-8') to build all outputs in a single command.
I'd like to ensure what happens on my CI server is exactly what happens in my IDE, so it seems like I should have Travis call rmarkdown::render_site rather than several invocations of bookdown::render_book as is done by the bookdown-demo. However, Yihui is the expert, and he has chosen to use the latter approach.
So, my question: what is the best script to invoke on a continuous integration server like Travis when multiple outputs will be built?
In bookdown projects, they usually don't make a difference, because rmarkdown::render_site() eventually calls bookdown::render_book() to render your book. Feel free to use either way.
The only exception is when your index.Rmd does not contain the field site: bookdown::bookdown_site. In that case, rmarkdown::render_site() won't work, because it doesn't know this is supposed to be a bookdown project.
BTW, to render all output formats with bookdown::render_book(), you can use the argument output_format = 'all'.

knitr pandoc: "cannot produce pdf output with pdf writer"

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.

More efficient R / Sweave / TeXShop work-flow?

I've now got everything to work properly on my Mac OS X 10.6 machine so that I can create decent looking LaTeX documents with Sweave that include snippets of R code, output, and LaTeX formatting together. Unfortunately, I feel like my work-flow is a bit clunky and inefficient:
Using TextWrangler, I write LaTeX code and R code (surrounded by <<>>= above and # below R code chunk) together in one .Rnw file.
After saving changes, I call the .Rnw file from R using the Sweave command
Sweave(file="/Users/mymachine/Documents/Assign4.Rnw",
syntax="SweaveSyntaxNoweb")
In response, R outputs the following message:
You can now run LaTeX on 'Assign4.tex'
So then I find the .tex file (Assign4.tex) in the R directory and copy it over to the folder in my documents ~/Documents/ where the .Rnw file is sitting (to keep everything in one place).
Then I open the .tex file (e.g. Assign4.tex) in TeXShop and compile it there into pdf format. It is only at this point that I get to see any changes I have made to the document and see if it 'looks nice'.
Is there a way that I can compile everything with one button click? Specifically it would be nice to either call Sweave / R directly from TextWrangler or TeXShop. I suspect it might be possible to code a script in Terminal to do it, but I have no experience with Terminal.
Please let me know if there's any other things I can do to streamline or improve my work flow.
I use a Makefile of the following form for my Sweave documents:
pdf: myfile.tex
R CMD texi2pdf myfile.tex
myfile.tex: myfile.Rnw
R CMD Sweave myfile.Rnw
Then I can build the document in one step in the Mac OS Terminal by running the command make pdf
I'm sure there is a way to bring this closer to your one-click goal in Mac OS X, but this works well enough for me.
One-click Sweaving is easy to do in TeXShop using the Sweave.sh script by Gregor Gorjanc.
Get it from http://cran.r-project.org/contrib/extra/scripts/Sweave.sh and put it in your ~/Library/TeXShop/bin/ folder.
Then add the following files to your ~/Library/TeXShop/engines/ folder:
As Sweave.engine:
#!/bin/bash
~/Library/TeXShop/bin/Sweave.sh -ld "$1"
As SweaveNoClean.engine:
#!/bin/bash
~/Library/TeXShop/bin/Sweave.sh -nc -ld "$1"
You'll have to set the permissions on Sweave.sh and the two engine files to allow execution.
To Sweave with one click, restart TeXShop after adding these files, open the Sweave document (with Rnw extension) and in the dropdown menu above the document window, change it from LaTeX to Sweave or SweaveNoClean.
BEWARE: The "Sweave" option wll clean up after itself, deleting all the extra files LaTeX and Sweave creates. If your file is called myfile.Rnw, this will include files called myfile.R and myfile.tex. So a word to the wise: make sure the basename of your Rnw file is unique; then nothing unexpected will be written over and then deleted.
The SweaveNoClean option does not clean up after itself. This makes sure you don't delete anything unexpected; though it could still write over a file called myfile.tex if you Sweave a myfile.Rnw. This also doesn't delete any graphics that have been created, in case you want to have them separate from your full typeset document.
On the bash shell command line:
R CMD Sweave foo.Rnw && pdflatex foo.tex
Runs Sweave, and if that succeeds it goes on to do pdflatex. Out pops a pdf. If you've got this in a bash Terminal then just hit up-arrow to get it back and do it again. And again. And Again.
Makefile solution also good.
RStudio has a button that does this in one go. One caveat is that it runs in its own session, so any workspace variables you may have set are ignored.
Just a note: you can actually call things like pdflatex etc. directly from R using texi2dvi (in the tools package). For example:
Sweave(file="/Users/mymachine/Documents/Assign4.Rnw")
texi2pdf("Assign4.tex")
would compile your Rnw file into a pdf. Thus, no need to leave R to handle the tex->pdf step.
I use these (saved as sweave.engine and sweavebibtex.engine) for custom engines in texshop. I usually work up a code chunk in R, then copy the block into the rnw file I have open in texshop. I'd love a solution that lets me do syntax highlighting and spelling correction of R and tex in the same document (that isnt emacs).
#!/bin/bash
echo 'SWEAVE | PDFLATEX. Custom engine--Akasurak-16Nov2009'
export PATH=$PATH:/usr/texbin:/usr/local/bin
R CMD Sweave "$1"
pdflatex "${1%.*}"
and the second, for doing bibtex as well:
#!/bin/bash
date
before="$(date +%s)"
echo 'SWEAVE | PDFLATEX | BIBTEX | PDFLATEX | PDFLATEX. Custom engine--Akasurak-16Nov2009'
#Updated 20Jul2010 for auto including Sweave.sty
export PATH=$PATH:/usr/texbin:/usr/local/bin
R CMD Sweave "$1"
R CMD pdflatex "${1%.*}"
bibtex "${1%.*}.aux"
R CMD pdflatex "${1%.*}"
R CMD pdflatex "${1%.*}"
after="$(date +%s)"
elapsed_seconds="$(expr $after - $before)"
date
echo Elapsed time '(m:s)': $(date -r $elapsed_seconds +%M:%S)
Can't say they are the best way of doing things, but they do work.
I use either Aquamacs or Eclipse to do the editing of the .Rnw file, then I use the following shell function to compile & view it:
sweaveCache () {
Rscript -e "library(cacheSweave); setCacheDir(getwd());
Sweave('$1.Rnw', driver = cacheSweaveDriver)" &&
pdflatex --shell-escape $1.tex &&
open $1.pdf
}
Notice that I'm using the cacheSweave driver, which helps avoid constantly re-executing code sections that take a long time to run.
BTW, I'm also trying to switch over to Babel instead of Sweave; not sure which I'll end up using more often, but there are definitely aspects of Babel that I like.
The best solution is here: you create a new *.engine for TeXShop to use, then typeset using the shortcut or the 1 button.
http://cameron.bracken.bz/sweave-for-texshop
Cameron is also very responsive, so I highly recommend his solution.
If you are open to switching to a (paid) solution, TextMate has a Sweave plugin that takes you from .Rnw to PDF in one step: Sweave, typeset, and view. Combined with Skim, which can be configured to reload PDFs, it makes tweaking files pretty easy.
I had this same issue (I use Mac OSX) and I opted to download Eclipse Classic 3.6.2. and then installed the StatET plugin. It's a bit hairy to get set up but once you do this environment is nice because you can one-click compile your .Rwn Sweave document using pdflatex and set options for your favorite viewer so the .pdf automatically pops up when you compile like it does in TeXShop. You can do this in TeXShop as well, but TeXShop is lousy for debugging .Rnw files and it doesn't highlight the R-code in the .Rwn file. In Eclipse you can customize the syntax highlighting (not the greatest from the Texclipse end, but ok) so that you can easily distinguish between your R and LaTeX code. You can also launch the R console from within Eclipse and it has a graphical object browser. Anyway, I could go on. If you want details about how to get it all installed, message me.
Guess I'm late to the party on this, but I put together a webpage that documents my Sweave workflow based on Eclipse (with one-touch sweave):
http://www.stanford.edu/~messing/ComputationalSocialScienceWorkflow.html

Resources