# tikz has probably problems with czech babel when compiling .Rnw

## Problem

Tikz seems to have a problem with \usepackage[czech]{babel}.

I had a moderately big .Rnw document written in czech with some plots via knitr chunks. When I set dev = 'tikz' the TeXmaker sent me an error:

### Error

Preview: PDFoutput 1 ! Package tikz Error: + or - expected. See tikz package documentation for explanation.


I had no time for fixing it so I have used dev='pdf' which has no great LaTeX axis label postprocessing (that's bad for someone who has to use awful lot of greek letters, subscripts, etc.).

I have done some research since then and haven't found it in tikz documentation, but found that a few people tackled the same problem with tikz + LaTeX and they said it was due to the czech babel LaTeX package.

So I tried a minimal example which compiles without \usepackage[czech]{babel} but with it it gives the same error. Removing it also allows the big document to compile but it's not a solution since it's written in czech so I need the babel.

### Code

edit: added some greek letters to plot label since that is why I need tikz and a czech testing sentence with czech specific letters and another one with czech specific quotation marks handled by the czech babel package.

If you uncomment the second sentence it doesn't compile even with commented \usepackage[czech]{babel} since babel handles the „“ characters. I could replace the quotation mark characters with some escape sequences, but this doesn't appear to me as a nice option.

\documentclass[12pt]{article}
\usepackage[czech]{babel}
\usepackage{tikz}
\begin{document}

Příliš žluťoučký kůň úpěl ďábelské ódy.
%„Příliš žluťoučký kůň úpěl ďábelské ódy.“

<<dev="tikz">>=
library(tikzDevice)
plot(1:10 , xlab = "$\\alpha_{\\beta^\\gamma}$")
@

\end{document}


### Environment

• OS: Windows 7
• Compiled in: TeXmaker 4.1.1
• knitr called through: R x64 v 3.0.3
• R package: tikzDevice v 0.7.0
• TeX package: pgf v 3.0.0

## Question

Can anything be done to fix this error?

• Spanish babel option produces similar problems (take a look at tex.stackexchange.com/a/48787/1952), may be your czech babel documentation provides equivalent solution to spanish es-noquoting Mar 14 '14 at 8:58

Good evening, this is working for me (let's say we use er0.Rnw):

    \usepackage[czech]{babel}
\catcode"=13
\catcode-=13


After adding those two lines we call these three lines in R (e.g. we could use pdflatex er0.tex later, or, let's use knit2pdf instead of knit...):

    require(knitr)
require(tikzDevice)
knit("er0.Rnw")


We would encounter the same problem with the slovak option in babel. Czech+Slovak quotation marks style „“ is working well then.

The Knitr package is using the tikzDevice package which needs TeX dimensions for its conversion (it uses an extra TeX file for this process), so we could use another approach in R bearing that in mind without touching the TeX code in the original er0.Rnwfile:

    require(knitr)
require(tikzDevice)
tikzOld=options("tikzMetricPackages")  # Let's store the old values...
tikzOld                                # Show us the old values...
tikzNew=c("\\catcode\"=13","\\catcode-=13","\\usepackage[T1]{fontenc}","\\usetikzlibrary{calc}")
tikzNew                                # List us the new values...
knit("er0.Rnw")                        # Use the new values...
options(tikzMetricPackages=tikzOld)    # Return me to the original setting...


It could be also useful if we wish to use the IL2 font encoding for typesetting and measuring the characters etc.

The last step is to run {pdf|lua|xe}latex over the er0.tex file. Let me hope it helped a bit, this is my very first post at TeX.SE. Knitr+tikzDevice rulez! :-)

Added \pagestyle{empty} and \usepackage[utf8]{inputenc} in the preambule (for pdflatexing later), uncommented the second sentence in the body and added fig.width=4, fig.height=4 in the chunk in the er0.Rnw file; manually added \vspace*{-15mm} before inclusion of the figure in the er0.tex file to save some space here.

• Welcome to TeX.SX! I'd prefer a full size beer, though. ;-) Mar 15 '14 at 20:43
• If I add the \catcode lines it throws this error. [compiling with "C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" -e "library(knitr);knit2pdf('%.Rnw')" in TeXstudio] Error: running 'texi2dvi' on 'helloworld.tex' failed LaTeX errors: ws.code.tex:1023: Package pgf Error: Unknown arrow tip kind '>'.
– VaNa
Mar 16 '14 at 16:25
• So I tried the other one by changing the compilation command to "C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" -e "library(knitr);library(tikzDevice);tikzOld=options("tikzMetricPackages");tikzNew=c("\\catcode\"=13","\\catcode-=13","\\usepackage[T1]{fontenc}","\\usetikzlibrary{calc}");options(tikzMetricPackages=tikzNew);knit("er0.Rnw");options(tikzMetricPackages=tikzOld)" and it also throws an error: Error in options(tikzMetricPackages) : object 'tikzMetricPackages' not found Execution halted
– VaNa
Mar 16 '14 at 16:42
• About the first option. You are not loading the tikzDevice package and therefore, it seems, the knitr package is having a problem how to deal with it. Please try "C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" -e "library(knitr);library(tikzDevice);knit2pdf('%.Rnw')" first. Also, at the moment, I would try to minimize number of tools used and give a try to knit('%.Rnw') instead in case it is still not working. Mar 16 '14 at 17:53

I figured out a hack:

1. Comment out the babel package: %\usepackage[czech]{babel}
2. Run knitr on the .Rnw file
3. Open the .tex document generated after the knitr run, find %\usepackage[czech]{babel} and uncomment it.
4. Compile the .tex

However if someone would be able to figure out something better I would appreciate it.

edit

It doesn't work if there are czech quotation marks „“ in the .Rnw file.

# Summary

### for future generations

tested on TeXstudio 2.7.0 and the rest of the environment section from the question (not TeXmaker for which it shouldn't be much different)

## When it appears

If you wish to compile .Rnw file while producing some figures with dev='tikz' option in knitr chunks

AND you use \usepackage[czech]{babel} or \usepackage[slovak]{babel}

AND you use „“ quotation marks in your .Rnw document you will probably run into trouble trying to compile it with a command like

"C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" -e "library(knitr);knit2pdf('%.Rnw')" | txs:///view-pdf


## Error

which appears in TeXstudio as a really long red text in the Messages section where somewhere near the end of it is something like

Preview: Fontsize 12pt Preview: PDFoutput 1 ! Package tikz Error: + or - expected.


(I don't know how to characterize it better)

## Solution

You can use user-defined command (in TeXstudio: Options -> Configure TeXstudio -> Build -> User Commands -> Add):

"C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" -e "library(knitr);library(tikzDevice);tikzOld=options('tikzMetricPackages');tikzNew=c('\\catcode\'=13','\\catcode-=13','\\usepackage[T1]{fontenc}','\\usetikzlibrary{calc}');options(tikzMetricPackages=tikzNew);knit2pdf('%.Rnw');options(tikzMetricPackages=tikzOld)" | txs:///view-pdf


This command correctly compiles the file and displays it with TeXstudio's internal PDF viewer (then you can run it with [ALT] + [SHIFT] + [F1] shortcut if it's your first user-defined command).

You won't probably run in this problem not using all of the above at the same time.

For example if you don't need awesome features of tikzDevice when using czech or slovak babel while using „“ in your document you can likely use dev='pdf' etc.

• That's a superb summary! Thank you in the name of future generations. :-) Mar 17 '14 at 20:42

About the second option. There is need to fix inside parenthesis from " to \". Please try:

"C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" -e "library(knitr);library(tikzDevice);tikzOld=options(\"tikzMetricPackages\");tikzNew=c(\"\\catcode\\\"=13\",\"\\catcode-=13\",\"\\usepackage[T1]{fontenc}\",\"\\usetikzlibrary{calc}\");options(tikzMetricPackages=tikzNew);knit(\"er0.Rnw\");options(tikzMetricPackages=tikzOld)"


I am not sure right now if it is related or not, but I had a problem with setting paths on Windows XP because of tildes in directory names (it was working well on Linux). Therefore, I am using external=FALSE in the chunk and I created .Renviron file in the working directory with one line: TMP='C:\striz\...\my-working-directory\figure'. After that Knitr+tikzDevice were able to find generated files in subdirectories.

• Tried the \" which failed, but replacing " with ' worked so the second approach works for me. Final TeXstudio command is: "C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" -e "library(knitr);library(tikzDevice);tikzOld=options('tikzMetricPackages');tikzNew=c('\\catcode\'=13','\\catcode-=13','\\usepackage[T1]{fontenc}','\\usetikzlibrary{calc}');options(tikzMetricPackages=tikzNew);knit('%.Rnw');options(tikzMetricPackages=tikzOld)" However I would like to compile it right to .pdf with one button so I will try to improve the command and eventually post the final solution.
– VaNa
Mar 17 '14 at 9:44
• Congratulations, dear VaNa! \" was working for me, it's good to know about another successful way. Now, it's the right time to try to change knit('%.Rnw') to knit2pdf('%.Rnw'). In case it is not working I would try to add & pdflatex %.tex at the end of your posted TeXstudio command. Mar 17 '14 at 12:34
• knit('%.Rnw') to knit2pdf('%.Rnw') transition worked! The final command looks like: "C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" -e "library(knitr);library(tikzDevice);tikzOld=options('tikzMetricPackages');tikzNew=c('\\catcode\'=13','\\catcode-=13','\\usepackage[T1]{fontenc}','\\usetikzlibrary{calc}');options(tikzMetricPackages=tikzNew);knit2pdf('%.Rnw');options(tikzMetricPackages=tikzOld)" | txs:///view-pdf with | txs:///view-pdf added to run the internal TeXstudio PDF viewer. I have also written a summary (below) since reading this could be chaotic to someone else. Thanks!
– VaNa
Mar 17 '14 at 20:23