diff --git a/NEWS.md b/NEWS.md index abe810d1ad..c1865e1141 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # CHANGES IN knitr VERSION 1.40 +## NEW FEATURES + +- Per suggestion of @jakubkaczor, a new chunk option called `fig.subsep` was added (thanks, @pedropark99, #2140). This option is focused on improving LaTeX sub-figures arrangement (see LaTeX [sub-figures section in _R Markdown Cookbook_](https://bookdown.org/yihui/rmarkdown-cookbook/latex-subfigure.html)). This option is available only for PDF output and will be ignored in other output formats. With this option, you can add LaTeX commands between (and around) sub-figures. + ## MINOR CHANGES - When the inline R code cannot be correctly parsed, the error message will show the original code in addition to the parsing error, which can make it easier to identify the code error in the source document (thanks, @AlbertLei, #2141). diff --git a/R/hooks-latex.R b/R/hooks-latex.R index eb8a728f06..5b7168ba22 100644 --- a/R/hooks-latex.R +++ b/R/hooks-latex.R @@ -156,6 +156,34 @@ hook_plot_tex = function(x, options) { sprintf('height=%s', options$out.height), options$out.extra), collapse = ',') + # If the chunk have sub-figures, check if we need to add a subfloat separator + # between each sub-figure. + if (usesub && !is.null(subsep <- options$fig.subsep)) { + # User can provide a single separator, or, a vector of multiple separators. + # Number of elements in this vector can vary from `fig.num - 1` to `fig.num + 1`. + n_subsep = length(subsep) + # If the length of `fig.subsep` does not comply with these boundaries, stop the user. + if (!n_subsep %in% (c(1L, -1:1 + fig.num))) stop2( + "'fig.subsep' should be a single character value, or a character vector ", + "with number of elements ranging from ", fig.num - 1, " to ", fig.num + 1, + ". But currently 'fig.subsep' has ", n_subsep, " elements.`" + ) + sub11 = sub21 = '' # strings from subsep to prepend sub1, or append sub2 + # If `fig.subsep` is a single separator, add it before all plots except first + if (n_subsep == 1L) { + if (!plot1) sub11 = subsep + } else if (n_subsep < fig.num) { + # If `fig.num - 1` separators, add i-th separator before (i+1)-th plot: + if (!plot1) sub11 = subsep[fig.cur - 1] + } else { + sub11 = subsep[fig.cur] + # If is the last plot in set, add the last separator after it. + if (n_subsep > fig.num && plot2) sub21 = subsep[fig.cur + 1L] + } + if (sub11 != '') sub1 = paste(sub11, sub1, sep = '\n') + if (sub21 != '') sub2 = paste(sub2, sub21, sep = '\n') + } + paste0( fig1, align1, sub1, resize1, if (tikz) {