From 21fdbab5144bba35d2a0378aa34553a02b9bd642 Mon Sep 17 00:00:00 2001 From: ThierryO Date: Thu, 20 Jul 2017 17:15:14 +0200 Subject: [PATCH 1/6] replicate bug in unit test --- tests/testit/test-table.R | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/testit/test-table.R b/tests/testit/test-table.R index 7c598cd602..f02f3e686f 100644 --- a/tests/testit/test-table.R +++ b/tests/testit/test-table.R @@ -53,9 +53,12 @@ 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 @@ -63,16 +66,20 @@ x & col\\_name\\\\ \\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 @@ -80,7 +87,8 @@ x & col_name\\\\ \\hline 5% & 40_6\\\\ \\hline -\\end{tabular}' +\\end{tabular} +\\end{table}' ) ) From 4fa57de32d28eb160086d432625565eeef24a70d Mon Sep 17 00:00:00 2001 From: ThierryO Date: Thu, 20 Jul 2017 17:15:43 +0200 Subject: [PATCH 2/6] escape latex caption --- R/table.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/table.R b/R/table.R index fc990d8504..1c475a59c0 100644 --- a/R/table.R +++ b/R/table.R @@ -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 From 8af9fca4b5117e2a3f8dc6f5304d22378f38a0b8 Mon Sep 17 00:00:00 2001 From: ThierryO Date: Thu, 20 Jul 2017 17:22:15 +0200 Subject: [PATCH 3/6] add contributor --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 968af30bb8..c35ed9fd5a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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"), From 96af5eded90e52054b691937d4d3ba447d05a44f Mon Sep 17 00:00:00 2001 From: ThierryO Date: Thu, 20 Jul 2017 17:30:05 +0200 Subject: [PATCH 4/6] escape figure captions in latex --- R/hooks-latex.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/hooks-latex.R b/R/hooks-latex.R index de1623d65b..d5729dbbca 100644 --- a/R/hooks-latex.R +++ b/R/hooks-latex.R @@ -90,8 +90,8 @@ 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 + cap = escape_latex(options$fig.cap) + scap = escape_latex(options$fig.scap) fig1 = fig2 = '' mcap = fig.num > 1L && options$fig.show == 'asis' && !length(subcap) # initialize subfloat strings From 46f9c9f53db402e4a78181611a8c61f0c124f061 Mon Sep 17 00:00:00 2001 From: ThierryO Date: Mon, 24 Jul 2017 10:32:35 +0200 Subject: [PATCH 5/6] try to make escaping latex figure captions conditional on latex format --- R/hooks-latex.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/R/hooks-latex.R b/R/hooks-latex.R index d5729dbbca..9a2e9c9240 100644 --- a/R/hooks-latex.R +++ b/R/hooks-latex.R @@ -90,8 +90,13 @@ 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 = escape_latex(options$fig.cap) - scap = escape_latex(options$fig.scap) + if (out_format('latex')) { + cap = escape_latex(options$fig.cap) + 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 From bba3a74cbbefe1d4ca5902e477bf1ea532f9c0e1 Mon Sep 17 00:00:00 2001 From: ThierryO Date: Mon, 24 Jul 2017 11:09:42 +0200 Subject: [PATCH 6/6] only use escape_latex() on when fig.cap and fig.scap when they are not NULL --- R/hooks-latex.R | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/R/hooks-latex.R b/R/hooks-latex.R index 9a2e9c9240..7a4c8d207b 100644 --- a/R/hooks-latex.R +++ b/R/hooks-latex.R @@ -91,8 +91,16 @@ hook_plot_tex = function(x, options) { # figure environment: caption, short caption, label if (out_format('latex')) { - cap = escape_latex(options$fig.cap) - scap = escape_latex(options$fig.scap) + 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