daqana / tikzDevice

A R package for producing graphics output as PGF/TikZ code for use in TeX documents.
https://daqana.github.io/tikzDevice
132 stars 26 forks source link

knitr + tikz fails if \input is used in Rnw #208

Open sgiannerini opened 4 years ago

sgiannerini commented 4 years ago

Dear all,

after updating R, knitr and tikzDevice, a Rnw file that used to compile flawlessly failed to compile and it took me hours to debug. In brief, if \input or \include is used, the Rnw does not compile, even if the included file is empty. See the following MWE.

    \documentclass{article}
    \usepackage{graphicx}
    \input{include}

    \begin{document}
    <<c0,echo=FALSE,message=FALSE,warning=FALSE,cache=FALSE>>=
        opts_chunk$set(dev='tikz', fig.path='fig/')
    @
    \section{Section}
    \begin{center}
     <<c1>>=
        hist(rnorm(100))
     @
    \end{center}
    \end{document}

This is the error I get:

    Error 1 Chunk c1:
      Error in getMetricsFromLatex(TeXMetrics, verbose = verbose): 
    TeX was unable to calculate metrics for:

     \bfseries{}Histogram of rnorm(100)

    Run the following commands for diagnosis:

     tikzTest()
     tikzTest("\\bfseries{}Histogram of rnorm(100)")

    Common reasons for failure include:
      * The string contains a character which is special to LaTeX unless
        escaped properly, such as % or $.
      * The string makes use of LaTeX commands provided by a package and
        the tikzDevice was not told to load the package.

    The TeX and log files used for the calculation can help diagnose the
    problem. If these files are missing, rerun the plot and make sure to
    keep the R session open.
    TeX file: tikzStringWidthCalc.tex
    Log file: tikzStringWidthCalc.log

But the file compiles if I comment out the \input{include} command and tikz has no problems. The file include.tex can be also empty, it does not matter what you put in it.

Even the error can be workarounded I thought it appropriate to report here even if I am not sure it is a tikzDevice problem, my apologies in case.

The above behaviour has been reprocuded on 3 different machines (2 Win and 1 OpenSuse), here are the relevant sessionInfo for two of them (the third one is identical to the first)

`> sessionInfo() R version 4.0.2 (2020-06-22) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale: [1] LC_COLLATE=Italian_Italy.1252 LC_CTYPE=Italian_Italy.1252 LC_MONETARY=Italian_Italy.1252 [4] LC_NUMERIC=C LC_TIME=Italian_Italy.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] tikzDevice_0.12.3.1 knitr_1.29

loaded via a namespace (and not attached): [1] compiler_4.0.2 magrittr_1.5 tools_4.0.2 tinytex_0.25 filehash_2.4-2 stringi_1.4.6 [7] highr_0.8 grid_4.0.2 stringr_1.4.0 xfun_0.16 evaluate_0.14 `

`R version 3.6.1 Patched (2019-10-25 r77334) Platform: x86_64-pc-linux-gnu (64-bit) Running under: openSUSE Leap 15.1

Matrix products: default BLAS/LAPACK: /opt/intel/compilers_and_libraries_2019.5.281/linux/mkl/lib/intel64_lin/libmkl_gf_lp64.so

locale: [1] LC_CTYPE=it_IT.UTF-8 LC_NUMERIC=C [3] LC_TIME=it_IT.UTF-8 LC_COLLATE=it_IT.UTF-8 [5] LC_MONETARY=it_IT.UTF-8 LC_MESSAGES=it_IT.UTF-8 [7] LC_PAPER=it_IT.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=it_IT.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] knitr_1.29

loaded via a namespace (and not attached): [1] compiler_3.6.1 magrittr_1.5 tools_3.6.1 [4] tinytex_0.25 tikzDevice_0.12.3.1 filehash_2.4-2 [7] stringi_1.4.6 highr_0.8 grid_3.6.1 [10] stringr_1.4.0 xfun_0.16 evaluate_0.14 `

Thank you,

Simone

sgiannerini commented 4 years ago

UPDATE: By reverting to tikzDevice 0.12 (2018-06-28) the problem disappears

rstub commented 3 years ago

this seems to be very similar to #198 which was fixed in #203. Could you give the current development version via remotes::install_github("daqana/tikzDevice") a try?

sgiannerini commented 3 years ago

Dear Ralph,

it appears that with the following setup the problem is no longer present:

`>sessionInfo()

R version 4.0.3 Patched (2020-10-12 r79333) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale: [1] LC_COLLATE=Italian_Italy.1252 LC_CTYPE=Italian_Italy.1252
[3] LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C
[5] LC_TIME=Italian_Italy.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] knitr_1.30

loaded via a namespace (and not attached): [1] compiler_4.0.3 magrittr_2.0.1 tools_4.0.3 tinytex_0.28
[5] tikzDevice_0.12.3.1 filehash_2.4-2 stringi_1.5.3 highr_0.8
[9] grid_4.0.3 stringr_1.4.0 xfun_0.19 evaluate_0.14 ` In any case I also tried the development version as you indicated and the file also compiles correctly.

`> sessionInfo() R version 4.0.3 Patched (2020-10-12 r79333) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale: [1] LC_COLLATE=Italian_Italy.1252 LC_CTYPE=Italian_Italy.1252
[3] LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C
[5] LC_TIME=Italian_Italy.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] knitr_1.30

loaded via a namespace (and not attached): [1] compiler_4.0.3 magrittr_2.0.1 tools_4.0.3 tinytex_0.28
[5] tikzDevice_0.12.3.2 filehash_2.4-2 stringi_1.5.3 highr_0.8
[9] grid_4.0.3 stringr_1.4.0 xfun_0.19 evaluate_0.14 `

And this leaves me with no clue but at least the problem is no longer present. Thank you for your investigation.

Kind regards

Simone