Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support alt text for images in LaTeX output #2378

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- In-chunk references of the form `<<label>>` can be disabled via the chunk option `ref.chunk = FALSE` now (thanks, @jennybc @gadenbuie, #2360).

- Added support for `fig.alt` for LaTeX output, i.e., using `\includegraphics[alt={alt text}]` (thanks, @capnrefsmmat, #2378).

## BUG FIXES

- In-chunk references of the form `<<label>>` should not be resolved if `label` is not found in the document (thanks, @jennybc @gadenbuie, #2360).
Expand Down
7 changes: 4 additions & 3 deletions R/hooks-latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ hook_plot_tex = function(x, options) {
# maxwidth does not work with animations
if (animate && identical(ow, '\\maxwidth')) ow = NULL
if (is.numeric(ow)) ow = paste0(ow, 'px')
size = paste(c(sprintf('width=%s', ow),
sprintf('height=%s', options$out.height),
options$out.extra), collapse = ',')
size = paste(c(
sprintf('width=%s', ow), sprintf('height=%s', options$out.height),
sprintf('alt={%s}', escape_percent(options$fig.alt)), options$out.extra
), collapse = ',')

paste0(
fig1, align1, sub1, resize1,
Expand Down
17 changes: 17 additions & 0 deletions tests/testit/test-hooks-latex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library(testit)

assert("alt text is included in LaTeX output", {
# no alt text
(hook_plot_tex('foo.pdf', list(fig.align = 'center', fig.show = 'asis')) %==%
'\n\n{\\centering \\includegraphics{foo} \n\n}\n\n')

# alt text
(hook_plot_tex('foo.pdf', list(fig.alt = 'Alt', fig.align = 'center',
fig.show = 'asis')) %==%
'\n\n{\\centering \\includegraphics[alt={Alt}]{foo} \n\n}\n\n')

# with width
(hook_plot_tex('foo.pdf', list(fig.alt = 'Alt', fig.align = 'center',
fig.show = 'asis', out.width = '\\maxwidth')) %==%
'\n\n{\\centering \\includegraphics[width=\\maxwidth,alt={Alt}]{foo} \n\n}\n\n')
})