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

Using "longtable" causes some source code to become available in PDF-file. #715

Closed
piiskop opened this issue Jul 3, 2022 · 22 comments · Fixed by #742
Closed

Using "longtable" causes some source code to become available in PDF-file. #715

piiskop opened this issue Jul 3, 2022 · 22 comments · Fixed by #742

Comments

@piiskop
Copy link

piiskop commented Jul 3, 2022

Describe the bug
Everywhere i use "longtable", i see some trace of source code after the table like

\end{ThreePartTable}

or

& 1555073.7456 & 7.12127* \end{longtable}

or

\end{landscape}

.

To Reproduce
My function for printing a table looks like this:

# code to reproduce the bug.
library(librarian)

print_table <- function(data_frame, caption, digits = 2, additional_header = "") {

  if (!is.null(knitr::opts_knit$get('rmarkdown.pandoc.to')) && (knitr::opts_knit$get('rmarkdown.pandoc.to') == "docx")) {
    librarian::shelf("flextable")
    table <- (flextable(data_frame))
  } else {
    librarian::shelf("kableExtra")

    table <- kable(data_frame, booktabs = TRUE, escape = FALSE, caption = caption, digits = digits, longtable = TRUE)
    
    if (is.data.frame(additional_header) || is.vector(additional_header)) {
      table <- add_header_above(kable_input = table, header = additional_header, escape = FALSE)
    }
    
    
    table <- (kable_styling(
      table,
      bootstrap_options = c("striped", "bordered", "condensed", "responsive", "hover"),
      latex_options = c("striped", "repeat_header"),
      repeat_header_text = paste(caption, "$\\textit{(Jätkub...)}$"),
      repeat_header_method = "replace",
      repeat_header_continued = "Tabel järgneb järgmisel leheküljel..."
    ))
    
  } 

  return(table)
}

@montesmariana
Copy link

I was trying out on some small code and the problem seemed to be with repeat_header_continued. Could you try setting it to FALSE and see if you still have the problem?

@piiskop
Copy link
Author

piiskop commented Jul 28, 2022

Yes, the problem is really with repeat_header_continued and with the value FALSE, the anomaly doesn't appear. However, this isn't a solution to turn that important part off as i want to tell the reader that the table continues on the next page.

@rafavdz
Copy link

rafavdz commented Aug 5, 2022

Have you found any possible solution? I just noticed that the error disappears when I knit the document in Mac. Any ideas?

@piiskop
Copy link
Author

piiskop commented Aug 6, 2022

No, i haven't. Mac is no solution for me as it's not FOSS.

@al-obrien
Copy link

I have seen the same thing...

I didnt see the issue on RStudio Workbench using Pandoc 2.14 but did see it when deployed to RStudio Connect, which was using Pandoc 2.16.

Not sure if the Pandoc version may have some kind of impact on this, but thats the only difference I could see!

@piiskop
Copy link
Author

piiskop commented Dec 16, 2022

my version of Pandoc is 2.5.

@phargarten2
Copy link

phargarten2 commented Feb 15, 2023

I have gotten the same problem as well. It doesn't seem to be pandoc, but the issue with repeat_header_continued, as stated above.

Pandoc 2.18
Windows 10 Enterprise

@aschersleben
Copy link
Contributor

aschersleben commented Feb 20, 2023

@phargarten2 Use devtools::install_github("aschersleben/kableExtra") to install the abovementioned pull request (that fixes this problem) as long as the pull request is not merged with this repository.


Disclaimer (2023-02-21): Please note, that this will not only install a fixed version of `kableExtra` regarding this issue, but also the full current development version of this package, so that unintended side effects might occur.

@phargarten2
Copy link

phargarten2 commented Feb 20, 2023

@aschersleben Thank you! Unfortunately, the r code didn't work for me. I got an error similar to what others have found in [the remotes package]: (r-lib/remotes#130)

@aschersleben
Copy link
Contributor

@phargarten2 Sorry to hear that, but I cannot reproduce this - works fine for me on Windows 10 and Ubuntu 20.04.4 LTS with the latest version of remotes.
Maybe this works for you, as the problem with remotes seems to be related to the downloading process: Download zip file (here) and install it manually via devtools::install_local("path/to/zip/file")
Good luck!

@piiskop
Copy link
Author

piiskop commented Feb 21, 2023

After using your pull request, nothing compiles anymore:

Error in (knit_hooks$get("source"))(src, options) :
object 'v_spaces' not found
Calls: ... sew.list -> lapply -> FUN -> sew.source ->
Execution halted

