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

RJournal template can't handle some characters in R chunks #49

Closed
rpruim opened this issue Nov 3, 2015 · 4 comments
Closed

RJournal template can't handle some characters in R chunks #49

rpruim opened this issue Nov 3, 2015 · 4 comments

Comments

@rpruim
Copy link
Contributor

rpruim commented Nov 3, 2015

Most notably, $ and ^ fail because of how knitr, pandoc, and latex interact with the RJournal requirement that code and output be placed in Sinput and Soutput environments.

See jgm/pandoc#2493 for more discussion.

For many uses, this will be a blocking failure that will preclude authors from being able to use this template easily.

@rfhb
Copy link
Contributor

rfhb commented Aug 8, 2016

This tool chain works for me: Adding the code below into the first chunk of an Rmd document, then pressing "Knit" in RStudio results is a fully rendered PDF file. Note the chunk output option escapes the problematic characters ($ becomes $). This enables pandoc to convert to latex. The calls to pandoc and other operating system commands are made from the document output option. Perl is used to remove the escape ("") again. Subsequently, the latex tools are run. Note to adapt the pandoc command to reference the specific template file for the intended journal. Maybe this hack is helpful for others wanting to use the templates. Thanks for knitr, rticles, pandoc!

# workaround https://github.com/rstudio/rticles/issues/49
# see options on http://yihui.name/knitr/hooks/
#
knitr::knit_hooks$set(chunk = function(x, options) {
  #
  #cat(gsub("([$^%])", "\\\\\\1", "test%this$and^that"))
  gsub("([$^%])", "\\\\\\1", x)
  #
})
#
knitr::knit_hooks$set(document = function(x) {
  #
  fid  <- paste0(format(Sys.time(), "%Y-%m-%d_%H-%M-%S_file"))
  tmpf <- paste0(fid, ".md")
  texf <- paste0(fid, ".tex")
  auxf <- paste0(fid, ".aux")
  #
  cat(x, file = tmpf)
  #
  system(paste("pandoc +RTS -K512m -RTS", tmpf, "--to latex", "--from", 
               "markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash",
               "--output", texf, "--highlight-style tango", 
               "--latex-engine /Library/TeX/texbin/pdflatex", "--template",
               paste0(system.file(package = "rticles"), 
               "/rmarkdown/templates/jss_article/resources/template.tex")))
  #
  system(paste("perl -p -i -e 's/\\\\([\\$\\%\\^])/$1/g'", texf))
  #
  system(paste("pdflatex", texf))
  system(paste("bibtex",   auxf))
  system(paste("pdflatex", texf))
  system(paste("pdflatex", texf))
  #
  file.remove(c(tmpf, texf, auxf, paste0(fid, c(".log", ".out", ".bbl", ".blg"))))
  # 
  x
  #
})

@yihui
Copy link
Member

yihui commented Jul 3, 2018

Fixed. Please try devtools::install_github('rstudio/rticles'). Note that it will require Pandoc 2.2.1. You can use the Preview version of RStudio if you do not want to install Pandoc separately: https://www.rstudio.com/products/rstudio/download/preview/

yihui added a commit that referenced this issue Jul 3, 2018
…raw latex block ```{=latex} for R Journal articles, which is the same fix as 8da3d29
yihui added a commit that referenced this issue Jul 3, 2018
…raw latex block ```{=latex} for R Journal articles, which is the same fix as 8da3d29
@rfhb
Copy link
Contributor

rfhb commented Jul 3, 2018

Very cool, many thanks for fixing and closing!

@github-actions
Copy link

github-actions bot commented Nov 2, 2020

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants