knitr icon indicating copy to clipboard operation
knitr copied to clipboard

remove new line before table with kable for format LaTeX

Open srnnkls opened this issue 5 years ago • 1 comments

When calling cable with format = latex the actual latex output is preceded by a newline. I can write a wrapper that strips it but was curious: Is this intentional?

library(knitr)
a <- c(1, 2, 3) 
b <- kable(a, format = "latex")

b[1]
#> [1] "\n\\begin{tabular}{r}\n\\hline\nx\\\\\n\\hline\n1\\\\\n\\hline\n2\\\\\n\\hline\n3\\\\\n\\hline\n\\end{tabular}"

By filing an issue to this repo, I promise that

  • [x] I have fully read the issue guide at https://yihui.name/issue/.
  • [x] I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • [x] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

srnnkls avatar Oct 24 '19 21:10 srnnkls

digging a bit doing source code archeology, I found that this newline was added in this commit https://github.com/yihui/knitr/commit/3dadff208a4c3c746a0bf5509cd5642bfbd7b022 7 years ago with message

add an empty line before the table; this is to make sure hook_movecode{} works well

and this was before adding some new lines above it in the longtable environment.

I guess only a few tests can tell us if this is still an issue or not...

Hope it helps decide if this \n is needed or not.

cderv avatar Feb 25 '20 21:02 cderv