Skip to content

Commit

Permalink
revert 4307aed and fix #2302 by escaping % instead
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Oct 26, 2023
1 parent 4307aed commit 09db6b2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.44.3
Version: 1.44.4
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down
12 changes: 6 additions & 6 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- Improved the error message to contain more specific information when YAML chunk options could not be parsed (thanks, @pedropark99, #2294).

## MAJOR CHANGES

- The object `opts_current` will be restored after each code chunk has finished executing. Previously, it would not be restored, which means even for inline R expressions, `opts_current$get()` will inherit chunk options from a previous code chunk (thanks, @rundel, #1988). Besides, `opts_current$get('label')` will return a unique label for inline expressions. One possible application is to construct unique figure paths via `fig_path()` (e.g., ropensci/magick#310).

## BUG FIXES

- Special characters in the chunk option `fig.alt` are properly escaped now (thanks, @jay-sf, #2290).
Expand All @@ -14,16 +18,12 @@

- Add the necessary `\newline` to the last subfigure (thanks, @slrellison, rstudio/rmarkdown#2518).

- Percent signs (`%`) in LaTeX figure captions and short captions are properly escaped now (thanks, @s-u, #2302).

## MAJOR CHANGES

- `opts_current$set()` without `opts_current$lock(FALSE)` will trigger a warning instead of an error for now and it will become an error in future (#2296).

- The object `opts_current` will be restored after each code chunk has finished executing. Previously, it would not be restored, which means even for inline R expressions, `opts_current$get()` will inherit chunk options from a previous code chunk (thanks, @rundel, #1988). Besides, `opts_current$get('label')` will return a unique label for inline expressions. One possible application is to construct unique figure paths via `fig_path()` (e.g., ropensci/magick#310).

## MINOR CHANGES

- For R Markdown documents, figure output is now wrapped in raw `latex` blocks when the output is LaTeX code (thanks, @s-u, #2302).

# CHANGES IN knitr VERSION 1.44

## NEW FEATURES
Expand Down
5 changes: 4 additions & 1 deletion R/hooks-latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ hook_plot_tex = function(x, options) {
}
scap = if (is.null(scap) || is.na(scap)) '' else sprintf('[%s]', scap)
cap = if (cap == '') '' else sprintf(
'\\caption%s{%s}%s\n', scap, cap,
'\\caption%s{%s}%s\n', escape_percent(scap), escape_percent(cap),
create_label(lab, if (mcap) c('-', fig.cur), latex = TRUE)
)
fig2 = sprintf('%s\\end{%s}\n', cap, options$fig.env)
Expand Down Expand Up @@ -192,6 +192,9 @@ hook_plot_tex = function(x, options) {
)
}

# % -> \%, but do not touch \%
escape_percent = function(x) gsub('(?<!\\\\)%', '\\\\%', x, perl = TRUE)

.chunk.hook.tex = function(x, options) {
ai = output_asis(x, options)
col = if (!ai) paste0(
Expand Down
4 changes: 2 additions & 2 deletions R/hooks-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ hook_plot_md = function(x, options) {
if (is.null(to <- pandoc_to()) || is_html_output(to))
return(hook_plot_md_base(x, options))
if ((options$fig.show == 'animate' || is_tikz_dev(options)) && is_latex_output())
return(raw_latex(hook_plot_tex(x, options)))
return(hook_plot_tex(x, options))
office_output = to %in% c('docx', 'pptx', 'rtf', 'odt')
if (need_special_plot_hook(options)) {
if (is_latex_output()) {
# Pandoc < 1.13 does not support \caption[]{} so suppress short caption
if (is.null(options$fig.scap)) options$fig.scap = NA
return(raw_latex(hook_plot_tex(x, options)))
return(hook_plot_tex(x, options))
}
if (office_output) {
if (options$fig.align != 'default') {
Expand Down

0 comments on commit 09db6b2

Please sign in to comment.