Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make RStudio use user-specified TeX engines? #70

Closed
CLRafaelR opened this issue Sep 4, 2018 · 1 comment
Closed

How to make RStudio use user-specified TeX engines? #70

CLRafaelR opened this issue Sep 4, 2018 · 1 comment

Comments

@CLRafaelR
Copy link

@yihui

Related to this question, I would like to ask what I should configure to use any TeX engines, rather than pdflatex when I knit a document in RStudio. My final goal is following: just pressing knit button creates the right PDF output, calling the TeX engines I want to use.

My question may seem similar to this but due to the difference in the final goals, I newly raised the issue.

What I intend to do by customizing the TeX engines

I'm trying to use jecon-unicode.bst to produce my citation sections (I changed the file name as jeconunicode.bst in my working directory due to the error with the hyphen in the file name). Since, in my case, there is no .csl file which satisfies my citation style, I have to specify .bst file in biblio-style line in the YAML of .Rmd as shown below.

test2.Rmd

---
title: "Title"
author: "Name"
date: "`r format(Sys.time(), '%Y年 %B %d日')`"
output:
  bookdown::pdf_document2:
    latex_engine: lualatex
    keep_tex: yes
    toc: TRUE
    toc_depth: 2
    number_sections: TRUE
    fig_caption: TRUE
    dev: cairo_pdf
    extra_dependencies: subfig
    citation_package: natbib
link-citations: yes    
always_allow_html: yes
documentclass: ltjsarticle
header-includes:
   - \RequirePackage{luatexja}
   - \usepackage{graphicx}
   - \usepackage{float}
   - \DeclareGraphicsExtensions{.pdf,.PNG}
   - \usepackage{luatexja-fontspec}
   - \setmainfont{TeX Gyre Termes}
   - \setsansfont{TeX Gyre Heros}
   - \setlength\parskip{0mm}
   - \setlength\parindent{1\Cwd}
   - \usepackage[japanese]{babel}
   - \captionsjapanese
   - \newdimen\bibindent
   - \setlength\bibindent{3\Cwd}
bibliography: myref.bib
biblio-style: jeconunicode
editor_options:
  chunk_output_type: console
---

