R-markdown latex font encoding failing - latex

Edit: Problem solved. It turns out I had some missing packages, and rmarkdown doesn't give a very good error message for this. If anyone else has this problem, you have to use the "keep_tex" option in the yaml. You can then see in the tex file all of the required tex packages.
I'm on Windows 7. I have the latest Miktex, R, rmarkdown, rstudio.
When I try to produce a new rmarkdown document and output to pdf, it fails.
It seems to be a font encoding which is missing. I've tried searching the internet for omxenc.dfu and uenc.dfu files, but it turned up nothing. I was going to post this in the latex exchagne, but I can produce latex documents fine without using rmarkdown.
Any help would be great!
Here's the rmarkdown
---
title: "Untitled"
author: "Triceraflops"
date: "19 März 2018"
output: pdf_document
---
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for
authoring HTML, PDF, and MS Word documents. For more details on using R
Markdown see <http://rmarkdown.rstudio.com>.
And here's the output of the R Markdown console
"C:/Users/triceraflops/Documents/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS missing-encoding.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output missing-encoding.tex --template "C:\Users\triceraflops\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"
output file: missing-encoding.knit.md
Output created: missing-encoding.pdf
Error in tools::file_path_as_absolute(output_file) :
file 'missing-encoding.pdf' does not exist
Calls: <Anonymous> -> <Anonymous>
In addition: Warning messages:
1: running command '"pdflatex" -halt-on-error -interaction=batchmode
"missing-encoding.tex"' had status 1
2: In readLines(logfile) :
incomplete final line found on 'missing-encoding.log'
Execution halted
And finally here's the last part of the output log file where it fails.
Now handling font encoding OMX ...
... no UTF-8 mapping file for font encoding OMX
Now handling font encoding U ...
... no UTF-8 mapping file for font encoding U
defining Unicode char U+00A9 (decimal 169)
defining Unicode char U+00AA (decimal 170)
defining Unicode char U+00AE (decimal 174)
defining Unicode char U+00BA (decimal 186)
defining Unicode char U+02C6 (decimal 710)
defining Unicode char U+02DC (decimal 732)
defining Unicode char U+200C (decimal 8204)
defining Unicode char U+2026 (decimal 8230)
defining Unicode char U+2122 (decimal 8482)
defining Unicode char U+2423 (decimal 9251)

Related

Pandoc fails to render valid LaTeX macro definitions from markdown (and R markdown)

In R, I have character objects that contain LaTeX macro definitions. The
challenge is to use these objects in R Markdown documents, so that the
macro definitions are rendered correctly when the .Rmd files are converted to
LaTeX (and then to PDF). It is a challenge because Pandoc (v2.9.1 and 2.9.2)
fails to render some macro-generating code correctly, even when that
code is valid LaTeX.
Here is a minimal example. First, consider this Rmd file:
---
title: "Rendering LaTeX Macros from R Objects"
output:
pdf_document:
keep_md: true
keep_tex: true
---
```{r}
withoutBraces <- "\\newcommand\\withoutBraces{This is a sentence.}"
withBraces <- "\\newcommand{withBraces}{This is a sentence.}"
```
```{r, results = "asis"}
writeLines(withoutBraces)
writeLines(withBraces)
```
Knitting this .Rmd file from RStudio produces a .tex file that includes the
following output:
\newcommand\withoutBraces{This is a sentence.}
but
\textbackslash newcommand\{withBraces\}\{This is a sentence.\}
In other words, the \withoutBraces command is rendered correctly in the .tex
document, but the \withBraces command is not. Inspection
reveals that the rmarkdown::render() part of the knitting process is fine, in
the sense that it produces an unproblematic .md file. The problem lies with
pandoc: when it converts the .md file to a .tex file, the \withBraces
command doesn't render correctly.
If I were writing .md files instead of .Rmd files, I could use "generic raw
attributes" in my code chunks to get the \withoutBraces macro definition to
render correctly, as in this example from
#mb21. But I don't see a way to
do that when working with R Markdown files. Is there anything that I can do
to get the \withoutBraces definition to render correctly when I am knitting
an .Rmd file to LaTeX and PDF?
The problem lay with a LaTeX formatting error on my part, not in any problem with pandoc. I had written
withBraces <- "\\newcommand{withBraces}{This is a sentence.}"
when I should have written
withBraces <- "\\newcommand{\\withBraces}{This is a sentence.}"
When I use the second string instead of the first, there is no problem with the conversion to LaTeX.

How to know charset of character?

