Remove \SweaveOpts{concordance=TRUE}
in your Rnw file. It is invalid and useless to knitr
.
Update: I have fixed the plot problem in the development version (v1.2.4). See here for instructions on installation. It will go to the stable version 1.3 eventually, but perhaps not until a month later.
You can do what you want, but the approach is a bit different from Sweave and knitr.
With Sweave and knitr, the file you create is a .Rnw
, and the output is .tex
. In the .tex
, each \Sexpr{...}
is replaced by its output. So in the final .tex
file that is compiled, there are no \Sexpr{...}
.
With PythonTeX, you create the .tex
directly, so everything you do has to be valid .tex
. Based on how \SI
works, having \py
inside it causes problems--\SI
expects numbers, not commands.
There are a number of ways you could work around this. I've given two examples below. In the first approach, I've created a Python function SI()
that takes a variable and a unit, and returns an \SI
command. In the second approach, I've created a new LaTeX command \pySI
that does the same thing, just using a more LaTeX-style interface. This last approach will have problems if you need to use the #
and %
characters in the arguments, but that shouldn't be an issue for this application.
\documentclass{article}
\usepackage{siunitx}
\usepackage{pythontex}
\begin{pycode}
def SI(var, unit):
return '\\SI{' + str(var) + '}{' + unit + '}'
\end{pycode}
\newcommand{\pySI}[2]{\py{'\\SI{' + str(#1) + '}{#2}'}}
\begin{document}
\pyc{y = 4}
The value of y is \py{SI(y, r'\metre')}.
The value of y is \pySI{y}{\metre}.
\end{document}
Best Answer
With the command line with
Sweave
:Really you need only two steps for a simple document as this
my_sweave_file.Rnw
:From the command line is enough:
Or even only one:
In case that you have cross-references (
\ref
or\cite
commands, table of contents, index, glossaries, etc.) this will not enough, so you must also compile the generated.tex
file with the appropriate tools and rerunpdlatex
as needed. See Tools for automating document compilation if you have some problem with the compilation sequences.With the command line with
knitr
:A compilation of a simple document could be done with:
Obviously, in complex Latex documents, you will need to add some compilation steps as above.
Without the command line
For the sake of completeness, an easy way to compile LateX+R documents is to work with LyX instead of LaTeX directly, loading the
Sweave
orRnw(knitr)
modules, making automatically in both case the "R Noweb" (.Rnw
) to LaTeX exportation, and all the compilation steps of the LaTeX file.Another possibility is use Rstudio to edit the .Rnw file and just push the PDF icon with the "Compile PDF" label. It is worth mention that you use also here Rmarkdown files (.Rmd extension) that offer the simplified syntax of markdown for text but can be converted as well to a LaTeX PDF. In this in case the compilation icon in changed by a is done by a dropdown menu "Knit" where you can select "Knit to PDF", with an obvious meaning.
Warning: The MWE without R chunk options options can be compiled equally with
Sweave
andknit
functions, but there are chunk options specific for each of these functions: Some options cannot be used withknitr
and manyknitr
options cannot be used with the olderSweave
, so actually is preferable useknitr
.