2
votes

I'm trying to write a LyX document in English with some Chinese interspersed, and I also want knitr for the figures and analyses. My installation likes knitr, and it likes Chinese (using the "Alternative" approach here: http://wiki.lyx.org/LyX/Chinese#toc5), but I can't get documents with CJK in them to knit.

My test document has this text (the Tex in a TeX code box):

Hello    
\cntxt{你好吗}

My user-defined preamble is (from the LyX wiki):

\usepackage{CJKutf8}
\usepackage[overlap, CJK]{ruby}
\newenvironment{SChinese}{%
\CJKfamily{gbsn}%
\CJKtilde
\CJKnospace}{}
\newcommand{\cntxt}[1]{\begin{CJK}{UTF8}{}\begin{SChinese}#1\end{SChinese}\end{CJK}}

Doc settings: my language is English, Encoding is utf8 (the other options for these don't seem to change anything). Turned off language marking in LyX as mentioned on Xie Yihui's site.

When knitr is loaded and I do View [PDF (pdflatex)], it hiccups when it sends it to R:

10:13:40.589: Warning in readLines(input, warn = FALSE) :
10:13:40.597:   invalid input found on input connection 'test-chinese-2.Rnw'

And latex doesn't like what it gets back:

! File ended while scanning use of \cntxt.

Taking out the Chinese characters removes the error. Leaving the Chinese and unloading knitr also removes the error. Any suggestions for how to keep the CJK, keep knitr loaded, and still get a document out?

Lyx is native Windows 2.0.4, R is 2.15.1, knitr is 0.8. The 034-chinese.Rhtml example in knitr-examples works perfectly.

Here is the messages window output:

13:45:45.419: Previewing ...
13:45:45.581: Rscript --verbose --no-save --no-restore "C:/Program Files (x86)/LyX20/Resources/scripts/lyxknitr.R" "C:/Users/Patrick/AppData/Local/Temp/lyx_tmpdir.Hp8472/lyx_tmpbuf2/""test-chinese-2.Rnw" "C:/Users/Patrick/AppData/Local/Temp/lyx_tmpdir.Hp8472/lyx_tmpbuf2/""test-chinese-2.tex" UTF-8 "C:/Users/Patrick/Downloads/"
Running: Rscript --verbose --no-save --no-restore "C:/Program Files (x86)/LyX20/Resources/scripts/lyxknitr.R" "C:/Users/Patrick/AppData/Local/Temp/lyx_tmpdir.Hp8472/lyx_tmpbuf2/""test-chinese-2.Rnw" "C:/Users/Patrick/AppData/Local/Temp/lyx_tmpdir.Hp8472/lyx_tmpbuf2/""test-chinese-2.tex" UTF-8 "C:/Users/Patrick/Downloads/"
13:45:47.202: [1] TRUE
13:45:47.203: [1] TRUE
13:45:47.230: running
13:45:47.237:   'C:\PROGRA~1\R\R-215~1.1\bin\i386\Rterm.exe --slave --no-restore --no-save --no-restore --file=C:/Program Files (x86)/LyX20/Resources/scripts/lyxknitr.R --args C:/Users/Patrick/AppData/Local/Temp/l
13:45:47.247: yx_tmpdir.Hp8472/lyx_tmpbuf2/test-chinese-2.Rnw C:/Users/Patrick/AppData/Local/Temp/lyx_tmpdir.Hp8472/lyx_tmpbuf2/test-chinese-2.tex UTF-8'
13:45:47.252: 
13:45:47.258: Warning in readLines(input, warn = FALSE) :
13:45:47.266:   invalid input found on input connection 'test-chinese-2.Rnw'
13:45:47.802: 
  |                                                                       
  |                                                                 |   0%
  |                                             
13:45:47.813:                           
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>| 100%
13:45:47.820:   ordinary text without R code
13:45:47.826: 
13:45:47.828: 
13:45:47.832: [1] "test-chinese-2.tex"
13:45:47.836: [1] TRUE
13:45:47.837: [1] TRUE
13:45:47.843: 
13:45:47.844: 
13:45:47.848: processing file: test-chinese-2.Rnw
13:45:47.854: output file: C:\Users\Patrick\Downloads\test-chinese-2.tex
13:45:47.860: 
13:45:47.875: pdflatex  "test-chinese-2.tex"
Running: pdflatex  "test-chinese-2.tex" > nul
13:45:48.186: This is pdfTeX, Version 3.1415926-2.3-1.40.12 (MiKTeX 2.9)
13:45:48.215: entering extended mode
13:45:48.219: 
13:45:48.224: (C:\Users\Patrick\AppData\Local\Temp\lyx_tmpdir.Hp8472\lyx_tmpbuf2\test-chinese
13:45:48.229: -2.tex
13:45:48.233: LaTeX2e <2011/06/27>
13:45:48.239: Babel <v3.8m> and hyphenation patterns for english, afrikaans, ancientgreek, ar
13:45:48.247: abic, armenian, assamese, basque, bengali, bokmal, bulgarian, catalan, coptic, 
13:45:48.255: croatian, czech, danish, dutch, esperanto, estonian, farsi, finnish, french, ga
13:45:48.263: lician, german, german-x-2009-06-19, greek, gujarati, hindi, hungarian, iceland
13:45:48.269: ic, indonesian, interlingua, irish, italian, kannada, kurmanji, lao, latin, lat
13:45:48.275: vian, lithuanian, malayalam, marathi, mongolian, mongolianlmc, monogreek, ngerm
13:45:48.280: an, ngerman-x-2009-06-19, nynorsk, oriya, panjabi, pinyin, polish, portuguese, 
13:45:48.285: romanian, russian, sanskrit, serbian, slovak, slovenian, spanish, swedish, swis
13:45:48.291: sgerman, tamil, telugu, turkish, turkmen, ukenglish, ukrainian, uppersorbian, u
13:45:48.296: senglishmax, welsh, loaded.
13:45:48.300: 
13:45:48.638: Error while previewing format: pdf2C:\Users\Vincent\Documents\LyX\source\lyx20x\src\support\Systemcall.cpp(273): Systemcall: 'pdflatex  "test-chinese-2.tex"' finished with exit code 1
1
You're using Windows so perhaps a locale issue as described here? It may sound like voodoo but unfortunately R under Windows does not seem to play well with CJK scripts. stackoverflow.com/questions/11069908/…SlowLearner
Yeah, I've done battle with R and locales before (and lost; see the question I asked months ago, which seems to be a duplicate of the one you linked me to). If I have time maybe I'll mess around with the LyX->knitr script and see if the "fix" in that question works. The thing that gets me is knit() does fine with CJK in the 034-chinese.html from knitr examples, so there is that small sliver of hope.Patrick
please paste the complete log if possible, otherwise I have to guess many things, e.g. if this bug is biting you lyx.org/trac/ticket/7741 see also mail-archive.com/[email protected]/msg93501.htmlYihui Xie
I posted the msg window output now. Any thoughts?Patrick
Fiddling with interface language does this. In either Chinese (simplified) or English, a warning 13:59:26.879: Setting LC_CTYPE=en_US failed. Default gives no such message, but the locale is always NULL if I put a getOption("locale") in lyxknitr.R. Does that help at all?Patrick

1 Answers

0
votes

Not sure if this counts as an answer, but taking a page from SlowLearner's book I messed around with the locale settings in LyX20\Resources\scripts\lyxknitr.R.

Adding the code

## add a kludge for locale 
print(Sys.getlocale(category = "LC_CTYPE"))
Sys.setlocale("LC_CTYPE","chinese")

before knit() did what I wanted. God forbid I should ever want to put another non-Roman language in the document, though.