Exited with status 1.

How can i compile my documents now? Please fix ASAP.

@aschersleben
Copy link
Contributor

@piiskop You can just re-install the CRAN version of the kableExtra package via install.packages("kableExtra").

@piiskop
Copy link
Author

piiskop commented Feb 21, 2023

It did not help. Installing your version offered me to upgrade about 20 packages which i did and now, compiling does not work anymore unless i turn off the part that turns on line numbering for any output in code blocks.

On the positive side, the trace of the source code is not displayed anymore.

@aschersleben
Copy link
Contributor

@piiskop Sorry to hear that. But it seems that the error would have occured anyway if you updated your R packages and it is not related to this package.

@m3hdad
Copy link

m3hdad commented Mar 24, 2023

Great, post-processing tex file for now and waiting for the next stable release, cheers.

@m3hdad
Copy link

m3hdad commented Mar 24, 2023

It did not help. Installing your version offered me to upgrade about 20 packages which i did and now, compiling does not work anymore unless i turn off the part that turns on line numbering for any output in code blocks.

On the positive side, the trace of the source code is not displayed anymore.

just a recommendation, containerize your work. It has a learning curve but as you move to bigger projects it will save you.

@haozhu233
Copy link
Owner

This bugs locates somewhere in rmarkdown. The generated tex code is correct. I can render it on overleaf without any issue. You can check the tex code by simply cast the output of print_table.

Also here is a reproduction of the same error without using kableextra. If you check the tex file, you will see an extra line of \& 21.4 \& 6 \& 258.0 \& 110 \& 3.08 \& 3.21 \& 19.44 \& 1 \& 0 \& 3 \& 1\textbackslash* \textbackslash end\{longtable\}

---
title: "Untitled"
output: 
  pdf_document:
    keep_tex: yes
header-includes:
  - \usepackage{booktabs}
  - \usepackage{longtable}
  - \usepackage{xcolor}
  - \usepackage{colortbl}
date: "2023-03-24"
---

\begin{longtable}[t]{lrrrrrrrrrrr}
\caption{sss}\\
\toprule
\multicolumn{1}{c}{} \\

  & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
\midrule
\endfirsthead
\caption[]{sss $\textit{(Jätkub...)}$}\\
\toprule
\multicolumn{1}{c}{} \\

  & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
\midrule
\endhead
\midrule
\multicolumn{12}{r@{}}{Tabel järgneb järgmisel leheküljel...}\
\endfoot
\bottomrule
\endlastfoot
\cellcolor{gray!6}{Mazda RX4} & \cellcolor{gray!6}{21.0} & \cellcolor{gray!6}{6} & \cellcolor{gray!6}{160.0} & \cellcolor{gray!6}{110} & \cellcolor{gray!6}{3.90} & \cellcolor{gray!6}{2.62} & \cellcolor{gray!6}{16.46} & \cellcolor{gray!6}{0} & \cellcolor{gray!6}{1} & \cellcolor{gray!6}{4} & \cellcolor{gray!6}{4}\\
Mazda RX4 Wag & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.88 & 17.02 & 0 & 1 & 4 & 4\\
\cellcolor{gray!6}{Datsun 710} & \cellcolor{gray!6}{22.8} & \cellcolor{gray!6}{4} & \cellcolor{gray!6}{108.0} & \cellcolor{gray!6}{93} & \cellcolor{gray!6}{3.85} & \cellcolor{gray!6}{2.32} & \cellcolor{gray!6}{18.61} & \cellcolor{gray!6}{1} & \cellcolor{gray!6}{1} & \cellcolor{gray!6}{4} & \cellcolor{gray!6}{1}\\
Hornet 4 Drive & 21.4 & 6 & 258.0 & 110 & 3.08 & 3.21 & 19.44 & 1 & 0 & 3 & 1\\*
\end{longtable} 

@m3hdad
Copy link

m3hdad commented Mar 24, 2023

Thanks for the follow up.
Just to make it clear, I do get an extra line \textbackslash end\{landscape\} only when I use landscape and longtables regardless whether I use landscape() function or wrap the chunk around with begin\{landscape} end\{landscape}.

So I would say it is probably related to rmarkdown/bookdown.
This is the tex end of my table:

