5

I would like to use knitr to produce pdf documents from which example code can be cleanly cut and pasted, but don't seem to be able to.

An example of the problems I run into:

The knitr manual pdf includes this code block (p.3):

## option tidy=TRUE
for (k in 1:10) {
j <- cos(sin(k) * kˆ2) + 3
print(j - 5)
} 

When copied from the pdf and then pasted into R (or SO, or etc.), it yields:

## option tidy=TRUE
for (k in 1:10) f j <- cos(sin(k) * kˆ2) + 3
print(j - 5)
g 

See how the first two code lines get combined onto one, and, worse, { and } get converted to f and g?

My questions:

First, I guess, is this something other folks experience? Does it happen just on Windows, or elsewhere as well?

If it's not just me, is there some easy workaround? Would using a different font when compiling the *.tex file produce a *.pdf document that is easier to copy-and-paste from?

(FWIW, if I instead use minted to highlight my R code, I don't have any of the same problems, so I know it's possible to get this right.)

Community
  • 1
  • 1
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • (I see now that this *might* be a more appropriate question for tex.stackexchange . Will leave it here, though, since it seems possible that knitr's R code is what causes the behavior shown above.) – Josh O'Brien May 09 '13 at 18:15
  • I've experienced the same thing on my mac. I think it can also depend on the text editor that you are pasting into. – Carson May 09 '13 at 19:56
  • @Carson -- Thanks for the confirmation! Pasting into Emacs, WinEdt, NotePad, and TextPad also fail for me. Pasting into WordpPad does produce brackets but they look odd. I suspect the brackets in the pdf file are not represented as ASCII characters, but as characters in some other non-ASCII encoding that only some text editors understand. – Josh O'Brien May 09 '13 at 20:09
  • have you tried xelatex or luatex to compile the document? It sounds like a font issue – baptiste May 09 '13 at 20:23
  • @baptiste -- I think I just figured this out (and yeah, it's a font issue). Will post my fix momentarily. (BTW, do you experience this same issue when pasting from knit'ed pdfs into R?) – Josh O'Brien May 09 '13 at 20:24
  • i tend not to copy from pdfs, they're notoriously prone to such problems. Also, I always compile with xelatex. With your example, I only get a missing linebreak before print, but the other characters are fine. – baptiste May 09 '13 at 20:47
  • @baptise -- Good to know, and I'll look into xelatex. It doesn't happen to work for me here, but that' probably because it reports an undefined font shape for 'textleftbrace' and goes and uses a different symbol instead. – Josh O'Brien May 09 '13 at 20:54

1 Answers1

8

Based on clues in this question and its accepted answer, I found that using the LaTeX fontenc package to set the font encoding to T1 fixes the problems reported above. (See also here for some explanation of why using T1 is the more or less officially recommended best practice for LaTeX documents of all sorts. Improved copy-and-pastability is one of several good reasons noted at that link.)

Here's what the start of the preamble in the fixed document looks like:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
Community
  • 1
  • 1
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • hmm... I did not know that, although I do have `\usepackage[T1]{fontenc}` in knitr-manual.Rnw, which was added by LyX automatically :) – Yihui Xie May 23 '13 at 06:55
  • @Yihui -- Interesting. I guess that's yet more evidence that it's a good default encoding. – Josh O'Brien May 29 '13 at 23:35