yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

Issue with code highlighting (RStudio/Sweave/knitr/tinytex) #2345

Closed WilliamCipolli closed 1 month ago

WilliamCipolli commented 1 month ago

I have a new machine and I completed the following:

  1. downloaded and installed R and RStudio
  2. Installed the knitr and tinytex packages
  3. Installed tinytex using install_tinytex()
  4. Adjusted global options to use knitr and tinytex
  5. Attempted to compile this test document:
\documentclass{article}

\begin{document}
Hello World!

<<>>=
# Hello World!
set.seed(1313)
x <- sample(x = 1:100, 
            size = 1)
(result <- x * 5)
@
When we multiply \Sexpr{x} by 5, we get \Sexpr{result}. %\Sexpr to call objects from R
\end{document}

I get an error in the log-file:

! Undefined control sequence.
l.60 \hlkwd{set.seed}\hldef
                           {(}\hlnum{1313}\hldef{)} 

The issue is patched and the file will compile if I add the following to the preamble.

\newcommand{\hldef}[1]{\texttt{#1}}

Session Info for knitr

R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22621)

Locale:
  LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
  LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
  LC_TIME=English_United States.utf8    

Package version:
  evaluate_0.23   graphics_4.4.0  grDevices_4.4.0 highr_0.11      knitr_1.46     
  methods_4.4.0   stats_4.4.0     tools_4.4.0     utils_4.4.0     xfun_0.44      
  yaml_2.3.8 

And for tinytex:

R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22621)

Locale:
  LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
  LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
  LC_TIME=English_United States.utf8    

Package version:
  graphics_4.4.0  grDevices_4.4.0 stats_4.4.0     tinytex_0.51    tools_4.4.0     utils_4.4.0    
  xfun_0.44      

LaTeX version used: 
  TeX Live 2024 (TinyTeX) with tlmgr 2024-04-26

When I use the development version, I get an error when compiling: "Unabled to find specified LaTeX program 'pdfLaTeX' on the system path." This goes away when I switch back to the current version. Note that I attempted to use development versions for knitr and tinytex. I don't think it would be tinytex, but just in case I tried using MiKTeX instead and I have the same issue.

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

yihui commented 1 month ago

Thanks for the report. This has already been fixed last week in the current dev version of knitr, which the issue guide (at the bottom of your post) asked you to try. I have also submitted the new version of knitr to CRAN, but it has been pending CRAN's review for a while.

WilliamCipolli commented 1 month ago

Thanks for the quick response. I did try the development version, but I got a different error when trying to compile: "Unabled to find specified LaTeX program 'pdfLaTeX' on the system path."

yihui commented 1 month ago

I can't think of a possible reason why the dev version of knitr could possibly make RStudio fail to find pdflatex. I guess this particular issue is irrelevant to knitr. If you install or reinstall TinyTeX on Windows, you'd better quit and restart RStudio after installation.

yihui commented 1 month ago

The new version of knitr is on CRAN now.

WilliamCipolli commented 1 month ago

I'm still seeing issues but I'm wondering if it's a RStudio issue?

Here is a video of what I'm seeing. It doesn't go away if I switch to Sweave instead of knit.

R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22621), RStudio 2024.4.1.748

Locale:
  LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
  LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
  LC_TIME=English_United States.utf8    

Package version:
  evaluate_0.23   graphics_4.4.0  grDevices_4.4.0 highr_0.11      knitr_1.47.2    methods_4.4.0  
  stats_4.4.0     tools_4.4.0     utils_4.4.0     xfun_0.44       yaml_2.3.8  

Any ideas here?

WilliamCipolli commented 1 month ago

It appears to be an Rstudio issue. I've reverted to version 2023.12.1 and the issue no longer exists.

yihui commented 1 month ago

Yes, I think it's an RStudio issue.