How to use a LaTeX package with R/exams? - r

I am using exams2moodle() to create exam quizzes. I would like to use some math symbols which require the LaTeX package amssymb. How should I proceed?

TL;DR: amssymb LaTeX symbols are supported by MathJax which is the default renderer in a standard Moodle installation. So these symbols should work out of the box when using exams2moodle(..., converter = "pandoc-mathjax") which is the default sinc R/exams 2.3.3 (released in July 2019).
Details: As already pointed out by the answer of #Hack-R, there are various ways of including mathematical notation written in LaTeX in HTML-based exercises. An overview is given in this blog post: http://www.R-exams.org/tutorials/math/
It is important to note that in this case the LaTeX code is not actually processed by LaTeX itself. It can either be converted to MathML (using pandoc or ttm) or rendered in the browser by the MathJax JavaScript library. In any case it depends on the converter/renderer which LaTeX commands can be successfully displayed. All of them support basic LaTeX commands plus some extra commands from certain packages. Which additional commands from which packages are supported depends on the converter/renderer. In any case, the capabilities can not be extended by style files!
In a standard Moodle installation MathJax is enabled which suports the AMS LaTeX package (because MathJax was developed by/for the AMS!). So the best strategy is to simply keep the LaTeX in the HTML for Moodle. This can be done with exams2moodle(..., converter = "pandoc-mathjax") which is actually the default converter since R/exams 2.3.3 released in July 2019. In previous versions of R/exams, the default was converter = "ttm" which did not support all the amssymb symbols.

In general, you input your symbols into either .Rmd or .Rnw and you can have it rendered with any available engine (e.g. pandoc, mathjax, knitr, etc).
install.packages("exams")
require(exams)
xWeave is called on each exercise file and creates LaTeX code
elearn_exam <- c("swisscapital.Rmd", "deriv.Rmd", "ttest.Rmd",
"boxplots.Rmd", "function.Rmd", "lm.Rmd", "fourfold2.Rmd")
set.seed(2020-04-16)
exams2moodle() produces an XML file that may be uploaded into Moodle
It goes from LaTeX to HTML then HTML to XML
exams2moodle(elearn_exam, n = 3, name = "R-exams")
To add custom LaTeX packages you can modify the preamble as per the answers in this post, i.e. \usepackage
As described in the linked post, that can either be done direct, i.e.
---
title: "Title"
author: "Me"
header-includes:
- \usepackage{mypackage}
output:
pdf_document
---
or via a mystyles.sty file in the same directory.

Related

Adding a new bibliography style with R Markdown and TinyTex

