title: Emacs, R, Sweave: UTF-8 encoding is not recognized when launching Sweave through Emacs
body: <p>I work with Emacs 24 on Mac OS X 10.7.3 with R 2.14.0. I have a file <code>foo.Rnw</code> containing </p>
  
  <pre><code>\documentclass[
  paper=a4,% 210mm × 297mm
  pagesize% write page size to dvi
  ]{article}
  
  \usepackage[american]{babel}
  \usepackage{fancyvrb}
  
  \usepackage[utf8]{inputenc}
  
  \SweaveOpts{engine=R}
  \SweaveOpts{pdf=TRUE}
  
  \begin{document}
  Some text
  &lt;&lt;&gt;&gt;=
  sessionInfo()
  @
  \end{document}
  </code></pre>
  
  <p>My <code>.emacs</code> for working with Sweave looks like this:</p>
  
  <pre><code>(add-hook 'Rnw-mode-hook
    (lambda ()
      (add-to-list 'TeX-command-list
           '("Sweave" "R CMD Sweave %s"
             TeX-run-command nil t :help "Run Sweave") t)
      (add-to-list 'TeX-command-list
           '("Stangle" "R CMD Stangle %s"
             TeX-run-command nil t :help "Run Stangle") t)
      (setq TeX-command-default "Sweave")))
  </code></pre>
  
  <p>If I use <code>C-c C-c</code> and then choose <code>Sweave</code>, the <code>.tex</code> file looks like this:</p>
  
  <pre><code>\documentclass[
  NA
  pagesize% write page size to dvi
  ]{article}
  
  \usepackage[american]{babel}
  \usepackage{fancyvrb}
  
  \usepackage[utf8]{inputenc}
  
  
  
  
  \usepackage{Sweave}
  \begin{document}
  Some text
  \begin{Schunk}
  \begin{Sinput}
  &gt; sessionInfo()
  \end{Sinput}
  \begin{Soutput}
  R version 2.14.0 (2011-10-31)
  Platform: x86_64-apple-darwin11.2.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.14.0
  \end{Soutput}
  \end{Schunk}
  \end{document}
  </code></pre>
  
  <p>As you can see, the second line of foo.Rnw containing the non-ASCII character <code>×</code> is replaced by <code>NA</code>. I could figure out that this has to do with the localization in which <code>R</code> runs (see also the output of <code>locale: [1] C</code>. I could include statements of the form <code>export LC_MESSAGES="en_US.UTF-8"</code> in my <code>.bashrc</code> so that <code>R</code> gets the correct <code>locale</code>. I can also perfectly use <code>R CMD Sweave foo.Rnw</code> <em>from the terminal</em> (which of course loads <code>.bashrc</code> and thus sets <code>locale</code> correctly). The question is: How can I make it work through Emacs, so that I can run Sweave via <code>C-c C-c Sweave</code>? I know that Emacs is not aware of variables set in <code>.bashrc</code> , but how can I guarantee that <code>R CMD Sweave</code> issued from Emacs runs with the same localization as when I execute it from the terminal?</p>
  
  <p>I found several posts to this problem, but no solution for Macs and from within Emacs. Again, note that I can perfectly run <code>R CMD Sweave foo.Rnw</code> from the terminal (due to my settings in <code>.bashrc</code>) -- most of the posts address this issue -- it's just not working from within Emacs (which is quite annoying). </p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>My <code>~/.bashrc</code> contains:</p>
  
  <pre><code>export LC_COLLATE="en_US.UTF-8"
  export LC_CTYPE="en_US.UTF-8"
  export LC_MONETARY="en_US.UTF-8"
  export LC_TIME="en_US.UTF-8"
  export LC_MESSAGES="en_US.UTF-8"
  </code></pre>
  
  <p>Following the advice of Yihui, I put in the following in my <code>~/.Rprofile</code>:</p>
  
  <pre><code>invisible(Sys.setlocale("LC_COLLATE", "en_US.UTF-8"))
  invisible(Sys.setlocale("LC_CTYPE", "en_US.UTF-8"))
  invisible(Sys.setlocale("LC_MONETARY", "en_US.UTF-8"))
  invisible(Sys.setlocale("LC_TIME", "en_US.UTF-8"))
  invisible(Sys.setlocale("LC_MESSAGES", "en_US.UTF-8"))
  </code></pre>
  
  <p>This solved the problem. </p>