A python script fail when trying to encode a supposed utf-8 string in iso-8859-1:
>>> 'à'.encode('iso-8859-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u0300' in position 1: ordinal not in range(256)
How to know wich charset is that character ? When encode it in utf-8:
>>> 'à'.encode('utf-8')
b'a\xcc\x80'
a then \xcc\x80. I can fount \xcc\x80 in http://www.utf8-chartable.de/unicode-utf8-table.pl?start=768&names=-&utf8=string-literal utf8 table.
But it is utf-8 ? If it is utf-8 why 'à'.encode('utf-8') can't encode this string in iso-8859-1 ?
It's a bit unclear where the 'à' character comes from. In fact, it's a combining character sequence and you need to normalize it. Next python script uses unicodedata module to self-explain and solve your questions:
import sys, platform
print (sys.stdout.encoding, platform.python_version())
print ()
import unicodedata
agraveChar='à' # copied from your post
agraveDeco='à' # typed as Alt+0224 (Windows, us keyboard)
# print Unicode names
print ('agraveChar', agraveChar, agraveChar.encode('utf-8'))
for ins in range( 0, len(agraveChar)):
print ( agraveChar[ins], unicodedata.name(agraveChar[ins], '???'))
print ('agraveDeco', agraveDeco, agraveDeco.encode('utf-8'))
for ins in range( 0, len(agraveDeco)):
print ( agraveDeco[ins], unicodedata.name(agraveDeco[ins], '???'))
print ('decomposition(agraveChar)', unicodedata.decomposition(agraveChar))
print ('\nagraveDeco normalized:\n')
print ("NFC to utf-8", unicodedata.normalize("NFC" , agraveDeco).encode('utf-8'))
print ("NFC to latin", unicodedata.normalize("NFC" , agraveDeco).encode('iso-8859-1'))
print ("NFKC to utf-8", unicodedata.normalize("NFKC", agraveDeco).encode('utf-8'))
print ("NFKC to latin", unicodedata.normalize("NFKC", agraveDeco).encode('iso-8859-1'))
Output:
==> D:\test\Python\40422359.py
UTF-8 3.5.1
agraveChar à b'\xc3\xa0'
à LATIN SMALL LETTER A WITH GRAVE
agraveDeco à b'a\xcc\x80'
a LATIN SMALL LETTER A
̀ COMBINING GRAVE ACCENT
decomposition(agraveChar) 0061 0300
agraveDeco normalized:
NFC to utf-8 b'\xc3\xa0'
NFC to latin b'\xe0'
NFKC to utf-8 b'\xc3\xa0'
NFKC to latin b'\xe0'
==>

Unicode characters not recognised by LaTex in RMarkdown Pander table

I am trying to get some greek characters into the rownames of a table in rmarkdown knitting to pdf. I am using knitr, pander and MacTex. It seems like pander accepts some unicode characters but not others. When i use \u2013 (emdash) it works.
---
title: "Untitled"
author: "Llew Mills"
date: "24 June 2016"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
echo=FALSE, warning=FALSE, message=FALSE, dev = 'pdf')
```
``` {r stuff}
library(pander)
m1 <- rnorm(12,8,3)
m2 <- rnorm(12,4,5)
m3 <- rnorm(12,5,1)
mR <- rep("gene \u2013", 12)
df <- data.frame(mR,m1,m2,m3)
pander(df, justify = "right", style = "simple")
```
However if instead of that unicode character I use the unicode for gamma \u03B3 i get the error message ! Package inputenc Error: Unicode char \u8:γ not set up for use with LaTeX.
Does anyone know if there is a list of unicode characters that are compatible with LaTex, or alternatively a way to get latex to accept all unicode characters?
I think it's an issue with your locale/console settings and not really a pander issue, as this seems to work fine in a console with support for Unicode chars:
But pdflatex indeed sucks with Unicode chars, you might better try eg xelatex.
PS: sorry for posting this comment as an answer, but this was the easiest way to add an image

compiling latex file generated by RStudio

I am trying to edit the latex file generated by RStudio but it seems the file generated by RStudio does not compile out of the box.
Here is the template rmarkdown (edited a little as I want to show code in my slides)
---
title: "Untitled"
author: "SN248"
date: "April 17, 2016"
output:
beamer_presentation:
keep_tex: true
theme: "CambridgeUS"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
Some code
```{r eval=FALSE, echo=TRUE}
install.packages("mypackage")
library(mypackage)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=TRUE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Note, that I added keep_tex: true to get the intermediate tex file, the generated tex file is pasted below:
\begin{frame}[fragile]{R Markdown}
This is an R Markdown document. Markdown is a simple formatting syntax
for authoring HTML, PDF, and MS Word documents. For more details on
using R Markdown see \url{http://rmarkdown.rstudio.com}.
Some code
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{install.packages}\NormalTok{(}\StringTok{"mypackage"}\NormalTok{)}
\KeywordTok{library}\NormalTok{(mypackage)}
\end{Highlighting}
\end{Shaded}
\end{frame}
\begin{frame}[fragile]{Including Plots}
You can also embed plots, for example:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(pressure)}
\end{Highlighting}
\end{Shaded}
\includegraphics{Untitled_files/figure-beamer/pressure-1.pdf}
Note that the \texttt{echo\ =\ FALSE} parameter was added to the code
chunk to prevent printing of the R code that generated the plot.
\end{frame}
I noticed that the generated tex file does not compile out of the box. So, I added the necessary bits (i.e., package listings for code highlighting) so that it compiles
\documentclass[hyperref={pdfpagelabels=false}]{beamer}
\usepackage{lmodern}
\usepackage{listings}
\usetheme{CambridgeUS}
\begin{document}
...
\end{document}
I am still not able to compile this tex file as the bit begin{Shaded} does not compile, I get the following error:
LaTeX Error: Environment Shaded undefined
My question is, how to generate tex file from RStudio which compiles out of the box. If not, which packages should I use to compile the tex code (especially to show code with highlighting) shown below
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{install.packages}\NormalTok{(}\StringTok{"mypackage"}\NormalTok{)}
\KeywordTok{library}\NormalTok{(mypackage)}
\end{Highlighting}
\end{Shaded}
Just to give some context, I am trying to compile the tex file so that I can add sections and subsections in the tex code, I am not sure how to do that in the rmarkdown file.
Thank you very much for your help.
SN248
I can see that you are using RMarkdown and want to generate a stand alone .tex file that can be compiled without knitr.
I do the same thing with SWeave. As SWeave is a latex document with R code, you can easily compile it once SWeave is run and figures are created.
For Example, I have created this document in RStudio and compile it in Rstudio.
\documentclass{article}
\title{Best title ever}
\author{XTriMa, PhD}
\begin{document}
\maketitle
\tableofcontents
\section{Method} \label{intro}
Let me make two vectors with random numbers and plot them.
<<>>=
x<-runif(100)
y <- x^(sin(x))
plot(x, y)
#
\end{document}
My folder looks like this
figure test.log test.Rnw test.tex
test-concordance.tex test.pdf test.synctex.gz test.toc
Now if you run
$pdflatex test.tex
It works as you intended. However command "latex" will not work as it can not work with pdf graphics in figures.
Advantage: I can work with R and latex very efficiently and get rid of intermediate software/clients like RStudio and Sweav.