```{r include=FALSE}
#library(tinytex)
#Sys.setenv(RSTUDIO_PDFLATEX = "latexmk")
#options(tinytex.engine = 'lualatex')
options(tinytex.latexmk.emulation = FALSE)
options(tinytex.verbose = TRUE)

# 何とかの考察に関する歴史

@ST-jp は、「なんとかとは何とかだ!」[@ST-jp]と述べた。
しかし、@ST-en では一転して、
「Something should be more significant than something!!」[@ST-en]と
主張している。

# 参考文献 {-}

myref.bib

Since jecon-unicode.bst adopt different citation styles according to the language, the user has to allow the .bst to recognize which article is written in Japanese. To do so, it is necessary to add language = {ja}.

@article{ST-jp,
title = {われらの発見について:変貌を遂げつつある何とかの将来に関する過去・現在・未来},
journal = {何十華論考},
volume = 42,
number = 5,
pages = {128 -- 182},
year = {2021},
author = {鈴木, 一郎 and 田中, 太郎},
language = {ja}
}

@article{ST-en,
title = {Our first investigation: this is not the first, but this should be the last},
journal = {Journal of Something},
volume = 23,
number = 5,
pages = {19 -- 176},
year = 2032,
author = {Suzuki, Ichiro and Tanaka, Taro}
}

The example of the desirable output

By using jecon-unicode.bst, I can cite the documents published in English in APA style. Simultaneously, I can also cite the documents written in Japanese following the style common with Japanese academic societies. I intend to make the citation section as shown below:

However, in order to use this .bst file, the users have to activate (u)pbibtex to resolve Japanese characters correctly.

What I have done so far

My goal is, by bookdown::pdf_document2 of RStudio insatlled in my Windows 10, knitting a document which finally requires lualatex + pbibtex engines to be processed rightly, as I mentioned before.

So far, I have tried options(tinytex.latexmk.emulation = FALSE) on R console, following the guide to avoid using tinytex's emulation, so that I can directly use latexmk command of TeX from RStudio, rather than tinytex's latexmk'. Moreover, I wrote a .latexmkrc file to specify which TeX engines should be use to compile a .tex files and placed the file in R's home dir, TeX's home dir and my current working dir.

#!/usr/bin/env perl

$latex = 'uplatex %O -kanji=utf8 -no-guess-input-enc -synctex=1 %S';
$pdflatex = 'lualatex %O %S';
$lualatex = 'lualatex %O -synctex=1 %S';
$bibtex = 'upbibtex %O %B';
$makeindex = 'upmendex %O -o %D %S';
$pdf_mode = 4;
$pdf_previewer = '"C:\Program Files\RStudio\bin\sumatra\SumatraPDF.exe" -reuse-instance';

However, pressing knit button (or ctrl + shift + k) and tinytex::latexmk() fail to produce the pdf output because RStudio always calls pdflatex. I also tried tinytex::latexmk(..., engine = "lualatex") but still RStudio seems to call pdflatex. In each way, I got .tex file and other auxiliary files. I also received the following error message.

Latexmk: This is Latexmk, John Collins, 7 August 2018, version: 4.59.
 Error: Failed to compile test2.tex.
 Execution Has Been Interrupted. 

I've tried tinytex::lualatex(), too. However, it had stuck without any error messege, though it produced a .tex file and other auxil files.

Then, is there any way to force RStudio to use TeX engine which the user specifies in .latexmkrc file to create a PDF output?

BTW, my attempts did created test2.tex and executing latexmk test2 on the shell (Windows PowerShell, in my environment) produces the desirable PDF.

Session Infos

> devtools::session_info(('bookdown'))
Session info ----------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.5.1 (2018-07-02)
 system   x86_64, mingw32             
 ui       RStudio (1.1.453)           
 language (EN)                        
 collate  Japanese_Japan.932          
 tz       Asia/Tokyo                  
 date     2018-09-04                  

Packages --------------------------------------------------------------------------------------------
 package   * version date       source        
 backports   1.1.2   2017-12-13 CRAN (R 3.5.0)
 base64enc   0.1-3   2015-07-28 CRAN (R 3.5.0)
 bookdown    0.7     2018-02-18 CRAN (R 3.5.1)
 digest      0.6.16  2018-08-22 CRAN (R 3.5.1)
 evaluate    0.11    2018-07-17 CRAN (R 3.5.1)
 glue        1.3.0   2018-07-17 CRAN (R 3.5.1)
 graphics  * 3.5.1   2018-07-02 local         
 grDevices * 3.5.1   2018-07-02 local         
 highr       0.7     2018-06-09 CRAN (R 3.5.1)
 htmltools   0.3.6   2017-04-28 CRAN (R 3.5.1)
 jsonlite    1.5     2017-06-01 CRAN (R 3.5.1)
 knitr       1.20    2018-02-20 CRAN (R 3.5.1)
 magrittr    1.5     2014-11-22 CRAN (R 3.5.1)
 markdown    0.8     2017-04-20 CRAN (R 3.5.1)
 methods   * 3.5.1   2018-07-02 local         
 mime        0.5     2016-07-07 CRAN (R 3.5.0)
 Rcpp        0.12.18 2018-07-23 CRAN (R 3.5.1)
 rmarkdown   1.10    2018-06-11 CRAN (R 3.5.1)
 rprojroot   1.3-2   2018-01-03 CRAN (R 3.5.1)
 stats     * 3.5.1   2018-07-02 local         
 stringi     1.1.7   2018-03-12 CRAN (R 3.5.0)
 stringr   * 1.3.1   2018-05-10 CRAN (R 3.5.1)
 tinytex     0.8     2018-08-30 CRAN (R 3.5.1)
 tools       3.5.1   2018-07-02 local         
 utils     * 3.5.1   2018-07-02 local         
 xfun        0.3     2018-07-06 CRAN (R 3.5.1)
 yaml        2.2.0   2018-07-25 CRAN (R 3.5.1)

In fact, I haven't installed tinytex itself, though I do have tinytex package in my R, because I've already have a huge TeX system called TeX Live 2018.
Related to Yihui's comment, I think my RStudio directly calls TeX Live 2018 in my computer, because when I comment out citation_package: natbib in test2.Rmd, I do get a PDF file though its citation section is not correct.

@CLRafaelR CLRafaelR reopened this Sep 5, 2018
@yihui
Copy link
Member

yihui commented Oct 8, 2018

With

output:
  bookdown::pdf_document2:
    latex_engine: lualatex

and Ctrl + Shift + K, RStudio should call lualatex instead of pdflatex.

The only problem I can see is that you cannot specify a custom bibtex engine (currently only bibtex and biber are supported). Unfortunately I'm not familiar with other bibtex engines, so you'll have to submit a pull request to implement them by yourself. The relevant code is here:
https://github.com/yihui/tinytex/blob/1b130e127646ebe98428add7bf167e9bc780e8c9/R/latex.R#L199-L233

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants