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

Sanitise latex captions of kable() and figures #1418

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Authors@R: c(
person("Taiyun", "Wei", role = "ctb"),
person("Thibaut", "Assus", role = "ctb"),
person("Thibaut", "Lamadon", role = "ctb"),
person("Thierry", "Onkelinx", role = "ctb"),
person("Thomas", "Leeper", role = "ctb"),
person("Tom", "Torsney-Weir", role = "ctb"),
person("Trevor", "Davis", role = "ctb"),
Expand Down
17 changes: 15 additions & 2 deletions R/hooks-latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,21 @@ hook_plot_tex = function(x, options) {
switch(a, left = '\\hfill{}\n\n', center = '\n\n}\n\n', right = '\n\n', '')

# figure environment: caption, short caption, label
cap = options$fig.cap
scap = options$fig.scap
if (out_format('latex')) {
if (is.null(options$fig.cap)) {
cap = NULL
} else {
cap = escape_latex(options$fig.cap)
}
if (is.null(options$fig.scap)) {
scap = NULL
} else {
scap = escape_latex(options$fig.scap)
}
} else {
cap = options$fig.cap
scap = options$fig.scap
}
fig1 = fig2 = ''
mcap = fig.num > 1L && options$fig.show == 'asis' && !length(subcap)
# initialize subfloat strings
Expand Down
2 changes: 1 addition & 1 deletion R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ kable = function(
# create a label for bookdown if applicable
if (!is.null(caption) && !is.na(caption)) caption = paste0(
create_label('tab:', opts_current$get('label'), latex = (format == 'latex')),
caption
ifelse(escape && format == 'latex', escape_latex(caption), caption)
)
if (inherits(x, 'list')) {
# if the output is for Pandoc and we want multiple tabular in one table, we
Expand Down
24 changes: 16 additions & 8 deletions tests/testit/test-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,42 @@ x & y\\\\
assert(
'kable() escapes LaTeX special characters by default',
identical(
kable2(data.frame(x = c("10%", "5%"), col_name = c("3_8", "40_6")), 'latex'),
'
\\begin{tabular}{l|l}
kable2(data.frame(x = c("10%", "5%"), col_name = c("3_8", "40_6")), 'latex', caption = "Test % and _"),
'\\begin{table}

\\caption{\\label{tab:}Test \\% and \\_}
\\centering
\\begin{tabular}[t]{l|l}
\\hline
x & col\\_name\\\\
\\hline
10\\% & 3\\_8\\\\
\\hline
5\\% & 40\\_6\\\\
\\hline
\\end{tabular}'
\\end{tabular}
\\end{table}'
)
)

assert(
'kable() doesn\'t escape LaTeX special characters when escape = FALSE',
identical(
kable2(data.frame(x = c("10%", "5%"), col_name = c("3_8", "40_6")), 'latex', escape = FALSE),
'
\\begin{tabular}{l|l}
kable2(data.frame(x = c("10%", "5%"), col_name = c("3_8", "40_6")), 'latex', caption = "Test % and _", escape = FALSE),
'\\begin{table}

\\caption{\\label{tab:}Test % and _}
\\centering
\\begin{tabular}[t]{l|l}
\\hline
x & col_name\\\\
\\hline
10% & 3_8\\\\
\\hline
5% & 40_6\\\\
\\hline
\\end{tabular}'
\\end{tabular}
\\end{table}'
)
)

Expand Down