Skip to content

Commit b2ce792

Browse files
authored
for Rnw documents, suppress the extra paragraph break at the end of the chunk (#1958)
1 parent 074e0ba commit b2ce792

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
- For Rnw documents, the commented `%\begin{document}` will no longer cause trouble (thanks, @NewbieKnitter @shrektan, #1819).
1818

19+
## MINOR CHANGES
20+
21+
- For Rnw documents, if a chunk's output ends with `\n`, **knitr** will no longer add another `\n` to it (thanks, @krivit #1958, @jpritikin 1092).
22+
1923
# CHANGES IN knitr VERSION 1.31
2024

2125
## NEW FEATURES

R/hooks-latex.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,13 @@ hook_plot_tex = function(x, options) {
191191
k2 = '\\end{kframe}'
192192
x = .rm.empty.envir(paste0(k1, x, k2))
193193
size = if (options$size == 'normalsize') '' else sprintf('\\%s', options$size)
194-
if (!ai) x = sprintf('\\begin{knitrout}%s\n%s\n\\end{knitrout}', size, x)
194+
if (!ai) {
195+
# if the chunk content starts with \n, don't add \n; similarly, if it ends with \n, don't append \n
196+
n1 = n2 = '\n'
197+
if (grepl('^\\s*\n', x)) n1 = ''
198+
if (grepl('\n\\s*$', x)) n2 = ''
199+
x = sprintf('\\begin{knitrout}%s%s%s%s\\end{knitrout}', size, n1, x, n2)
200+
}
195201
if (options$split) {
196202
name = fig_path('.tex', options, NULL)
197203
if (!file.exists(dirname(name)))

0 commit comments

Comments
 (0)