5

How do I highlight python code in my Rnw files (LaTeX files)? I'm using RStudio's "Compile PDF" function to run pdflatex.

There are two related questions (Q1, Q2) for markdown files, but this questions is specific to Rnw files.

Example:

\documentclass{article}
\begin{document}

<<>>=
print("This is R")
@

<<engine='python'>>=
print "This is python"
@

\end{document}

enter image description here

Community
  • 1
  • 1
kdauria
  • 6,300
  • 4
  • 34
  • 53
  • 1
    You need to install highlight: http://www.andre-simon.de – Yihui Xie Oct 28 '15 at 04:39
  • Well that was easy. Thanks @Yihui! On my Mac, `brew install highlight` did the job. Feel free to make your comment an answer. Otherwise, I'll "close out" the question by answering it myself in a few days. – kdauria Oct 28 '15 at 05:16
  • Please feel free to answer your own question. PS: I'm glad you are using Homebrew, otherwise I really hate explaining how to install highlight :) – Yihui Xie Oct 28 '15 at 06:11

2 Answers2

1

Install highlight: andre-simon.de. See @Yihui's post in the question's comments.

kdauria
  • 6,300
  • 4
  • 34
  • 53
  • 1
    For those of us who aren't as competent with computers, make sure you do the following: 1) Install Highlight, as described above. 2) Add the directory in either the `Program Files` or `Program Files (x86)` folder where Highlight is to your `PATH` variable (see [here](http://www.computerhope.com/issues/ch000549.htm) for example). If you already have other things in your `PATH` variable, place a semicolon before the directory. For example, I inserted `;C:\Program Files\Highlight` in my `PATH` variable. Once this is done, close any open RStudio windows, reopen RStudio, compile, and it will work. – Clarinetist Jan 19 '17 at 14:00
0

I used IPython notebook and you can convert it to latex pretty easy.

$ ipython nbconvert notebook.ipynb 

And you'll have a .tex file.

Yue
  • 151
  • 1
  • 8