Skip to content

Commit

Permalink
close #2116: new chunk option fig.subsep to add separators to subfl…
Browse files Browse the repository at this point in the history
…oat environment (#2140)

Co-authored-by: Yihui Xie <[email protected]>
  • Loading branch information
pedropark99 and yihui authored Jun 29, 2022
1 parent c874c17 commit 09ab146
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
28 changes: 28 additions & 0 deletions R/hooks-latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 09ab146

Please sign in to comment.