° symbol and other unicode codes in knitr

To use ° symbol in knitr or sweave, first load the latex package gensymb  and after use this code $\degree$.
Then the beginning of your code should be something like that:

\documentclass[a4paper]{article}
\usepackage{gensymb}

If the package gensymb is not present, alternative is to do:
\documentclass[a4paper]{article}
\DeclareTextSymbol{\degree}{OT1}{23}

###############################################

To use the ° symbol within a chunk, you must define the encoding. Be sure also to save the .Rnw file using the same encoding (command File -> Save with encoding... in Rstudio).

For latin1:
\documentclass[a4paper]{article}
\usepackage[latin1]{inputenc}
\begin{document}
<<>>=
degree <- "°"
print(degree)
@
\end{document}

For utf8 (based on a remark of  Yihui Xie in r-help list):
If you use pdflatex, you have to declare the font encoding
\usepackage[T1]{fontenc} when you save the document with UTF-8:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\begin{document}
<<>>=
degree <- "°"
print(degree)
@
\end{document}

For XeTeX, you will have to use different packages, fontspec and
xunicode. Here is a minimal example for XeTeX:

\documentclass{article}
\usepackage{fontspec}
\usepackage{xunicode}
\begin{document}
<<>>=
degree <- "°"
print(degree)
@
\end{document}





Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04