I'm writing a paper using R Markdown and TinyTex, using Biblatex for referencing. It works fine with default referencing styles, but I need to add a custom bibliography and citation style for the journal I'm writing for.
I need to follow the Unified Stylesheet for Linguistics, for which there is a Biblatex implementation available on Github here, containing a .bbx and .cbx file.
I've tried adding those .bbx and .cbx files to my local copy of TinyTex, inside Library/TinyTex/texmf-local/tex/latex/biblatex. My YAML header includes:
output:
pdf_document:
citation_package: biblatex
biblatexoptions: [bibstyle=biblatex-sp-unified, citestyle=sp-authoryear-comp]
When I knit the document, I get the following error:
tlmgr search --file --global '/biblatex-dm.cfg'
! Package keyval Error: bibstyle undefined.
I don't have a biblatex-dm.cfg file (nor do I really understand what that would be). I would have thought the .bbx and .cbx files would be sufficient, based on the regular installation instructions in the style's Github repo.
Where should I put .bbx and .cbx files, so that tlmgr can find them? And/or what additional steps do I need to take to use this style with my paper?
====================================================================
UPDATE: The problem seems to be coming from the Pandoc LaTeX template that R Markdown uses.
Setting aside R Markdown, I created a smaller minimal LaTeX example:
main.tex
references.bib
Where main.tex is:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[bibstyle=biblatex-sp-unified,citestyle=sp-authoryear-comp]{biblatex}
\addbibresource{references.bib}
\begin{document}
Something something \citep{darwin_origin_1859}.
\printbibliography
\end{document}
And references.bib is:
#book{darwin_origin_1859,
location = {London},
title = {On the Origin of Species by Means of Natural Selection},
publisher = {J. Murray},
author = {Darwin, Charles},
date = {1859}
}
I had success compiling this example using the sequence of commands pdflatex, biber, pdflatex, pdflatex. Thus it seems my local TeX installation knows about the biblatex-sp-unified.bbx and sp-authoryear-comp.cbx files I added and can use them just fine.
Subsequently, I created an equivalent minimal R Markdown document with the YAML header:
title: "Untitled"
output:
pdf_document:
citation_package: biblatex
bibliography: references.bib
biblatexoptions: [bibstyle=biblatex-sp-unified, citestyle=sp-authoryear-comp]
and body:
Something something [#darwin_origin_1859].
This time, I got the same old error message from before:
tlmgr search --file --global '/biblatex-dm.cfg'
! Package keyval Error: bibstyle undefined.
This would seem to suggest that the problem is caused by something in Pandoc's LaTeX template, but I don't know what.
Just to confirm that it's definitely the Pandoc template and not my own installation/setup, I took the .tex file that gets produced when I knit the minimal R Markdown example above, and tried to compile it in Overleaf (with biblatex-sp-unified.bbx and sp-authoryear-comp.cbx files added). I reproduced the same error.
Although I think I've localised the problem, I'd still very much like to understand what and where the problem is in the Pandoc template. I'd also be keen to hear if anyone has any fixes (other than just using a different template or writing my own).
UPDATE: This seems to be an issue with using an out-of-date version of R Markdown and/or Pandoc.
I was using rmarkdown package v.1. At time of writing, the most up-to-date version is 2.1.
I updated all my packages and updated Rstudio (which currently ships with Pandoc v2.3.1) and no longer experience problems. I also upgraded R (from 3.5.something to 3.6.2) and did a fresh re-install of tinytex while I was at it, but I'm not sure whether those things had an effect for this particular problem.
Now, when I put biblatexoptions: [bibstyle=biblatex-sp-unified, citestyle=sp-authoryear-comp] in my YAML header, it's correctly converted into the LaTeX command \usepackage[bibstyle=biblatex-sp-unified,citestyle=sp-authoryear-comp]{biblatex}, rather than the \ExecuteBibliographyOptions command as described below.
Ralf Stubner initially suggested I check my R Markdown/Pandoc versions in the comments. Please give his comments an upvote if you them useful as well.
Problem recap:
I'm writing a document in R Markdown and I have a particular referencing style that I'd like to use with biblatex. I have a .bbx and .cbx file defining the style, available on Github (linked above). The problem is that the document fails to compile, saying biblio/citation styles are undefined (even when the style files are in the project folder itself).
I've found that the problem was caused by the way I was passing options to biblatex. In my YAML Header, the line:
biblatexoptions: [bibstyle=biblatex-sp-unified, citestyle=sp-authoryear-comp]
gets converted to the latex command:
\ExecuteBibliographyOptions{bibstyle=biblatex-sp-unified,citestyle=sp-authoryear-comp}
I'm not sure why, but when this command is included, it produces the errors I was observing.
Installing new Biblatex style:
I'm finding that TeX doesn't know about the .bbx and .cbx files when they're in my ~/Library/TinyTex/texmf-local/tex/latex/biblatex directory (which is where I expected to put them based on the Github installation instructions).
To get the referencing style recognised by the system, I placed .bbx and .cbx files inside ~/Library/TinyTex/texmf-dist/tex/latex/biblatex/bbx and ~/Library/TinyTex/texmf-dist/tex/latex/biblatex/cbx respectively. Then, in the terminal, I ran sudo mktexlsr.
(Alternatively, for use only with a particular document, the .bbx and .cbx files could simply be kept in the project directory with the R Markdown file)
Original hacky answer (but see update above):
Instead of using biblatexoptions in the YAML header of the R Markdown document, I simply knitted it with citation_package: biblatex (and no extra options). I also added keep_tex: yes. Then, I opened the resulting tex file, found the \usepackage{bibtex} command and added the desired options, so it read \usepackage[bibstyle=biblatex-sp-unified,citestyle=sp-authoryear-comp]{biblatex}.
Finally, I ran pdflatex and biber on the tex file in the terminal. Clearly far from ideal, but it will technically produce the desired output.

apply multiple columns division in the same R markdown page

I want to divide my Rmarkdown document like in picture. One column for the first section then two columns in the other sections.
used \onecolumn and \twocolumn but each section is created in a single page. Is there a solution to fix it?
This article has been published in a journal of the AMS. Fortunately, the rticle package supports the AMS journals. I use the standard template as generated by RStudio, change layout in the YAML header and use fancyhdr to redefine the pagestyle:
layout: twocol
header-includes:
- \usepackage{fancyhdr}
- '`\fancypagestyle{plain}{\fancyhf{}\fancyfoot[C]{\thepage}\renewcommand{\headrulewidth}{0pt}}`{=latex}'
I then get the following result:
As an alternative, you could try to adapt the revtex LaTeX class as used by the APS.
Note: I am using a quite recent version of pandoc, which makes the quoting of LaTeX complicated. With older versions of pandoc you might get away without the quoting.

Rmd to PDF compiling error: Package geometry \paperwidth (0.0pt) too short

I am writing a paper in R markdown and need to format it with this .cls file supplied by an academic journal.
A minimal .tex file compiles perfectly well with the above cls file.
My .tex file (compiled on ShareLaTeX with clv3.cls saved in same directory):
\documentclass[shortpaper]{clv3}
\usepackage[utf8]{inputenc}
\title{Paper title}
\author{Name Surname}
\date{May 2018}
\begin{document}
\maketitle
\section{Introduction}
Some text.
\end{document}
However a comparable minimal document in R markdown, using the same cls file, fails to compile in Rstudio, with the following error: ! Package geometry Error: \paperwidth (0.0pt) too short.
My Rmd file (with clv3.cls file saved in same directory):
---
title: "Paper title"
author: "Name Surname"
documentclass: clv3
classoption: shortpaper
output: pdf_document
---
# Introduction
Some text.
Why is this error induced when I try to use this class file with an R markdown document and how may I fix it?
I've tried manually specifying a pagewidth setting in the YAML header, but I don't really know what I'm doing. This seems undesirable anyway, since the normal LaTeX document works fine without it (and surely page width is something that should be specified by a journal, not manually overwritten by an author).
I do not know where exactly the clv3.cls class and the default pandoc template clash. However, that template does so many things that do not make sense when writing with a specific style, that is best to use your own template. Using clv3-template.tex
\documentclass[shortpaper]{clv3}
\usepackage[utf8]{inputenc}
$if(title)$
\title{$title$}
$else$
\title{}
$endif$
$if(author)$
\author{$for(author)$$author$$sep$ \\ $endfor$}
$else$
\author{}
$endif$
\begin{document}
$if(title)$
\maketitle
$endif$
$body$
\end{document}
together with
---
title: "Paper title"
author: "Name Surname"
output:
pdf_document:
template:
clv3-template.tex
---
# Introduction
Some text.
should be a good starting point.
The accepted answer works perfectly for the minimal example presented. However it breaks again pretty quickly as the document is made more complex (for example, inserting a bibliography and in-text citations). I'd like to expand a little on my solution for the potential benefit of any future readers, as I found it a bit of steep learning curve:
The issue here is that Pandoc has a LaTeX template, which it uses to produce PDF documents. This is separate from a .cls class file, which defines a document class. Like Ralf Stubner says, something about my particular class file was not cooperating with Pandoc's default template. This is probably super basic and obvious to many, but I had not appreciated this extra step nor understood the distinction between these files.
If one does not wish to deal with raw LaTeX, it seems there are quite a few templates out there for various kinds of documents (see, for example, the rticle package). Using R Markdown to produce a PDF document in a particular custom format (such as for a particular journal) will require construction of a LaTeX template, however. This can be done by either of two ways:
Tinkering with an existing template until you get what you need, either by finding Pandoc's default template and starting from there (see comment by user2554330 for location) or by cloning someone else's template on Github etc.
Writing a template from scratch. In this case, Ralf Stubner's minimal example above plus this section of the Pandoc manual will be informative.
In my case, I went with the latter option. I have saved my eventual template as an R package which can be installed using devtools::install_github("JaydenM-C/CLtemplate"). So if anyone else would ever like to author a document for Computational Linguistics using this particular document style, this may save you some time.

How to add a new language for highlight in bookdown

I'm writing a document in rmarkdown with the thesisdown template.
Related to the issue thesisdown-41: how can I add a new language for highlighting which is currently not supported?
The project mentioned in the link is derived from bookdown
Under the hood bookdown uses pandoc for tranforming markdown to HTML/PDF/.... From pandoc's manual at http://pandoc.org/MANUAL.html#syntax-highlighting we get:
The library used for highlighting is skylighting.
The list of available languages can be retrieved with pandoc --list-highlight-languages
Slightly off topic, but I just worked out how to do this in RMarkdown rather than Bookdown. I suspect you'll need this and maybe a little more.
Passing extra arguments to Pandoc via the YAML front-matter:
output:
html_document:
highlight: haddock
pandoc_args: ["--syntax-definition", "cobol.xml"]
Obtain the XML syntax definition file from somewhere (or create it). I got my COBOL one from:
wget http://kde.6490.n7.nabble.com/attachment/1163657/0/cobol.xml.gz
The syntax of the highliting file is as used by the Kate project in KDE.
Obtain the pre-req language.dtd file, this is some deep dependency with pandoc.
wget https://raw.githubusercontent.com/jgm/highlighting-kate/master/xml/language.dtd
If've just added the two files to my git repo, plus the YAML lines to my RMarkdown, and everything then worked on other developers machines.

Different versions of the rMarkdown package compile a .Rmd differently

Edit
Thanks to some SO (i.e., #tmpname12345) users, I've come to the conclusion that my problem (described below) is likely caused by the differences between v0.2.64 and v0.3.11 of rmarkdown. The question now becomes:
Is there a way I can prevent rmarkdown version 0.3.11 and knitr from adding a line after the title of a .pdf file?
Original Question
I've come across a curious difference between compiling a .pdf document with the same yaml header block on two different computers with Rstudio and knitr. The difference is that on one computer, an additional line is inserted after the title while on the other, the line is not inserted (the preferred behavior). I would like to prevent the line from being inserted regardless of where the document is compiled.
I am using 32-bit R 3.1.2 on both computers. The yaml block is identical between both computers.
Here is the .Rmd file:
---
title: Header Test
output: pdf_document
---
My#gmail.com\hfill \hfill Address
Phone number\hfill \hfill Town, State, ZIP
Evidence of no additional line after the title, generated from my laptop with rmarkdown version 0.2.64:
Evidence of the additional line after the title, generated from my desktop with rmarkdown version 0.3.11:
Initially, I thought it may have been a function of the knitr package version, so I upgraded the package in the second image from 1.7 to the newest available version on CRAN, 1.9, but the extra line still remains.
I have also considered that this is an artifact of the Rstudio version on my laptop but if it is, I don't want to upgrade as the first image is the preferred.
I routinely edit .Rmd files in my Google Drive folder from both a desktop computer and my laptop, accessing the same file. Unfortunately, they compile differently. I prefer that there is no line added after the title. At this point, I don't want to change anything on my laptop, fearing that compilation will start to include the extra line.
Is there some way I can force knitr to not add the additional line?
One way is to create your own latex template that formats the header like you want. You can use the default template as a starting point here. Then save the file as .tex in the same directory as your Rmd file and add this to the yaml header:
output:
pdf_document:
template: mytemplate.tex

Resources