I want to print some foreign characters in my final PDF produced with RStudio (0.99) + *.Rnw-File with knitr + XeLaTex (via Miktex).

While using XeLaTex and polyglossia I can manage that all foreign characters are print properly in the PDF, if I pass it directly to the Tex-environment of the *.Rnw-File.

If I use the Cat("...")-function to print text in the PDF within a R-chunk, it fails in some characters ("1."), in others not ("2....").

I use utf8 enconding in the RStudio settings which obviously works well inside Tex-environment. But why does the cat-expression inside the R-chunk dont like the same characters?

What can I do to use cat("...") expression (I frequently used) also for the foreign characters?


\documentclass[utf8, a4paper]{article} % with/without [utf8] does not change anything 
% !Rnw weave = knitr
% !TeX program = XeLaTeX 

Directly passed in tex environment:    
1. äöüßĐØ

<<echo=FALSE, results = 'asis'>>=
cat("Within a R-Knitr-Junk: \\newline")
cat("1. äöüßĐØ ~")
cat("2. ČĈĐĎŸ")


enter image description here

Checking the tex-file produced by RSTudio (using Notepad++ with utf8 support) the line inside the R-chunk looks like:

enter image description here

OS: Win7/64Bit, RStudio 0.99 , R 3.2.5, MikTex 2.9.5900

  • 1
    This is a question about the R cat function, not about TeX. – Thruston Apr 26 '17 at 7:28
  • Thanks to your suggestion I have checked with the RStudio console and cat("ČĈĐĎŸ") and print("ČĈĐĎŸ") both produce "CCÐDŸ" out of the box. This differs to the above posted example only in the "Ÿ" which is missing above. Until no I could not find a soluten to properly print these signs inside the console. You suggest to post it to another forum, right (which?)? – therob Apr 26 '17 at 8:34
  • With RStudio in Linux this MWE print all the characters correctly as is. Check twice that you saved the file with encoding utf8 in RStudio using the menu File > Save with Encoding ... > UTF-8 (System default). – Fran Dec 25 '18 at 16:44
  • tex.stackexchange.com/q/500959/42406 and tex.stackexchange.com/q/505086/42406 are quite related to your concerns – Basile Starynkevitch Aug 22 '19 at 6:17

It may be related with the font you're system is using to output the code, it is generally another font than the one for the normal text. It is also dependent from your system, so you may have to follow these two steps

What happen if you add this line to your latex preamble after \usepackage{fontspec}?


And you may have to use special escape unicode character in your R code, it is more robust. That is for example ß

# ß

and the easiest way to do that is to use stringi::stri_escape_unicode()

# \u010c\u0108\u0110\u010e\u0178

See https://stackoverflow.com/questions/29265172/print-unicode-character-string-in-r

  • Thanks for you hints. - Did I get it right: you suggests to use "\u00DF" instead of "ß" etc? Not very practical as I have to care about all foreign characters manually (by find&replace).... - I would prefer that this can be done "automatic" .... as I have also no problems with german umlauts äöüß --> why no way for croatian characters? – therob Apr 27 '17 at 8:26
  • - Meanwhile I did some other progress - but still not perfect: 1.) I followed r-bloggers.com/r-and-foreign-characters and using now inside the R-chunk (or in the R-console while debugging) --> Sys.setlocale("LC_CTYPE", "croatian") <- 2.) Now most of the croatian characters are drawn correctly, but not all. Any other advice with this approach? 3.) using "pdf.options(encoding = 'ISOLatin2') # or utf8, latin1..." did not bring any improvement – therob Apr 27 '17 at 8:28
  • @therob Did you try \usepackage{libertine}? Can you edit your answer to add your OS+version of programm/Package. It works fiine on my debian 9 – nebi Apr 27 '17 at 15:27
  • Update: after using sys.setlocale("..") (see above comment) most, but not all characters I mentioned are displayed properly. Funny thing is, that with the same source code all characters worked properly, after using another PC (Windows8.1) in croatia. Maybe there is any croatian package/characterset/keyboardlayout installed, which was not the case in my german test-PC before? I could not figure out why it worked, but it did without further measures from my side....strange... – therob May 15 '17 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.