1

I'd like to customize the citation style when I knit an .Rmd file with bookdown::pdf_document2 option, specifing biblio-style in its YAML section. I saw that using .csl file rather than .bst has solved some former questions about formatting citation sections in bookdown (like this or this). However, in my case, I have to specify which .bst file to use in biblio-style line in the YAML of .Rmd because there is no .csl file which satisfies my citation style...

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). By this file, I can cite the documents published in English in APA style and, simultaneously, I can also cite the documents written in Japanese following the style which some academic societies in Japan recommend. I intend to make the citation sections as shown below:

Desirable Result

In-text Citation

Suzuki and Tanaka (2032)

鈴木・田中 (2021)

As you can see, "・" separates the multiple author names in in-text citations when a document written in Japanese language.

Reference Section

Suzuki, Ichiro, and Taro Tanaka (2032) “Our First Investigation: This Is Not the First, but This Should Be the Last.” Journal of Something Vol.23, No. 5, pp. 19–176.

鈴木一郎・田中太郎 (2021) 「われらの発見について:変貌を遂げつつある何とかの将来に関する過去・現在・未来」, 『何十華論考』, 第42巻, 第5号, 128–82頁.

In the reference section, jeconunicode.bst will also treat Japanese and English document differently, without creating different reference sections per language (i.e. the reference chapter I want to create is different from this). For instance, in the same bibliography section, 「」『』 are uesd for Japanese document names, while " " and italicization are used to modify English document names. 

In order to activate jeconunicode.bst, I wrote Test.Rmd, inserting myref.bib in it.

Test.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}
   # You can unset Japanese font specification
   # - \usepackage{luatexja-fontspec}
   # - \setmainjfont{YuMincho}[UprightFont=YuMincho-Regular,BoldFont=YuMincho-Demibold]
   # - \setmainfont{TeX Gyre Termes}
   # - \setsansjfont{YuGothic-Medium}[BoldFont=YuGothic-Bold]
   # - \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: jconunicode.bst
editor_options:
  chunk_output_type: console
---

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

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



# 参考文献 {-}

\leftskip=3\Cwd

\setlength\parindent{-3\Cwd}

<div id="refs"></div>

\leftskip=0pt

\setlength\parindent{1\Cwd}

myref.bib

To make jeconunicode.bst recognize which article is written in Japanese, 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}
}

However, I got only the undesirable result for the present. jeconunicode.bst specified in the biblio-style option doesn't have any effect on the output and the default citation style is used, although I put .Rmd, .bst, and .bib files in the same directory.

Current Unwanted Result

In-text Citation

Suzuki and Tanaka (2032)

鈴木 and 田中 (2021)

Reference Section

Suzuki, Ichiro, and Taro Tanaka. 2032. “Our First Investigation: This Is Not the First, but This Should Be the Last.” Journal of Something 23 (5): 19–176.

鈴木, 一郎, and 田中太郎 . 2021. “われらの発見について:変貌を遂げつつある何とかの将来に関する過去・現在・未来.” 何十華論考 42 (5): 128–82.

Is there any nice way to cite Japanese and English (or any languages with Latin alphabet) in harmony as I pointed out above?

My Session Info

I use the bookdown in RStudio as shown below, with TeXLive 2018, under Windows 10:

>devtools::session_info(('bookdown'))
Session info ---------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.3 (2017-11-30)
 system   x86_64, mingw32             
 ui       RStudio (1.1.453)           
 language (EN)                        
 collate  Japanese_Japan.932          
 tz       Asia/Tokyo                  
 date     2018-08-30                  

