From fe1bdf63be2f3206f4fff518808d301d73d41413 Mon Sep 17 00:00:00 2001 From: Alex Reinhart Date: Sun, 27 Oct 2024 15:28:58 -0400 Subject: [PATCH 1/4] Support alt text for images in LaTeX output Since 2021, graphicx supports an alt option containing alt text. Pass fig.alt through unless knitr.include_graphics.alt is FALSE (which can be set if e.g. you're using an older TeX Live whose graphicx does not support this.) Ongoing LaTeX work to support accessible (tagged) PDFs will eventually use this option to embed alt text in PDFs. --- R/hooks-latex.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/hooks-latex.R b/R/hooks-latex.R index 4c2964df61..721cc89129 100644 --- a/R/hooks-latex.R +++ b/R/hooks-latex.R @@ -76,6 +76,7 @@ hook_plot_tex = function(x, options) { a = options$fig.align fig.cur = options$fig.cur %n% 1L fig.num = options$fig.num %n% 1L + fig.alt = options$fig.alt animate = options$fig.show == 'animate' fig.ncol = options$fig.ncol %n% fig.num if (is.null(fig.sep <- options$fig.sep)) { @@ -179,6 +180,11 @@ hook_plot_tex = function(x, options) { sprintf('\\animategraphics%s{%s}{%s}{%s}{%s}', size, 1 / options$interval, sub(sprintf('%d$', fig.num), '', sans_ext(x)), 1L, fig.num) } else { + if (!is.null(fig.alt) && getOption('knitr.include_graphics.alt', TRUE)) { + size = paste(c(size, sprintf('alt={%s}', escape_percent(fig.alt))), + collapse = ',') + } + if (nzchar(size)) size = sprintf('[%s]', size) res = sprintf( '\\includegraphics%s{%s} ', size, From db16625785ce89f4ac604ec462a0e84d2774788c Mon Sep 17 00:00:00 2001 From: Alex Reinhart Date: Sun, 27 Oct 2024 19:33:21 -0400 Subject: [PATCH 2/4] Remove global option gating alt text in LaTeX output --- R/hooks-latex.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/hooks-latex.R b/R/hooks-latex.R index 721cc89129..4bf003cad2 100644 --- a/R/hooks-latex.R +++ b/R/hooks-latex.R @@ -180,7 +180,7 @@ hook_plot_tex = function(x, options) { sprintf('\\animategraphics%s{%s}{%s}{%s}{%s}', size, 1 / options$interval, sub(sprintf('%d$', fig.num), '', sans_ext(x)), 1L, fig.num) } else { - if (!is.null(fig.alt) && getOption('knitr.include_graphics.alt', TRUE)) { + if (!is.null(fig.alt)) { size = paste(c(size, sprintf('alt={%s}', escape_percent(fig.alt))), collapse = ',') } From 76b9d79822560606486d599999be302cc8c5730b Mon Sep 17 00:00:00 2001 From: Alex Reinhart Date: Sun, 27 Oct 2024 19:48:35 -0400 Subject: [PATCH 3/4] Add tests for LaTeX alt text --- tests/testit/test-hooks-latex.R | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/testit/test-hooks-latex.R diff --git a/tests/testit/test-hooks-latex.R b/tests/testit/test-hooks-latex.R new file mode 100644 index 0000000000..3a7c0c00ba --- /dev/null +++ b/tests/testit/test-hooks-latex.R @@ -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') +}) From d422e757469dc6586276c2d736c81df01df340bf Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Wed, 30 Oct 2024 13:56:56 -0500 Subject: [PATCH 4/4] tweak PR and add news [ci skip] --- NEWS.md | 2 ++ R/hooks-latex.R | 13 ++++--------- tests/testit/test-hooks-latex.R | 2 +- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/NEWS.md b/NEWS.md index 12ce0ffc21..76b71a64ba 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ - In-chunk references of the form `<