I want to use knitr with TeXShop. So far I can’t get knitr and TeXShop to talk. I’m not sure how to make a reproducible example nor how to make the following succinct, but here’s what I’ve tried:

  1. Versions, recently updated, are: R 3.4.2, knitr 1.17, TeXShop 3.88. Within RStudio on a Mac with Sierra OS, knitr has worked well recently and often. The manuscript I want to migrate is an .rmd. [Btw, the reason finally to switch is blurred plots as described at https://stackoverflow.com/questions/29568923/geom-raster-comes-out-smeared-when-saving-to-pdf. Preview, which recipients are likely to use, no longer has an option to disable Smooth Text and Line Art: https://discussions.apple.com/thread/7681819?start=0&tstart=0. I use TeXShop rather than, for example, LyX, is that my work group does.]

  2. Per https://yihui.name/knitr/demo/editors/, I created Knitr.engine. In TeXShop, knitr now appears as an option in Programs.

  3. Per https://iangow.wordpress.com/2014/04/04/using-knitr-with-texshop/, I made Knitr.engine executable with chmod +x ~/Library/TeXShop/Engines/Knitr.engine. Rebooted afterward for good measure.

  4. Following https://stackoverflow.com/questions/14617041/how-can-i-see-the-current-value-of-my-path-variable-on-os-x, included in the returned string for echo $PATH is /Library/TeX/texbin. Since understanding this part is a bit beyond my skill level, here’s the full response: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/TeX/texbin

  5. A year ago TeXShop worked fine for a long document not connected to r. To check again, I copied https://github.com/yihui/knitr-examples/blob/master/005-latex.Rtex to TeXShop then Typeset with Program = LaTeX. Obviously except the r part, it rendered nicely. In case this is a clue, in place of r snippets like “…x is \rinline{x[1]}. And…” the .pdf has a polite “…x is SOMETHING WRONG WITH knitr. And…”

  6. Using the same example file, I changed the TeXShop Program to knitr then tried Typeset. The console screen is blank and no .pdf is returned.

  7. Potentially related questions: Comments on the unanswered https://stackoverflow.com/questions/33570145/texshop-and-knitr didn’t get me un-stuck. I don’t understand https://stackoverflow.com/questions/22901174/synchronise-pdf-to-rnw-in-knitr-with-texshop beyond step 1 enough to know if it should help. The versioning issue described at https://stackoverflow.com/questions/27915263/knitr-in-texshop-uses-late-r-version doesn’t apply; The version shown in my R.framework is 3.4.

Thank you very much for any ideas.

  • Welcome to TeX.SX! – Bobyandbob Nov 8 '17 at 17:28
  • The code of the .engine file in the link in 2 still uses /usr/texbin to set the path. Did you change this to /Library/TeX/texbin? – Alan Munn Nov 8 '17 at 17:40
  • What kind of pre-processing needs to be done? What is the contents of your engine? Do you need to use shell-escape so the proceeding can be done by a package you use? – Herb Schulz Nov 8 '17 at 17:41
  • I think the problem is with the demo file you are trying (assuming you have (i) installed the knitr library in R and (ii) adjusted the path in the knitr.engine to be /Library/TeX/texbin). Try running this file instead: github.com/yihui/knitr/blob/master/inst/examples/… – Alan Munn Nov 8 '17 at 20:34
  • @HerbSchulz There is no preprocessing that needs to be done, and documents don't need shell-escape for compilation. The knitr.engine linked in point 2 works fine once the path is corrected. – Alan Munn Nov 8 '17 at 21:38

Here are some things that should help.

  1. The knitr.engine you linked to sets an old path for the TeX binaries. It should be /Library/TeX/texbin But as Herb Shulz notes in the comments, the path doesn't actually matter, since TeXShop sets the path correctly.

  2. Make sure you have installed the knitr package in R.

  3. knitr recognizes various kinds of syntax for embedding the R code, and this depends on the file extension of the source file. The code you linked to is an .Rtex file, and not an .Rnw file, which uses a different embedding syntax.

The .Rtex syntax is

% begin.rcode 

% end.rcode

But this requires that you save the file as .Rtex, which TeXShop unfortunately does not allow you to do. (Even though it can open such files.)

The .Rnw syntax is

<< ... >>=


There are some other differences. See knitr Reference Card for a quick overview.

Finally, there is also .Rmd syntax:

```{r ... }


But TeXShop also doesn't know how to save this extension either (although it can open such files.)

Because of the way TeXShop manages its Save dialogue, only a specified set of file extensions are allowed, so the full range of files that knitr recognizes are not available if you are trying to save a new untitled document. At present, TeXShop only knows about .Rnw files. However, TeXShop can read other file types without any trouble, so you can manually change the file extension to .Rtex and then in the Finder choose to have that file type be always opened by TeXShop (choose Get Info for the selected file and then choose TeXShop as the application to open the file.)

Furthermore, if you add

% !TEX TS-program = knitr

to the first line of your document, the knitr engine will automatically be chosen for that file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.