Inconsistent IO character reading when converting encoding

In Ruby 1.9.3-429, I am trying to parse plain text files with various encodings that will ultimately be converted to UTF-8 strings. Non-ascii characters work fine with a file encoded as UTF-8, but problems come up with non-UTF-8 files.
Simplified example:
File.open(file) do |io|
io.set_encoding("#{charset.upcase}:#{Encoding::UTF_8}")
line, char = "", nil
until io.eof? || char == ?\n || char == ?\r
char = io.readchar
puts "Character #{char} has #{char.each_codepoint.count} codepoints"
puts "SLICE FAIL" unless char == char.slice(0,1)
line << char
end
line
end
Both files are just a single string áÁð encoded appropriately. I have checked that the files have been encoded correctly via $ file -i <file_name>
With a UTF-8 file, I get back:
Character á has 1 codepoints
Character Á has 1 codepoints
Character ð has 1 codepoints
With an ISO-8859-1 file:
Character á has 2 codepoints
SLICE FAIL
Character Á has 2 codepoints
SLICE FAIL
Character ð has 2 codepoints
SLICE FAIL
The way I am interpreting this is readchar is returning an incorrectly converted encoding which is causing slice to return incorrectly.
Is this behavior correct? Or am I specifying the file external encoding incorrectly? I would rather not rewrite this process so I am hoping I am making a mistake somewhere. There are reasons why I am parsing files this way, but I don't think those are relevant to my question. Specifying the internal and external encoding as an option in File.open yielded the same results.
This behavior is a bug. See http://bugs.ruby-lang.org/issues/8516 for details.

Resources