Packages -------------------------------------------------------------------------------------------
 package   * version date       source        
 backports   1.1.2   2017-12-13 CRAN (R 3.4.3)
 base64enc   0.1-3   2015-07-28 CRAN (R 3.4.1)
 bookdown    0.7     2018-02-18 CRAN (R 3.4.4)
 digest      0.6.15  2018-01-28 CRAN (R 3.4.3)
 evaluate    0.10.1  2017-06-24 CRAN (R 3.4.3)
 graphics  * 3.4.3   2017-12-06 local         
 grDevices * 3.4.3   2017-12-06 local         
 highr       0.6     2016-05-09 CRAN (R 3.4.3)
 htmltools   0.3.6   2017-04-28 CRAN (R 3.4.3)
 jsonlite    1.5     2017-06-01 CRAN (R 3.4.3)
 knitr       1.20    2018-02-20 CRAN (R 3.4.4)
 magrittr    1.5     2014-11-22 CRAN (R 3.4.3)
 markdown    0.8     2017-04-20 CRAN (R 3.4.3)
 methods   * 3.4.3   2017-12-06 local         
 mime        0.5     2016-07-07 CRAN (R 3.4.1)
 Rcpp        0.12.18 2018-07-23 CRAN (R 3.4.4)
 rmarkdown   1.10    2018-06-11 CRAN (R 3.4.4)
 rprojroot   1.3-2   2018-01-03 CRAN (R 3.4.4)
 stats     * 3.4.3   2017-12-06 local         
 stringi     1.1.6   2017-11-17 CRAN (R 3.4.2)
 stringr     1.2.0   2017-02-18 CRAN (R 3.4.3)
 tinytex     0.6     2018-07-07 CRAN (R 3.4.4)
 tools       3.4.3   2017-12-06 local         
 utils     * 3.4.3   2017-12-06 local         
 xfun        0.3     2018-07-06 CRAN (R 3.4.4)
 yaml        2.1.16  2017-12-12 CRAN (R 3.4.3)
zx8754
  • 52,746
  • 12
  • 114
  • 209