D9Q98XXXX & APE1 & Accumulation of photosynthesis to environment & \cellcolor[HTML]{f3d94c}{\bgroup\fontsize{5}{7}\selectfont 131.2\egroup{}} & \cellcolor[HTML]{E8948D}{\textcolor{white}{\textbf{\bgroup\fontsize{5}{7}\selectfont  0.64\egroup{}}}} & \cellcolor[HTML]{bbbbbb}{\textcolor{white}{\bgroup\fontsize{5}{7}\selectfont  0.38\egroup{}}} & \cellcolor[HTML]{bbbbbb}{\textcolor{white}{\bgroup\fontsize{5}{7}\selectfont -0.40\egroup{}}} & \cellcolor[HTML]{A59EF1}{\textcolor{white}{\textbf{\bgroup\fontsize{5}{7}\selectfont -1.03\egroup{}}}}\\*
\end{longtable}
\endgroup{}
\end{landscape}

\textbackslash end\{landscape\}

I'm not an expert in the topic and I'm relying on others to properly report the bug :D cheers

@phargarten2
Copy link

The version submitted by @aschersleben fixed the issue described here. The source code is removed in the final PDF file.

Use devtools::install_github("aschersleben/kableExtra") to install the abovementioned pull request (that fixes this problem) as long as the pull request is not merged with this repository.

The main versions used that solve the problem.

#devtools::update_packages("kableExtra")
#devtools::install_github("aschersleben/kableExtra")`
# xfun::session_info(packages = c("rmarkdown", "kableExtra"), dependencies = FALSE)

R version 4.2.3 (2023-03-15 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045), RStudio 2023.6.0.421

Locale:
  LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
  LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
  LC_TIME=English_United States.utf8    

Package version:
  kableExtra_1.3.4.9000 rmarkdown_2.22       

Pandoc version: 3.1.1

A full list of packages with all dependencies

# > xfun::session_info(packages = c("rmarkdown", "kableExtra"), dependencies = TRUE)
Package version:
  askpass_1.1           base64enc_0.1.3       bslib_0.5.0          
  cachem_1.0.7          callr_3.7.3           cli_3.6.1            
  colorspace_2.1.0      cpp11_0.4.3           curl_5.0.1           
  digest_0.6.31         ellipsis_0.3.2        evaluate_0.21        
  fansi_1.0.4           farver_2.1.1          fastmap_1.1.1        
  fontawesome_0.5.1     fs_1.6.1              glue_1.6.2           
  graphics_4.2.3        grDevices_4.2.3       highr_0.10           
  htmltools_0.5.5       httr_1.4.6            jquerylib_0.1.4      
  jsonlite_1.8.5        kableExtra_1.3.4.9000 knitr_1.43           
  labeling_0.4.2        lifecycle_1.0.3       magrittr_2.0.3       
  memoise_2.0.1         methods_4.2.3         mime_0.12            
  munsell_0.5.0         openssl_2.0.6         pillar_1.9.0         
  pkgconfig_2.0.3       processx_3.8.0        ps_1.7.3             
  R6_2.5.1              rappdirs_0.3.3        RColorBrewer_1.1.3   
  rlang_1.1.0           rmarkdown_2.22        rstudioapi_0.14      
  rvest_1.0.3           sass_0.4.6            scales_1.2.1         
  selectr_0.4.2         stats_4.2.3           stringi_1.7.12       
  stringr_1.5.0         svglite_2.1.1         sys_3.4.2            
  systemfonts_1.0.4     tibble_3.2.1          tinytex_0.45         
  tools_4.2.3           utf8_1.2.3            utils_4.2.3          
  vctrs_0.6.1           viridisLite_0.4.2     webshot_0.5.4        
  withr_2.5.0           xfun_0.39             xml2_1.3.4           
  yaml_2.3.7           

@piiskop
Copy link
Author

piiskop commented Jun 13, 2023

I confirm that by using aschersleben/kableExtra the issue is solved.

Is this the main kableExtra or something else?

@phargarten2
Copy link

I confirm that by using aschersleben/kableExtra the issue is solved.

Is this the main kableExtra or something else?

@piiskop, upon, looking at the main kable_Extra (haozhu233\kableExtra), the fix has not yet been applied (https://github.com/haozhu233/kableExtra/blob/master/R/kable_styling.R). A fix has been purposed to @haozhu233 (see #742) and is awaiting his response.

Thus, we need to currently use aschersleben/kableExtra.

@piiskop
Copy link
Author

piiskop commented Jan 17, 2024

As of 2024-1-17, the problem is still persistent although the issue is closed as completed. It is not fixed in the current version 1.3.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants