Some Unicode characters not displayed in RMarkdown PDF output - r

I'm trying to put together some course notes as a PDF, and am having trouble getting certain Unicode characters to display properly.
Using the xelatex latex engine is necessary for the document to be rendered at all (using the default engine results in an error due to the unrecognized characters), however, only the first Unicode character (uppercase delta) is displayed properly.
For example, when using the rmarkdown render() function to render the following .Rmd file:
---
output:
pdf_document:
latex_engine: xelatex
---
- works - Δ
- doesn't work - ⌘
The resulting PDF only shows the first unicode character (uppercase delta), and not the later one (looped square).
I know that there are different character subsets that make up the full UTF-8 character encoding, so it seems like perhaps only more basic subsets are supported.
Just to be certain, I checked the encoding of the file using iconv -f UTF-8 your_file -o /dev/null [1], and it does indeed appear to be a valid UTF-8 document.
Finally, the document renders fine as HTML using the default options, so the issue is specific to PDF output.
Any ideas how to get the second character to render to PDF?
System Information
Linux 64-bit
R 3.3.1
rmarkdown 1.0
pandoc 1.17.2
XeTeX 3.14159265-2.6-0.99996
Locale: en_US.UTF-8

The problem might be that the default font does not have that character. You'll need to find a font that has it. On Linux, fc-list lists the fonts that are available on your computer. Pick one and add it to your yaml front matter:
---
output:
pdf_document:
latex_engine: xelatex
mainfont: FreeMono
---
- works - Δ
- doesn't work - ⌘
Here, I am using FreeMono, which is the first one I found that has the ⌘ character, but there probably is a better one.

Related

Bookdown: Fix extra space before Chinese string inside R code chunk

When Chinese string inside R code chunk, the compiled PDF will get a redundancy space before the string, how to avoid this extra space? Please refer the minimum case in github - bookdown-chinese .
This issue was caused by the LaTeX package xeCJK. By default, it adds spaces between Chinese and non-Chinese characters, except in verbatim environments. In your case, the code was not actually in a verbatim environment, so you have to let xeCJK know that it should not add spaces automatically.
The solution is to add this line to your LaTeX preamble (the Highlighting environment was defined by Pandoc when converting Markdown to LaTeX to syntax highlight code, and it is based on the fancyvrb package):
\RecustomVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},formatcom=\xeCJKVerbAddon}
For R Markdown documents, this line can be save in a .tex file, e.g., preamble.tex, and included via the includes option, e.g.,
output:
pdf_document:
includes:
in_header: preamble.tex
See this Github issue for the full technical background.

RMarkdown xelatex !Missing & Inserted

I originally posted with issues here:
Rmarkdown with xelatex
I have since reinstalled, but now I'm having an entirely new issue.
Using the following code (inside a .Rmd file):
---
header-includes:
- \usepackage{graphicx}
output:
pdf_document:
latex_engine: xelatex
---
```{r}
knitr::include_graphics("F:/Big Folder/My Folder/image.png")
```
I get the following error:
! Missing $ inserted.
<inserted text>
$
l.134 .../Big Folder/My Folder/image}
pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
I think it has something to do with the space in the path, because when I put the image directly in F, it works fine.
Unfortunately, this project requires that I use folders with spaces in the name.
I'm on Windows 10 64 bit, running MikTex 2.9 with updated packages (including fontspec). Miktex is installed in program files, but I also tried an install to a different folder but had the same result.
I'm also running a recently installed/updated rstudio with updated knitr and rmarkdown packages.
This does work on pdflatex, even with spaces in the file name.
Got it, the actual image file was formatted as image_something.png.
The "_" was throwing it off and removing it fixed it.
Also, if I want to keep the underscore I need to add two escape characters "\\".
F:/Big Folder/image\\_something.png
This is somewhat different than the non rmarkdown latex solution in that it only requires a single escape character.
This solution was modified from https://tex.stackexchange.com/questions/266575/how-can-i-write-an-under-dash-in-text-without-invoking-italics-or-some-weird-fon.
I would still be curious as to why spaces are allowed on pdflatex but not xelatex.

Rstudio pdf knit fails with "Environment Shaded undefined" error

