# [OS X TeX] Strange TeXShop engine behaviour

Alan Munn amunn at gmx.com
Thu Sep 22 14:15:11 EDT 2011

I use Sweave to mix R code with LaTeX. I run this through a Sweave engine in TeXShop, which basically runs R on the .Rnw file (a mix of R and LaTeX) producing a .tex file with the output of the R commands included in the LaTeX.

I've discovered a problem, however, with how the file encoding is treated when R is run from within TeXShop.

Here's the summary: when I run R via an engine from within TeXShop, the encoding of the file seems to be changed when it is passed to R.  When I run the R command directly from the command line, the encoding is preserved AND when I run the .engine file from the command line (rather than from within TeXShop) the encoding is preserved.

Here's a sample file (you will need to have R installed to test this).

\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
Some non-ascii text: éüáî
<<>>=
sessionInfo()
@
\end{document}

If I run this file from the command line:

R CMD Sweave myfile.Rnw

the output .tex file is the following:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{Sweave}
\begin{document}
Some non-ascii text: éüáî
\begin{Schunk}
\begin{Sinput}
> sessionInfo()
\end{Sinput}
\begin{Soutput}
R version 2.13.1 (2011-07-08)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_2.13.1
\end{Soutput}
\end{Schunk}
\end{document}

This is correct. Notice that the non-ASCII characters in the .Rnw file get correctly coded in the output .tex file.  Furthermore, the output of the R sessionInfo() command shows that the locale is en_US.UTF8 (which is what it should be.)

Now here's the problem.

When run from within TeXShop using the following .engine:

#!/bin/bash

export PATH=$PATH:/usr/texbin:/usr/local/bin R CMD Sweave "$1"
pdflatex "\${1%.*}"

it produces the following:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{Sweave}
\begin{document}
NA
\begin{Schunk}
\begin{Sinput}
> sessionInfo()
\end{Sinput}
\begin{Soutput}
R version 2.13.1 (2011-07-08)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_2.13.1
\end{Soutput}
\end{Schunk}
\end{document}

As you can see, two things have changed.  The line containing the non-ASCII characters has been converted to NA by Sweave because the locale has been changed to C (which is basically plain ASCII).

What's even stranger is that if I use the .engine file to run Sweave from the command line, I get the correct output again.

I.e.

~/Library/TeXShop/Engines/Sweave.engine myFile.Rnw

doesn't change the locale, and the file is correctly produced as UTF-8.

Does anyone have any idea what might be causing this?

Thanks

Alan

--
Alan Munn
amunn at gmx.com