How to pass options to a LaTeX font in R Markdown? - r

rmarkdown, following {xe|lua}latex, allows to specify fonts for main text, sans-serif text, monspaced text (most notably code chunks !) and math fonts in the YAML header. At least for PDF rendering via xetex, this works.
However, I found no (documented) way to pass options to the underlying setxxxfont \LaTeX command. For example, the YAML fragment :
```
monofont: Inconsolata
```
generates the following \LaTeX fragment :
\setmonofont[Mapping=tex-ansi]{Inconsolata}
I have two questions with this:
why is the Mapping=tex-ansi added ? And how to control it ? (I'm working in UTF8...).
How could I set additional arguments for the font options i.e. \setmonofont[Scale=0.91]{TeX Gyre Cursor}?
The R Markdown book and the Pandoc's User's Guide did not reveal anything pertinent.

Background:
When R Markdown converts the knitted code to the output format (PDF), a pandoc template is used. The template is stored within the package, and any variables which get replaced by the YAML variables are contained in the $$ Notation
1. Encoding
The Mapping=tex-ansi is added to the code as a workaround an issue as reported on GitHub. Therefore I would be cautious of deleting this for potential side effects.
If you do indeed wish to change this code, you will have to make a copy of the LaTeX template file used to convert the document. You can find the default template here. See here for some more information on providing custom templates.
2. Additional Font Options
You can use the monofontoptions YAML argument to add additional arguments to the font options.
Documentation of the variables which can be parsed by the LaTeX output are available in the pandoc documentation

Related

Cross-references and figures doesnt render when knit a pdf, since Rstudio install babel-stuff (after changing language to "fr-FR")

I really need help this time:
Rstudio auto-install some babel-stuff after I've indicate the language of an academic paper in YAML (lang: "fr-FR"). After that point, when I'm knit to pdf, tables and figures doesn't render anymore in the pdf: computation printed in the text are correct, but the pdf produced is now without figure, without table, and cross-references are not working anymore (e.g., the pdf now contain some "Figure #ref(tab:repartition-transport)" where before the language-change there is a number printed like "Figure 1").
I try to set language back to "en-EN" but cross-references, tables and figures doesn't render in the pdf.
The error message said that, when knitting is over:
Package babel Warning: No hyphenation patterns were preloaded for the language 'French' into the format.
Avis : (babel) Please, configure your TeX system to add them and rebuild the format.
Now I will use the patterns preloaded for \language=nohyphenation instead on input line 87.
I don't understand.
Since I don't know how to remove babel (which is not in the packages list), I try to run tinytex::check_installed("babel"), which answer: TRUE. Any help is very appreciated, since I don't know what is the problem.
Thanks

Change formatting behavior for a URL in nbconvert

I want to convert a Jupyter notebook to LaTeX using nbconvert. The default exporting behavior is to convert Jupyter hypertext links
[a link](http://some.website.com)
to a LaTeX string that can be rendered as a link in a PDF document:
\href{http://some.website.com}{a link}
I would like to change this behavior, so that links are rendered instead as footnotes:
a link\footnote{http://some.website.com}
What do I have to modify to do this? I've looked at the documentation for nbconvert but haven't been able to figure it out. Is it possible to do this within a .tplx template file? I've looked at the standard template files and don't see anything defining the URL behavior, so I'm guessing it's handled by pandoc somehow, but I'm confused about where I need to change something.
You can use LaTeX to redefine the \href command. Put the following in your template or using header-includes:
\let\oldhref=\href
\renewcommand{\href}[2]{\footnote{\oldhref{#1}{#2}}}
Alternatively, you could write a pandoc filter to rewrite the actual output to a RawInline "latex" "\footnote"...

Changing keywords to highlight in an RMarkdown document

I have been writing a document in bookdown where within the *.Rmd file I call a figure by using the following syntax
\#ref(fig:MyFigureName)
This differs slightly from the notation that you would use in a normal RMarkdown file exporting to latex which would be
\ref{fig:MyFigureName}
The issue I am running into is that when I write something in bookdown the function calling the Figure is not being highlighted properly (see image below).
I have imported my own rsTheme (which is from my understanding, basically a .css file) but I don't see an option to add keywords to highlight.
But I would like for the entire function to be colored differently from the inline text (Photoshop version of desired output shown below)
Does anyone know how I would edit my *rsTheme file in order to accomplish this?
Thanks!

Extract html dependencies for .Rmd file (containing htmlwidgets)

Question
How could I create a function which would take a .Rmd file (containing htmlwidgets code) as its input, and output a html file containing its JavaScript/CSS dependencies?
Specifically, the tempfile rmarkdown generates for pandoc's --include-in-header argument, when rendering to html.
Details
Example -- myfile.Rmd:
This is some text
```{r}
library(dygraphs)
dygraph(nhtemp, main = "New Haven Temperatures")
```
Running rmarkdown::render('myfile.Rmd') passes a tempfile to the --include-in-header pandoc argument, containing all the JavaScript and CSS needed to render the interactive graphic (either compressed data:uri format, or html links to copied files). This is included in the final html file, so that everything renders in the browser. It's this tempfile I'd like to be able to generate.
It's appears to be output by passing a dependencies object to the internal function rmarkdown:::html_dependencies_as_string. I'd like to be able to generate this file for arbitrary .Rmd documents containing htmlwidgets code.
Context
I'm interested in blogging using knitr and Jekyll, as Yihui outlines here. However, the disadvantage of this approach is that none of the htmlwidgets functionality works, because the dependency injection step is missed. This is because pandoc is called by Ruby/Jekyll, and so does not benefit from htmlwidgets/rmarkdown's invisible magic. I'd like to add a function to my build process which writes the dependencies to a html file, which can be included in the html header by Jeykll if required.
This is a possible solution http://benjcunningham.org/2016/06/13/hacking-together-htmlwidgets-for-jekyll.html, that refers to an older post that also offers a solution: https://brendanrocks.com/htmlwidgets-knitr-jekyll/.
A bit hacky. An official solution to this problems is still pending. There is an open issue in yihui/knitr-jekyll: https://github.com/yihui/knitr-jekyll/issues/8

knitr: Document does not change anymore

I have a .Rnw document in which I include childs. The childs produce tables via the 'latex' command of the Hmisc library in R.
When I make changes in the child documents, these changes do not anymore change the pdf document. My first guess was to use the chunk option 'eval=TRUE', but this does not change anything. Then, I saw, that the tables are actually saved to a .tex file with same name as the .Rnw document. I deleted this file and after compilation with knitr I got an error:
Error: Latexmk: Could not find file documentname.tex.
I assume, this is not the way to do it. Now I am out of ideas what to do. I appreciate some help on my problem.
Best
Simon
Allright, when trying to construct a simple example, I actually found out, that neither the packages I included nor the nesting of child documents interfere with the compilation via knitr. The reason was a simple error in the low-level .Rnw document, where a Hmisc latex table had a label, that missed a closing speech mark.
This causes then the output pdf not to change - I assume, that in this case the already constructed .tex file is included instead of letting knitr recompile the .Rnw documents and this hasn't changed since the last compilation?
What I wonder about is the different format of the landscape ctable in the document. Using a simple knitr document just with \documentclass{article} produces well placed tables. In my document using a template for the JFE, I get a table that extends over the whole page and even in footnotesize it is far away from the great appearance in the simple document. There is only a margin of less than half a cm on the right and the left. Page size is the same: both US letter... Can I probably control that via knitr or only via resizebox?

Resources