When trying to knit a PDF using a template from package rticles output: rticles::acm_article I get the following error:
! LaTeX Error: Environment Shaded undefined.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.76 \begin{Shaded}
This appears to only happen when I include code chunks within the output document.
Reproducible example:
You will need to start a new R Markdown document using the New Document -> From Template -> Association for Computing Machinery. Here is the R Markdown file:
---
title: Short Paper
author:
- name: I Am Me
email: me#email.com
affiliation: Fictional University
abstract: |
This is the abstract.
It consists of two paragraphs.
output:
rticles::acm_article:
keep_tex: true
---
## Simple test
Code chuck follows:
```{r}
plot(rnorm(10))
```
This above example, however, works if I set echo=FALSE in the header. You won't get code in the output, but for an academic paper you probably don't need it anyway, an if you do you can display it in a different manner.
Notes:
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit),
running Ubuntu 16:10
I made sure I have the texlive-latex-base, texlive-latex-recommended, and texlive-latex-extra package installed, but still no luck
I also tried generating a pdf from the intermediate .tex file, as suggested here, but I get the same error.
I considered this approach, but it didn't work, I still got an error (maybe I need to tweak to work in my context, but not sure how)
So, the issue here is the rticles templates sometimes omit a means for pandoc to inject code highlighting environments, e.g. Shaded. To get around this, you would have to insert into the template.tex preamble:
$if(highlighting-macros)$
$highlighting-macros$
$endif$
I struggled with fixing the error and actually raised another question which I was able to answer after quite some experimentation and searching for other related questions/answers.
In short the solution presented above by coatless works perfect given you know the location of the template.tex file. Obviously, I played around with the wrong files before I came across a related question on rticles and how to embed (missing) LATEX.
The longer answer can be found in my "another question". For reference here are the key steps:
locate the template.tex file in your R package rticles library. If you do not know where you have your package library use .libPaths() in your RStudio console. Then work your way to the resources subfolder of your rticle template. In my case:
R-3.5.0/library/rticles/rmarkdown/templates/ieee_article/resources.
add the fix proposed by coatless above in the preamble of the template.tex. The preamble is anything before the line \begin{document}:
$if(highlighting-macros)$
$highlighting-macros$
$endif$
save template.tex and go back to R/RStudio and hit the knit button. R code chunks are now nicely displayed, if you do not suppress their printing with echo = FALSE.
I know the post is old, but I'll put this here for reference for others who have the same problem.
The reason is that in the latex template the environment Shaded is trying to be redefined. However the environment only exists if you are using R chunks in your pdf. So if you don't have any r chunks in the pdf, it tries to redefine something which was never defined - causing the error.
The solution is to modify the \renewenvironment{Shaded} command in the latex template.
https://github.com/yihui/bookdown-chinese/commit/a3e392593b464ba31a7eceb0cd60f7e0bd112798
I found a work around. If I hide the code with chunk option echo = FALSE, a PDF is generated with no issues.
Reproducible example 1:
title: Short Paper
author:
- name: I Am Me
email: me#email.com
affiliation: Fictional University
abstract: |
This is the abstract.
It consists of two paragraphs.
bibliography: sigproc.bib
output:
rticles::acm_article:
keep_tex: true
---
## Simple test
Code chuck follows:
```{r}
plot(rnorm(10))
```
Knit to PDF fails with ! LaTeX Error: Environment Shaded undefined.
Example 2:
---
title: Short Paper
author:
- name: I Am Me
email: me#email.com
affiliation: Fictional University
abstract: |
This is the abstract.
It consists of two paragraphs.
bibliography: sigproc.bib
output:
rticles::acm_article:
keep_tex: true
---
## Simple test
Code chuck follows:
```{r echo=FALSE}
plot(rnorm(10))
```
Knit to PDF works!.
The only difference between the two examples is adding echo=FALSE to the code chunk header. You won't get code in the output, but for an academic paper you probably don't need it anyway, an if you do you can display it in a different manner.
The rticles templates typically do not directly allow shading of code. As a result,
the required $highlighting-macros$ which is used to control the shading is not included.
If for any reason, you cannot (or do not) want to directly edit the template.tex file as suggested by #coatless, you can alternatively consider include the code directly in the acm_proc_article-sp.cls file:
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
You may also wish to see my answer to a similar question, whereby the same code is put into a header.tex file in the same directory as your project.

Two space line break doesnt work on GitHub when converting .rmd to .md using github_document

I was trying to convert an .rmd document to .md to upload on GitHub using output: github_document in my YAML header.
Apparently, the conversion does not respect the two or more spaces I used in my .rmd file to force a linebreak.
Example: I created this little .rmd file:
---
title: "Line Break issue"
output: github_document
---
1. My First point
Another point right below
and more
1. My second point
I used two spaces to force a break between "My first Point" and "Another point..." and "and more". When I knit, the preview is fine, however, when I upload to GitHub the linebreaks are only adequately respected in the .rmd file as you can see here but apparently gone in the .md file as you can here.
Why is that?
EDIT:
I am using rmarkdown_0.9.6 within RStudio Version 0.99.1130 on R 3.3.0.
It looks to me as if the behavior of GitHub's markdown renderer has changed. Formerly GitHub Flavored Markdown automatically created an HTML line break for any line break within Markdown (even those without 2 trailing spaces). As a result the pandoc conversion to github_markdown stripped off the 2 trailing spaces (as they were not necessary or meaningful in github_markdown).
Posting some test files on GitHub today however it looks like they now require the two spaces to yield a line break. I don't know if this change is permanent or temporary (as it will be sure to break a bunch of existing code). To deal with this I've added a hard_line_breaks option to github_document:
https://github.com/rstudio/rmarkdown/commit/0cbfcf7ebde5f587e70a064ae7c484cfd849ba6a
With this change the YAML of your example would need to look like this to preserve the two-spaces:
---
title: "Line Break issue"
output:
github_document:
hard_line_breaks: false
---
If this appears to be a permanent change by GitHub then I'll change the default to true so github_document will produce the right markdown by default.

knitr html to Word docx using pandoc

I have been saving some example R markdown html output to Word using pandoc. I actually only do this so I can add some page breaks for easier printing:
system("pandoc -s Exercise1.html -o Exercise1.docx")
Although the output is acceptable I was wondering if there is a way to keep the original syntax highlighting of the R chunks (just as they are in the original knit HTML document)?
Also, I seem to be loosing all images in the conversion process and have to stick them into Word by hand. Is that normal?
Using the rmarkdown package (baked into RStudio Version 0.98.682, the current preview release) it's very simple to convert Rmd to docx, and code highlighting is included in the docx file.
You just need to include this at the top of your markdown text:
---
title: "Untitled" # obviously you can change this
output: word_document # specifies docx output
---
However, it seems that page breaks are still not supported in this conversion.
Why not convert the markdown directly to Word format?
Anyway, Pandoc does not support syntax highlighting in Word: "Currently, the only output formats that uses this information are HTML and LaTeX."
About the images: the Word file would definitely include those if you'd convert the markdown to Word directly. I am not sure about the HTML source, but I suppose you might have a path issue.

Resources