Carlos Luis Rivera
  • 3,108
  • 18
  • 45
  • That YAML header should read `biblio-style: jeconunicode`, i.e. without `.bst` and with an `e`. Unfortunately your document does not compile on my system, so I cannot say if that is a solution (missing fonts). – Ralf Stubner Aug 30 '18 at 19:55
  • @RalfStubner Thank you for your fast reply. I edited the original post: I fixed the typo you pointed out and deleted the Japanese font specification section. I hope now you can try to compile the document without Japanese fonts (i.e. `YuMincho` and `YuGothic-Medium`). – Carlos Luis Rivera Aug 31 '18 at 01:05
  • @RalfStubner When I set `natbib` in `citation_package`, the file fails to compile. However, when I add `- \usepackage{natbib}` right after `\usepackage[japanese]{babel}` to configure the package loading order (see [this](https://tex.stackexchange.com/questions/283232/problem-with-natbib-and-french-babel-after-migrating-to-el-capitan#comment683013_283232)), instead of touching `citation_package`, I do get an `pdf` output but the result is not desirable... – Carlos Luis Rivera Aug 31 '18 at 02:08

1 Answers1

1

I was not able to reproduce your issue with the following test file:

---
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
---

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

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


# 参考文献 {-}

\leftskip=3\Cwd

\setlength\parindent{-3\Cwd}

<div id="refs"></div>

\leftskip=0pt

\setlength\parindent{1\Cwd}

Instead of producing the undesired output, my HD was filled with several GB of error messages from bibtex. Among many others there was:

is.kanji.str$ is an unknown function---line 1643 of file jeconunicode.bst
is.kanji.str$ is an unknown function---line 1644 of file jeconunicode.bst
is.kanji.str$ is an unknown function---line 1645 of file jeconunicode.bst
is.kanji.str$ is an unknown function---line 1646 of file jeconunicode.bst

I figured that I should be using the pbibtex instead of bibtex. Indeed, processing the resulting tex and aux files manually with pbibtex and lualatex worked fine. Since I found no way to configure that, I just created a symbolic link:

ln -s /usr/bin/pbibtex ~/bin/bibtex

This way a call to bibtex would actually call pbibtex. After that the document compiled, even though it took several calls to "Knit" within RStudio to get the references resolved:

enter image description here

To me this looks like the desired output. I cannot explain why you are seeing different results.

> devtools::session_info(('bookdown'))
Session info -----------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.5.1 (2018-07-02)
 system   x86_64, linux-gnu           
 ui       RStudio (1.2.830)           
 language (EN)                        
 collate  de_DE.UTF-8                 
 tz       Europe/Berlin               
 date     2018-08-31                  

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.0)                 
 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-08-24 Github (tidyverse/glue@4e74901)
 graphics  * 3.5.1   2018-07-03 local                          
 grDevices * 3.5.1   2018-07-03 local                          
 highr       0.7     2018-06-09 CRAN (R 3.5.0)                 
 htmltools   0.3.6   2017-04-28 CRAN (R 3.5.0)                 
 jsonlite    1.5     2017-06-01 CRAN (R 3.5.0)                 
 knitr       1.20    2018-02-20 CRAN (R 3.5.0)                 
 magrittr    1.5     2014-11-22 CRAN (R 3.5.0)                 
 markdown    0.8     2017-04-20 CRAN (R 3.5.0)                 
 methods   * 3.5.1   2018-07-03 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.0)                 
 rprojroot   1.3-2   2018-01-03 CRAN (R 3.5.0)                 
 stats     * 3.5.1   2018-07-03 local                          
 stringi     1.2.4   2018-07-20 CRAN (R 3.5.1)                 
 stringr     1.3.1   2018-05-10 CRAN (R 3.5.0)                 
 tinytex     0.8     2018-08-30 CRAN (R 3.5.1)                 
 tools       3.5.1   2018-07-03 local                          
 utils     * 3.5.1   2018-07-03 local                          
 xfun        0.3     2018-07-06 CRAN (R 3.5.1)                 
 yaml        2.2.0   2018-07-25 CRAN (R 3.5.1) 
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • You got the right result. I got your idea that you made a symbolic link from `pbibtex` on your user's home directory so that `knitr` uses `pbibtex`. However, your solution doesn't work straightforwardly in my environment, because my OS is Windows 10 (and that's why we got the different results, in my view). I tried to create a soft link from `C:\texlive\2018\bin\win32\pbibtex.exe` but I'm not sure where to locate such a symbolic link, because in my home directory, there is no `bin`. If Windows is available, could you show me the way to use `pbibtex` when knitting a document? – Carlos Luis Rivera Aug 31 '18 at 23:38
  • @CarlosLuisRivera The link has to be in a directory that is in your `PATH` and before the other version. If that does not work you can try it to reprocess the produced `tex` file with `lualatex` and `pbibtex`. – Ralf Stubner Sep 01 '18 at 05:56
  • 1
    Your second solution works nicely. After I press `ctrl` + `shift` + `k` to knit the document, the execution doesn't complete (i.e. R Markdown tab in RStudio doesn't return `Output created: test.pdf`) however long it takes. Nontheless, I find `test.tex` and related files (e.g. `test.log`, `test.bbl`, and so on) exist in my directory. So, I reprocess `test.tex` with `upBibTeX` once and with `LuaLaTex` twice. I used `upBibTeX` 'cause [The official document of jeconunicode](https://github.com/ShiroTakeda/jecon-bst/blob/master/unicode/jecon-unicode-xelatex.pdf) recommends us using it. – Carlos Luis Rivera Sep 01 '18 at 09:51
  • @CarlosLuisRivera Great. I have added the manual processing to the answer. Feel free to accept/up-vote it as you see fit. – Ralf Stubner Sep 01 '18 at 10:09
  • Although your 2nd solution satisfies me a lot, still I have to do some TeXing. Speaking of the 1st one, it seems that the `PATH` from `C:\texlive\2018\bin\win32` has already set before I encountered the problem. Therefore, RStudio seems to have an access to `(u)pbibtex` already, no matter RStudio really does access it. To complete producing a document solely with RStudio, what do you think I should do? I found [this](https://tex.stackexchange.com/a/73760/169454) and I think `Sys.setenv(RSTUDIO_PDFLATEX = "latexmk")` has to do with solving this issue, though my trial doesn't go well so far... – Carlos Luis Rivera Sep 01 '18 at 10:21
  • @CarlosLuisRivera Does `latexmk` work when called directly? Then you can try setting `RSTUDIO_PDFLATEX`. I am not sure if that still works. You might have to configure `tinytex` instead. – Ralf Stubner Sep 01 '18 at 10:45
  • Yes, `latexmk` itself works when I call on the shell (in my environment, `Windows PowerShell`), though I needed to configure `.latexmkrc` in my home dir. I set the file as shown below: `#!/usr/bin/env perl $latex = 'uplatex %O -kanji=utf8 -no-guess-input-enc -synctex=1 %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';`. Yet, `Sys.setenv(RSTUDIO_PDFLATEX = "latexmk")` written in `.Rprofile` still has no effect. – Carlos Luis Rivera Sep 02 '18 at 02:17
  • @RulfStubner I do have `tinytex` package but I haven't tried `install_tinytex()` because I've already have the broader TeX system called `TeX Live 2018`. Although `latexmk` command (i.e. `latexmk test2`) on the shell goes well with the setting specified in `.latexmkrc` above, executing `latexmk("test2.tex")` on the console of RStudio fails to compile the document. – Carlos Luis Rivera Sep 02 '18 at 04:29
  • @RulfStubner Following [this guideline to use `TeX Live` rather than `TinyTeX`](https://github.com/yihui/tinytex/issues/48#issuecomment-407015282), I executed `options(tinytex.latexmk.emulation = FALSE)` but in vain. – Carlos Luis Rivera Sep 02 '18 at 04:58
  • @CarlosLuisRivera This sounds as if `tinytex::latexmk` is ignoring your configuration file. To test this, you can make the file syntactically incorrect so that `latexmk` raises an error when called in the shell. Does this error also happen with `tinytex::latexmk` (without emulation)? – Ralf Stubner Sep 02 '18 at 10:25
  • @RulfStubner Sorry, which file shoud I intentionally add bad syntax to? To `.tex` file? – Carlos Luis Rivera Sep 02 '18 at 12:47
  • @CarlosLuisRivera The configuration file `.latexmkrc`. – Ralf Stubner Sep 02 '18 at 13:32
  • @RulfStubner I'm sure `tinytex::latexmk` ignores `.latexmkrc` in the home dir. I began a new R session and executed `options(tinytex.latexmk.emulation = FALSE)` only on the console, commenting `Sys.setenv(RSTUDIO_PDFLATEX = "latexmk")` and `options(tinytex.engine = 'lualatex')` out. Then `Latexmk: applying rule 'pdflatex'...` came up and the document compilation failed. If `tinytex::latexmk` accessed `.latexmkrc`, `test2.tex` should've been processed by `LuaLaTeX` without `options(tinytex.engine = 'lualatex')`. – Carlos Luis Rivera Sep 02 '18 at 13:37
  • @RulfStubner Adding some multibite characters into `.latexmkrc` was "incorrectly" ignored when I typed `tinytex::latexmk("test2.tex")` on the R console, i.e. `pdflatex` was called as if there were no error in `.latexmkrc`. However, the bad perl syntax "rightly" caused the malfunction when I used the shell: `Latexmk: Initialization file 'C:/Users/My name/.latexmkrc' gave an error`. After I deleted the characters, the shell command `latexmk test2` produced the output. However,`tinytex::latexmk("test2.tex")` on the R console behaved same even after the deletion of the characters. – Carlos Luis Rivera Sep 02 '18 at 14:04
  • @CarlosLuisRivera It worked for me when I used your `.latexmkrc`. Maybe `latexmk` does not find the file when called from R. What's the value of the environment variables `HOME` and `USERPROFILE` in R and in the shell. You could also copy the configuration file to the working directory. – Ralf Stubner Sep 03 '18 at 09:11
  • As `USERPROFILE` which both R and PowerShell (PS, henceforth) showed is identical, I only configured `HOME`. On Windows in general, the default `HOME` for R is `C:/Users/MY NAME/Documents`, so I changed it as `C:/Users/MY NAME` so that R's `HOME` can be the same as PS's one. However, `tinytex::latexmk("test2.tex")` still calls `pdflatex`. Even `tinytex::latexmk("test2.tex", engine = "lualatex", emulation = FALSE)` doesn't work for creating PDF output. I'm sure that I've executed `options(tinytex.latexmk.emulation = FALSE)`. Moreover, `.latexmkrc` in my working dir has no effect. – Carlos Luis Rivera Sep 04 '18 at 02:49
  • @CarlosLuisRivera I am out of ideas. I think it is best if you open an issue in the `tinytex` repository on GitHub referencing this question for further context. – Ralf Stubner Sep 04 '18 at 05:02
  • I entirely appreciate your supports. I've learned many ways to configure R and TeX through the discussion with you. I will tell you if I find further information and ideas to solve this issue. – Carlos Luis Rivera Sep 04 '18 at 05:13