Missing XeLateX when trying to use NBconvert (to PDF) on Windows - jupyter-notebook

Jupyter's default export to PDF, like the command-line NBconvert tool, are telling me that I can't convert my notebook to PDF because the PDF conversion goes via xelatex. The error message refers me to this help page, so I installed MiKTeX. I have a xelatex folder in AppData\Local\Programs\MiKTeX 2.9\tex, but I'm not sure what exactly I should add to my PATH.
If it's helpful, here is some of the code in nbconvert's pdf.py:
def run_latex(self, filename):
"""Run xelatex self.latex_count times."""
def log_error(command, out):
self.log.critical(u"%s failed: %s\n%s", command[0], command, out)
return self.run_command(self.latex_command, filename,
self.latex_count, log_error)

Related

Running R tikzDevice with tinytex: Help troubleshooting `! LaTeX Error: File `tikz.sty' not found`

I am trying to generate a tikz plot using R/ggplot2 via the tikzDevice package. I've had to wade through many errors as I figured out how to install R and tinytex on my docker image, but I've run into the following error which has simply stumped me.
When I run the code that generates my plot, this is the error I get:
Error in getMetricsFromLatex(TeXMetrics, verbose = verbose) :
TeX was unable to calculate metrics for:
\char77
Run the following commands for diagnosis:
tikzTest()
tikzTest("\\char77")
Common reasons for failure include:
* The string contains a character which is special to LaTeX unless
escaped properly, such as % or $.
* The string makes use of LaTeX commands provided by a package and
the tikzDevice was not told to load the package.
The TeX and log files used for the calculation can help diagnose the
problem. If these files are missing, rerun the plot and make sure to
keep the R session open.
TeX file: tikzStringWidthCalc.tex
Log file: tikzStringWidthCalc.log
Calls: <Anonymous> ... descentDetails.text -> grid.Call -> <Anonymous> -> getMetricsFromLatex
Execution halted
And the output of tikzDevice::tikzTest() is as follows:
Active compiler:
/home/jovyan/.TinyTeX/bin/x86_64-linux/pdflatex
pdfTeX 3.141592653-2.6-1.40.23 (TeX Live 2021)
kpathsea version 6.3.3
Measuring dimensions of: A
Running command: '/home/jovyan/.TinyTeX/bin/x86_64-linux/pdflatex' -interaction=batchmode -halt-on-error -output-directory '/tmp/RtmpLhckyy/tikzDevice29a5e00452e' 'tikzStringWidthCalc.tex'
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2021) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
TeX was unable to calculate metrics for:
A
Contents of TeX file tikzStringWidthCalc.tex:
\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage[active,tightpage,psfixbb]{preview}
\PreviewEnvironment{pgfpicture}
\setlength\PreviewBorder{0pt}
\usepackage{tikz}
\usepackage[T1]{fontenc}
\usetikzlibrary{calc}
\batchmode
\begin{document}
\begin{tikzpicture}
\node[inner sep=0pt, outer sep=0pt, scale=1] (TeX) {A};
\path let \p1 = ($(TeX.east) - (TeX.west)$),
\n1 = {veclen(\x1,\y1)} in (TeX.east) -- (TeX.west)
node{ \typeout{tikzTeXWidth=\n1} };
\end{tikzpicture}
\end{document}
Contents of log file tikzStringWidthCalc.log:
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2021) (preloaded format=pdflatex 2022.3.16) 16 MAR 2022 23:30
entering extended mode
restricted \write18 enabled.
%&-line parsing enabled.
**tikzStringWidthCalc.tex
(/tmp/RtmpLhckyy/tikzDevice29a5e00452e/tikzStringWidthCalc.tex
LaTeX2e <2021-11-15> patch level 1
L3 programming layer <2022-02-24> (/home/jovyan/.TinyTeX/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(/home/jovyan/.TinyTeX/texmf-dist/tex/latex/base/size10.clo
File: size10.clo 2021/10/04 v1.4n Standard LaTeX file (size option)
)
\c#part=\count185
\c#section=\count186
\c#subsection=\count187
\c#subsubsection=\count188
\c#paragraph=\count189
\c#subparagraph=\count190
\c#figure=\count191
\c#table=\count192
\abovecaptionskip=\skip47
\belowcaptionskip=\skip48
\bibindent=\dimen138
)
! LaTeX Error: File `tikz.sty' not found.
Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: sty)
Enter file name:
! Emergency stop.
<read *>
l.4 ^^M
Here is how much of TeX's memory you used:
235 strings out of 480236
3181 string characters out of 5895451
287032 words of memory out of 5000000
18278 multiletter control sequences out of 15000+600000
469259 words of font info for 28 fonts, out of 8000000 for 9000
14 hyphenation exceptions out of 8191
34i,0n,38p,138b,36s stack positions out of 5000i,500n,10000p,200000b,80000s
! ==> Fatal error occurred, no output PDF file produced!
As you can see I am using tinytex, but I have installed the recommended packages for this setup as described in the tinytex documentation (i.e., I've already ran tlmgr install pgf preview xcolor).
I've read every post I could possibly find on this subject but am coming up empty. Any tips to help me troubleshoot this problem would be greatly appreciated.
it would help if you had a minimum reproducible example to check on our end.
This said, the message LaTeX Error: File `tikz.sty' not found. suggests that a possible solution would be for you to throw:
library(tinytex)
tlmgr_search('/tikz.sty') # search for tikz.sty
tlmgr_install('pgf') # install the psnfss package
tlmgr_update() # update everything
since tlmgr_search('/tikz.sty') gives you
tlmgr search --file --global "/tikz.sty"
tlmgr.pl: package repository https://ctan.mirror.garr.it/mirrors/ctan/systems/texlive/tlnet
pgf:
texmf-dist/tex/latex/pgf/frontendlayer/tikz.sty
but then you already installed pgf... that's where tikz.sty lives.

Rmarkdown does not follow same paths as R script and console commands

In Rmarkdown, I cannot read files that I can read from the console and in an R script, because Rmarkdown is not following the same paths as my R scripts and console commands.
Here is a minimum reproducible example:
Create new project test.Rproj
Create a subdirectory called scripts
Run the following R Script scripts/test.R:
test <- as.data.frame(c(1, 2, 3))
dir.create("data")
write.csv(test, "data/test.csv")
rm(test)
test <- read.csv("data/test.csv")
Quit R, and reopen test.Rproj.
Knit the following Rmarkdown document (scripts/test.Rmd):
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
_```
```{r read-data}
test <- read.csv("data/test.csv")
_```
Yields the following error: Quitting from lines 12-13 (test.Rmd)
Error in file(file, "rt") : cannot open the connection
Calls: ... withVisible -> eval -> eval -> read.csv -> read.table -> file
Execution halted
(Note, the backticks in the .Rmd file are properly specified -- I added underscores above so that the backticks appeared in the code block.)
Two seemingly related issues:
I can read the test.csv file via Rmarkdown if it is in the scripts subdirectory, rather than the data subdirectory.
When I run list.files() from the console or script, I receive of list of files in the top-level directory (i.e., where test.Rproj is located), including the data and scripts subdirectories. When I run list.files() from Rmarkdown, I get a list of files in the scripts subdirectory.
How can I fix this problem?
Session info:
R version 4.1.0 (2021-05-18)
RStudio version 1.4.1717
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.5.1
Try something that looks like this as I am not sure of the nature of your `R Markdown.
test <- readRDS(here::here("data/test_data.rds"))
The bottom line is to use the here function from the here package.
When you knit a document in RStudio, by default the working directory is set to the current directory of the Rmd document (so that would be the "scripts" folder). Since the "scripts" folder does not contain the "data" directory, you get that error. You can change the default to use the project root directory if you prefer. That's an option in the RStudio Global Options menu.
See See https://bookdown.org/yihui/rmarkdown-cookbook/working-directory.html for more info

Not able to compile when specifying CSL in R Markdown

I'm writing an article in R markdown using a citation library generated in Mendeley. My document compiles fine with references when I'm not specifying citation style.
bibliography: library.bib
When I specify the CSL:
bibliography: library.bib
csl: stroke.csl
I get the following error:
pandoc-citeproc.exe: PandocResourceNotFound "stroke.csl"
Error running filter C:/PROGRA~1/Pandoc/pandoc-citeproc.exe:
Filter returned error status 1
Error: pandoc document conversion failed with error 83
Execution halted
I have tried different citationstyles from:
https://github.com/citation-style-language/styles
I appreciate any help.
This error is caused by pandoc-citeproc that cannot find the .CSL file. Check whether the .CSL file stroke.csl is in the same directory as the .RMD file you knit.

Unable to knit pdf, receiving tinytex error with failed tlmgr search

Using
- RStudio Version 1.2.1335
- R version
I am able to knit the R markdown file to HTML and Word, but fail with PDF. I keep receiving the same error message telling me that it is unable to locate the "Palatino" font.
The file is successfully processed (all chunks run).
I have tried re-installing and resetting Miktex and Tinytex, as well as R itself. I am pretty new to this, so I am unsure how to proceed. Should I install TexLive?
This is the header of the file I am trying to run:
--
title: "Assignment 1"
fontsize: 11pt
header-includes: \usepackage{fancyhdr}
geometry: "left=.5in,right=.5in,top=1in,bottom=1in"
output:
pdf_document:
latex_engine: xelatex
md_extensions: +inline_notes
number_sections: no
word_document: default
mathfont: Palatino
monofont: Courier
mainfont: Palatino
sansfont: Helvetica
subtitle: \textbf{Due Wednesday, 11 September 2019 (beginning of class)}
fontfamily: mathpazo
---
And this is the error I continue to receive, without rendering of a PDF file:
output file: PBHLTH252-Assignment-1_post.knit.md
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS PBHLTH252-Assignment-1_post.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash+inline_notes --output PBHLTH252-Assignment-1_post.tex --template "C:\Users\UYser\Documents\R\win-library\3.6\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --pdf-engine xelatex --variable graphics=yes --lua-filter "C:/Users/UYser/Documents/R/win-library/3.6/rmarkdown/rmd/lua/pagebreak.lua" --variable "compact-title:yes"
tlmgr search --file --global "/Palatino."
! Package fontspec Error: The font "Palatino" cannot be found.
! kpathsea: Running mktextfm Palatino
! The command name is C:\Users\UYser\AppData\Roaming\TinyTeX\bin\win32\mktextfm
! kpathsea: Appending font creation commands to missfont.log.
Error: Failed to compile PBHLTH252-Assignment-1_post.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See PBHLTH252-Assignment-1_post.log for more info.
In addition: Warning message:
In parse_packages(logfile, quiet = c(TRUE, FALSE, FALSE)) :
Failed to find a package that contains Palatino.
Execution halted
I tried following this thread, https://github.com/yihui/tinytex/issues/81, with no success:
Running this suggested code
tinytex::tlmgr_search("/Palatino[.](tfm|afm|mf|otf)")
results in:
package repository http://ctan.math.illinois.edu/systems/texlive/tlnet (not verified: gpg unavailable)
Running this one
tinytex::tlmgr_install("multirow")
gives me the following error:
tlmgr install Palatino.
'afm' is not recognized as an internal or external command,
operable program or batch file.
tlmgr update --self
tlmgr.pl: package repository http://ctan.math.illinois.edu/systems/texlive/tlnet (not verified: gpg unavailable)
tlmgr.pl: no self-updates for tlmgr available.
tlmgr install Palatino.
'afm' is not recognized as an internal or external command,
operable program or batch file.
Any help is appreciated!
It looks like you got the template from a Mac user while you are on Windows. The fontnames are different there. Instead of Palatino you could use Palatino Linotype. I am not sure if Helvetica and Courier will work. If not, you could use Arial and Courier New.
However, that sounds like a rather ugly font combination. Palatino is a very fine font, but neither Helvertica/Arial nor Courier (New) fit well. Besides, I don't think that normal Palatino fonts are set-up to be used for math. You could use Asana Math instead, which comes with MikTeX/TinyTeX.
I used to have similar problems. My solution sounds naive (or silly), but it worked perfectly for me. Instead of debugging, in RStudio:
install rticles package
start a new R Markdown file
choose one of journal article templates such as PNAS Journal Article, Springer Journal Article, or Elsevier Journal Article...
knit the template.
Then, it will be trying to automatically install missing LaTeX packages...
You may want to try a few journal templates. It worked for me, and I hope this will solve your problem as well. Enjoy it!

Error in creating PDF file from R Markdown (Failed with error 43)

I am trying to create a PDF file using R Markdown. I received error 43 when I ran it. I tried create word file and HTML using R Markdown and it works fine. Only the PDF file does not work.
processing file: Test_New.Rmd
|...................... | 33%
ordinary text without R code
|........................................... | 67%
label: unnamed-chunk-1 (with options)
List of 1
$ tidy: logi TRUE
|.................................................................| 100%
ordinary text without R code
/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS
Test_New.utf8.md --to latex --from
markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --
output Test_New.pdf --template/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --latex-engine /usr/local/bin/pdflatex --variable graphics=yes --variable 'geometry:margin=1in'
output file: Test_New.knit.md
dyld: Library not loaded: /usr/local/lib/libjpeg.8.dylib
Referenced from: /usr/local/bin/pdflatex
Reason: image not found
pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
Execution halted
My code on R Markdown is the following.
---
title: "Test R Markdown"
author: "Sam Ng"
date: "September 1, 2017"
output:
pdf_document: default
html_document: default
word_document: default
---
# 1. R commands
Testing R commands
```{r,tidy=TRUE}
vector=c(1,2,3) # Create a new vector with three entries
max(vector) # Returns the maximum of a vector
min(vector) # Returns the minimum of a vector
```
I spent hours trying to resolve this. I installed Miktex, but was unable to get the packages directly from the package manager due to sitting behind a firewall at work. I finally was able to resolve this using the following steps:
Install Miktex
Create a folder to install Miktex packages, e.g. "C:\miktex_pkgs"
Go to the CTAN package archive and download the following .tar.lzma files:
-fancyvrb
-framed
-microtype
-miktex-zzdb1-2.9
-miktex-zzdb2-2.9
-mptopdf
-titling
-upquote
-url
Move the above .tar.lzma files to the "C:\miktex_pkgs" folder
Set your packages to download from the local repository: "C:\miktex_pkgs". To do this, go to:
"Miktex Settings" - this is an application
Click Packages
Change Package Repository to "C:\miktex_pkgs"
Go to "Miktex Package Manager" application and install the following packages by right-click, install:
List item
fancyvrb
framed
microtype
mptopdf
titling
upquote
url

Resources