yihui / knitr

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

fig.ncol not working with Latex #2336

Open nassuphis opened 2 months ago

nassuphis commented 2 months ago

this should produce 2 figures per column:

\documentclass{article}
\begin{document}
<<,fig.show = "hold",out.width = "2cm",fig.ncol=2>>=
plot(1:100); plot(100:1); plot(1:100); plot(100:1); plot(1:100) 
@
\end{document}

it used to work.

now it produces this:

image

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.

nassuphis commented 2 months ago
> xfun::session_info()
R version 4.3.3 (2024-02-29)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.1, RStudio 2023.12.1.402

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  callr_3.7.6       cli_3.6.2         compiler_4.3.3    curl_5.2.1        desc_1.4.3        evaluate_0.23     graphics_4.3.3   
  grDevices_4.3.3   highr_0.10        knitr_1.46.3      methods_4.3.3     pkgbuild_1.4.4    processx_3.8.4    ps_1.7.6         
  R6_2.5.1          remotes_2.5.0     rstudioapi_0.16.0 stats_4.3.3       tools_4.3.3       utils_4.3.3       xfun_0.43        
  yaml_2.3.8       
> 
cderv commented 2 months ago

fig.ncol is used for sufigure and so you need to use fig.cap and fig.subcap for it to be used, also adding usage for the subfig package.

We document this in https://bookdown.org/yihui/rmarkdown-cookbook/latex-subfigure.html

fig.ncol is not used otherwise.

Example:

\documentclass{article}
\usepackage{subfig}
\begin{document}
<<out.width = "50%",fig.ncol=2, fig.subcap=letters[1:5], fig.cap="test">>=
plot(1:100); plot(100:1); plot(1:100); plot(100:1); plot(1:100)
@
\end{document}

Codebase for this in https://github.com/yihui/knitr/blob/44a7bee566afff3eb4c8b13fd6fa64487e12981f/R/hooks-latex.R

Hope this helps