Skip to content

Commit

Permalink
close #1679: obtain the caption from the chunk option tab.cap for `…
Browse files Browse the repository at this point in the history
…kable()`
  • Loading branch information
yihui committed Nov 10, 2023
1 parent 0d1ad21 commit 638d3ec
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 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.46

## NEW FEATURES

- Added a new chunk option `tab.cap` to specify the table caption for `kable()` (thanks, @ulyngs, #1679). Previously, the caption could only be specified via the `caption` argument of `kable()`. Now you can set it in the chunk header if you want. Please note that this chunk option only works with a single `kable()` in each code chunk, and its value must be of length 1.

## BUG FIXES

- `write_bib()` generated empty entries for packages without URLs (thanks, @bastistician, #2304).
Expand Down
2 changes: 1 addition & 1 deletion R/defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ opts_chunk_attr = local({
opts$fig.id = 'logical'
opts[c(
'opts.label', 'resize.width', 'resize.height', 'fig.alt', 'fig.link', 'fig.sep',
'ffmpeg.bitrate', 'ffmpeg.format'
'tab.cap', 'ffmpeg.bitrate', 'ffmpeg.format'
)] = 'character'
opts$lang = 'list'
opts$R.options = 'list'
Expand Down
10 changes: 6 additions & 4 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
#' are left-aligned. If \code{length(align) == 1L}, the string will be
#' expanded to a vector of individual letters, e.g. \code{'clc'} becomes
#' \code{c('c', 'l', 'c')}, unless the output format is LaTeX.
#' @param caption The table caption.
#' @param caption The table caption. By default, it is retrieved from the chunk
#' option \code{tab.cap}.
#' @param label The table reference label. By default, the label is obtained
#' from \code{knitr::\link{opts_current}$get('label')}. To disable the label,
#' use \code{label = NA}.
#' from \code{knitr::\link{opts_current}$get('label')} (i.e., the current
#' chunk label). To disable the label, use \code{label = NA}.
#' @param format.args A list of arguments to be passed to \code{\link{format}()}
#' to format table values, e.g. \code{list(big.mark = ',')}.
#' @param escape Boolean; whether to escape special characters when producing
Expand Down Expand Up @@ -100,7 +101,8 @@
#' kables(list(kable(d1, align = 'l'), kable(d2)), caption = 'A tale of two tables')
kable = function(
x, format, digits = getOption('digits'), row.names = NA, col.names = NA,
align, caption = NULL, label = NULL, format.args = list(), escape = TRUE, ...
align, caption = opts_current$get('tab.cap'), label = NULL, format.args = list(),
escape = TRUE, ...
) {

format = kable_format(format)
Expand Down
9 changes: 5 additions & 4 deletions man/kable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 638d3ec

Please sign in to comment.