I'm trying to write a document in TeXstudio with knitr, and I want TeXstudio to recognise the Danish characters 'æ', 'ø' and 'å'. Normally, I have no problems doing this, using:
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[danish]{babel}
However, for .Rnw files this no longer appears to work. When I run the following MWE
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[danish]{babel}
\begin{document}
æøå
\end{document}
I get a pdf saying
<U+00E6><U+00F8><U+00E5>
I have no problem running the same MWE as a .tex, nor do I have any problem running the MWE as a .Rnw in RStudio, so I'm guessing the problem is with the way knitr is set up in TeXstudio, though I can't seem to figure out what goes wrong exactly. If anything turns on this, I've set up TeXstudio to use knitr by adding the following as a user command in build preferences
Rscript -e "library(knitr); knit2pdf('%.Rnw')"
Any ideas?
Edit: Set up TeXShop to work with knitr, it produces the same problem as TeXstudio, so I'm guessing the problem is more general than with the specifics of the way I set up knitr in TeXstudio. Still haven't figured this out.
Edit again: Did some more fiddling with this. I can in fact get the above MWE to work by adding
<<>>=
options(Encoding="UTF-8")
@
However, there's still something strange going on with regional characters. For instance, including a \Sexpr{} reintroduces the problem. That is, while
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[danish]{babel}
\begin{document}
<<>>=
options(Encoding="UTF-8")
@
æøå
\end{document}
Works just fine, the following
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[danish]{babel}
\begin{document}
<<>>=
options(Encoding="UTF-8")
@
æøå \Sexpr{1 + 1}
\end{document}
Handles the R expression, but gives the wrong output for 'æøå'. This is puzzling to me.