rmarkdown: new behaviour when knitting pdf documents - r

I tried to knit an old rmarkdown document to pdf recently. In the document, I had used the tilde symbol to denote a non-breaking space, e.g. 'Figure~2'. This syntax now seems to behave differently, now it prints 'Figure~2' verbatim, with the tilde printed in the document. There are many other differences, for instance % would once be interpreted as a comment, now it is printed.
I'm using Debian stretch with RStudio-1.2.1335. I can't find any documentation of this change in rmarkdown, pandoc or RStudio. Does anyone know what caused this change? Or how to revert to the old behaviour? Thanks.

The pandoc solution is to simply escape a space:
This is a short\ sentence.
Then a tilde will appear in the tex output.
What might work as well is $nbsp;:
This is a short sentence.
And if you really like your TeX then use \protect{~}:
This is a short\protect{~}sentence.

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

How can I display UTF-8 characters in knitr chunk outputs? [duplicate]

Is there a set of best practices or documentation for working with Unicode in knitr and Rmarkdown? I can't seem to get any glyphs to show up properly when knitting a document.
For example, this works in the console (in Rstudio):
> cat("\U2660 \U2665 \U2666 \U2663")
♠ ♥ ♦ ♣
But when knitting I get this:
HTML
Word
It looks like an encoding issue specific to Windows, and may be related to this issue: https://github.com/hadley/evaluate/issues/59 Unfortunately we have to wait for a fix in base R, but if you don't have to use cat(), and this expression is a top-level expression in your code chunk (e.g. not inside a for-loop or if-statement), I guess this may work:
knitr::asis_output("\U2660 \U2665 \U2666 \U2663")
It passes the character string directly to knitr and bypasses cat(), since knitr cannot reliably catch multibyte characters written out by cat() on Windows -- it depends on whether the characters can be represented by your system's native encoding.
For anyone else who came across this after trying to get emoji support in Rstudio/Rmarkdown documents, another possible issue is that if the file encoding isn't set to UTF-8, the resulting compiled document won't support emojis either.
In order for emoji to work in Rmarkdown, you must change the file encoding of the Rmd document. Go to File -> Reopen with encoding, then select UTF-8.
Once you have ensured the file is open in UTF-8 encoding, you should be able to compile with emoji support.
You should even be able to paste emoji from a browser directly into the document. 😺
It is probably a good idea to change the default encoding for all files to UTF-8 so that you don't have to deal with this issue again.
Unicode: Inline
Phew, that was close `r knitr::asis_output("\U1F605 \U2660 \U2665 \U2666 \U2663")`
Unicode: Block
```{r, echo=FALSE}
knitr::asis_output("Phew, that was close \U1F605 \U2660 \U2665 \U2666 \U2663")
```
The emo package
Unfortunately, this package isn't yet on CRAN, but it can be installed with devtools::install_github("hadley/emo")
emo::ji("face")
There are some more examples here

Less space between letters and units in rmarkdown (pdf and docx output)

I cannot find any information about spacing between letters in R Markdown. All I found only were questions about vertical spacing. I basically have two issues:
In LaTeX I usually use the siunitx package to correctly typeset numbers and units. I can of course use this in R Markdown as well if I load the package with \usepackage{siunitx}. But this does not output to the docx format, only to pdf. That brings me to my other question...
So I tried using different spaces of LaTeX to display numbers and their units with at least less space, e.g. writing 40\,m^2 to display 40 m² (in LaTeX I would use \SI{40}{\square\meter}). However, apparently R Markdown does not handle the \, nor \; at all, not even in the pdf output.
Question: What is the correct way to add smaller spaces between letters in R Markdown? (irrespective of output format!) How do I replace the \, command?
And: Is there a way to handle units nicely using R Markdown? I have found this question on the R units package, and I could live with it. That is, if I want to write hardcoded numbers like 40 m² I would have to use something like`r format(set_units(40, m2))`,right?.
Have you tried adding thin spaces using Unicode chars? For example, this page http://jkorpela.fi/chars/spaces.html suggests that "\u2009" and "\u200A" should display as thin spaces.
When I try this with a PDF document (using latex_engine: xelatex to handle Unicode), this is what I see:
It also appears to work with HTML and Word output.
Edited to add:
To be clear, this needs to go through R. If you want it inline, use code like this:
This is standard spacing: 40 m²,
this is narrow `r knitr::asis_output("40\u2009m²")` spacing.
This produces this output in PDF:

In RMarkdown what does three backticks followed by a not braced language name (```yaml , ```r, ```bibtex) mean and do?

I know that three backticks ``` are used to indicate a code block in RMarkdown and if you put a language inside of braces R markdown will try to run it but what does it mean if the language is not in braces? I can see that Yihui uses ```yaml, ```r, ```bibtex to cause code blocks to appear with color highlighting in the RMarkdown book. Does anybody know a reference to what that is actually doing and best practices with the notation? I am thinking it means show (but don't execute) this code block (and indicate the language for anybody who bothers to look into the source). If that is right, where is it documented? Does anybody know how to set the color of the words that are rendered inside such a (language named but not braced) code block in a html and/or pdf document?

Create an ePub file from markdown with math

I've spent a considerable about of time trying to figure out how I can take a markdown file, which contains TeX math and convert it into an ePub file where the math is rendered properly.
For example:
This is a markdown file. Here is a [link](www.example.com).
Here is some inline math: $\sigma_{i=1}^n \frac{\mu}{100}$
Here is an equation:
$$ y = mx + b $$
How can I convert a markdown file with the above text into an ePub file?
I've experimented with different methods of conversion using Pandoc; however, I still can not find a solution which renders the math even 50% correct.
Can anyone provide any help as to how I can do this?
I've tried this solution as well as other Pandoc option without success. Thanks in advance for the help.
Pandoc has an EPUB3 writer. It renders latex math into MathML, which EPUB3 readers are supposed to support (but unfortunately still few do). Use pandoc -t epub3 to force EPUB3 output, as opposed to EPUB2 which is the default.
Of course, this isn't much help if you want EPUB2 output or target readers that don't support MathML. Then you could try using the --webtex option, which will use a web service to convert the TeX to an image.

Resources