rstudio / tinytex

A lightweight, cross-platform, portable, and easy-to-maintain LaTeX distribution based on TeX Live
https://yihui.org/tinytex/
Other
986 stars 116 forks source link

! LaTeX Error: Environment longtabu undefined #218

Closed drag05 closed 3 years ago

drag05 commented 4 years ago

I am building a shiny app that basically connects to a database, displays a table where the User can filter the data and then calls rmarkdown::render() by pressing a button and generate a .pdf report in app's subdirectory named ./makeReport.

For this app, all R packages are loaded via the global.R script.

The Rmd works fine when used in stand alone form, either from R Studio, R console or command line, and loads same libraries via a contained R chunk.

I use tinytex and run tinytex_install and tlmgr_update. I have no MiKTex installed.

The pdf report contains text, graphs, and tables crossing several pages. Tables wider than 6 columns are column-split via pander package.

The remaining tables are generated via knitr::kable() using the custom function below:

reshapeTables <- function(data, caption) {

  stopifnot(length(dim(data)) == 2L)

  rTab <- if(dim(data)[[2]] > 6){ ## 6 represents 3 conditions of 2 replicates each

  panderOptions('knitr.auto.asis', TRUE)

   pander(data, caption = caption
            , justify = 'left'
            , style = 'grid'
            , split.table = 90
            , keys.as.row.names = TRUE)

     } else {

knitr::kable(data
        , format = 'latex'
        , caption = caption
        , align = 'l'
        , booktabs = TRUE
        , longtable = TRUE) %>%
   kable_styling(font_size = 10) %>%
   kable_styling(full_width = TRUE, latex_options = c('repeat_header', 'hold_position')) %>%
   column_spec(1, bold = TRUE, color = 'red')

                }
 return(rTab)
}

which throws the following error:

This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/W32TeX) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
I was unable to find any missing LaTeX packages from the error log ReportFulc.log.
! LaTeX Error: Environment longtabu undefined.

Error : LaTeX failed to compile 

This is one issue. The second is the Report loses its header together with all the information in it. The footer is still there but cannot get it to show the page of Total pages info. Just the page

Probably misinterpreting the error message above, at first I tried to install package longtable.sty using tinytex::tlmgr_install('longtable') and got the follwing messages:

tlmgr install longtable
tlmgr.pl install: package longtable not present in repository.
tlmgr.pl: action install returned an error; continuing.
tlmgr.pl: package repository http://ctan.mirror.colo-serv.net/systems/texlive/tlnet (verified)
tlmgr.pl: An error has occurred. See above messages. Exiting.
tlmgr update --self
tlmgr install longtable
tlmgr.pl install: package longtable not present in repository.
tlmgr.pl: action install returned an error; continuing.
tlmgr.pl: package repository http://ctan.mirror.colo-serv.net/systems/texlive/tlnet (verified)
tlmgr.pl: An error has occurred. See above messages. Exiting.

I am able to print the table if use format = 'pandoc' or format = 'markdown'.

The yaml header looks like this:

title: "Report"
output:
  pdf_document: 
    fig_caption: yes
    toc: yes
    toc_depth: 1
toc_includes:
fontawesome: yes
header-includes: 
  \usepackage{titling}
  \usepackage{float}
  \usepackage{mdframed}
  \usepackage{fancyhdr}
  \usepackage{titlesec} 
  \usepackage{graphicx} 
  \usepackage{booktabs,xcolor}
  \usepackage{lastpage}
  \usepackage{longtable}
  \setlength{\droptitle}{-5em} 
  \pagestyle{fancy} \fancyhf{} \addtolength{\headheight}{20pt}
  \fancyfoot[R]{\footnotesize Page \thepage\, of\, \pageref*{LastPage}}
  \setlength\footskip{12pt}
  \fancypagestyle{plain}{\pagestyle{fancy}} 
  \lhead{<authors> - \today}
  \rhead{\includegraphics[width=0.7in]{./www/Logo.png}}

and

session_info()
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363), RStudio 1.3.959

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

Package version:
  abind_1.4-5         antiword_1.3        askpass_1.1         assertthat_0.2.1   
  backports_1.1.7     base64enc_0.1.3     BH_1.72.0.3         bitops_1.0.6       
  blob_1.2.1          boot_1.3.25         broom_0.5.6         callr_3.4.3        
  car_3.0-8           carData_3.0-4       cellranger_1.1.0    cli_2.0.2          
  clipr_0.7.0         colorspace_1.4-1    compiler_4.0.0      corrplot_0.84      
  cowplot_1.0.0       crayon_1.3.4        crosstalk_1.1.0.1   curl_4.3           
  data.table_1.12.9   DBI_1.1.0           dbplyr_1.4.4        desc_1.2.0         
  digest_0.6.25       dplyr_1.0.0         DT_0.13             ellipsis_0.3.1     
  evaluate_0.14       fansi_0.4.1         farver_2.0.3        fastmap_1.0.1      
  forcats_0.5.0       foreign_0.8-80      generics_0.0.2      ggplot2_3.3.1      
  ggpubr_0.3.0        ggrepel_0.8.2       ggsci_2.9           ggsignif_0.6.0     
  glue_1.4.1          graphics_4.0.0      grDevices_4.0.0     grid_4.0.0         
  gridExtra_2.3       gtable_0.3.0        haven_2.3.1         highr_0.8          
  hms_0.5.3           htmltools_0.4.0     htmlwidgets_1.5.1   httpuv_1.5.4       
  httr_1.4.1          isoband_0.2.1       jsonlite_1.6.1      kableExtra_1.1.0   
  knitr_1.28          labeling_0.3        later_1.1.0.1       lattice_0.20-41    
  lazyeval_0.2.2      lifecycle_0.2.0     lme4_1.1.23         magrittr_1.5       
  maptools_1.0.1      markdown_1.1        MASS_7.3.51.6       Matrix_1.2.18      
  MatrixModels_0.4.1  methods_4.0.0       mgcv_1.8.31         mime_0.9           
  minqa_1.2.4         munsell_0.5.0       ndjson_0.8.0        nlme_3.1-148       
  nloptr_1.2.2.1      nnet_7.3.14         openssl_1.4.1       openxlsx_4.1.5     
  pander_0.6.4        parallel_4.0.0      patchwork_1.0.0     pbkrtest_0.4.8.6   
  pdftools_2.3.1      pillar_1.4.4        pkgbuild_1.0.8      pkgconfig_2.0.3    
  pkgload_1.1.0       plyr_1.8.6          polynom_1.4.0       pool_0.1.4.3       
  praise_1.0.0        prettyunits_1.1.1   processx_3.4.2      progress_1.2.2     
  promises_1.1.1      ps_1.3.3            purrr_0.3.4         qpdf_1.1           
  quantreg_5.55       R6_2.4.1            RColorBrewer_1.1-2  Rcpp_1.0.4.6       
  RcppEigen_0.3.3.7.0 RCurl_1.98.1.2      readODS_1.6.7       readr_1.3.1        
  readtext_0.76       readxl_1.3.1        rematch_1.0.1       reshape2_1.4.4     
  rio_0.5.16          rjson_0.2.20        rlang_0.4.6         rmarkdown_2.2      
  RMySQL_0.10.20      rprojroot_1.3.2     rstatix_0.5.0       rstudioapi_0.11    
  rvest_0.3.5         scales_1.1.1        selectr_0.4.2       shiny_1.4.0.2      
  shinythemes_1.1.2   sourcetools_0.1.7   sp_1.4.2            SparseM_1.78       
  splines_4.0.0       statmod_1.4.34      stats_4.0.0         streamR_0.4.5      
  stringi_1.4.6       stringr_1.4.0       striprtf_0.5.2      sys_3.3            
  testthat_2.3.2      tibble_3.0.1        tidyr_1.1.0         tidyselect_1.1.0   
  tinytex_0.23.2      tools_4.0.0         utf8_1.1.4          utils_4.0.0        
  vctrs_0.3.1         viridisLite_0.3.0   webshot_0.5.2       withr_2.2.0        
  xfun_0.14           xml2_1.3.2          xtable_1.8-4        yaml_2.2.1         
  zip_2.0.4          

Please advise, thank you!

Originally posted by @drag05 in https://github.com/yihui/tinytex/issues/217#issuecomment-643645877

cderv commented 4 years ago

For reference, there is also a report in Rstudio community https://community.rstudio.com/t/rmd-works-fine-in-r-but-not-in-shiny/69109

drag05 commented 4 years ago

@cderv
I know, that's mine too. Still trying to find an answer.

Thank you!

cderv commented 4 years ago

Oh ok ! As there was the same error in both, I linked those. Please do not hesitate to link when cross-posted. I'll look into it when I found some time.

drag05 commented 4 years ago

@cderv

I will keep that in mind. Thank you!

drag05 commented 4 years ago

@cderv

Hello, I wonder if there were any developments on this issue. Please advise, thank you!

cderv commented 4 years ago

What you have is a latex error. So if you google search for CTAN and longtabu, you'll find that this is included in tabu TeX package: https://ctan.org/pkg/tabu . Try to install the this package and make it available to your document.

I think this longtabu environment is coming for the kableExtra 📦 because you used fullwidth = TRUE https://github.com/haozhu233/kableExtra/blob/9399dccf5d722878cbb07cfbb2bf037fbb5582ad/R/kable_styling.R#L360-L366 I believe the use of package tabut should be added by default.

Maybe @haozhu233 could help, but it will be like us : Without a proper minimal reproducible example, there is not much we can do to help you ! Thank you!

Hope this helps.

drag05 commented 4 years ago

@cderv

Thank you for your detailed answer! I thought the minimal reproducible example given above would have been sufficient.

The link you sent me shows requirement for both longtable and tabu:

longtable.sty was present in my tinytex installation but inside an environment called tools (below), together with few other latex packages

tlmgr_search('longtable')
tools:
    texmf-dist/doc/latex/tools/longtable.pdf
    texmf-dist/tex/latex/tools/longtable.sty
    texmf-dist/source/latex/tools/longtable.dtx
    texmf-dist/source/latex/tools/longtable.ins

but not present as an installed TeX Live package listed by tl_pkgs():

 'longtable' %in% grep( '^l', 
                              tl_pkgs(), 
                                      value = TRUE)
[1] FALSE

while tabu was present at all times:

> grep('^t', 
              tinytex::tl_pkgs(),
                               value = TRUE)

 [1] "tabu"            "tex"            
 [3] "tex-ini-files"   "texlive-scripts"
 [5] "texlive.infra"   "textcase"       
 [7] "threeparttable"  "thumbpdf"       
 [9] "times"           "tipa"           
[11] "titlesec"        "tlgs"           
[13] "tlperl"          "tlpsv"          
[15] "tools"           "translator"     
[17] "tufte-latex"    

I assumed that - when called from within Shiny - the rmarkdown::render() function does not look in the tools environment for longtable however, it is able to look in this environment when called from outside Shiny.

Therefore, I have implemented a workaround in which function render() is called from outside Shiny. The workaround brought back the long table split accross pages, as well as the pdf Report's missing header and footer.

Thank you!

cderv commented 4 years ago

I don't think it is related to longtable as the error you have is

! LaTeX Error: Environment longtabu undefined.

So longtabu environment, which is defined in the tabu CTAN package, that should be included in your document when you render to PDF.

I thought the minimal reproducible example given above would have been sufficient.

Sorry I may have missed it but I didn't find a piece of code I could run to reproduce your issue.

Have you already isolate the issue to be related to when in a shiny app? Or do you have still have the issue when you knit the document to pdf outside your app ?

Anyway, having an example we can run would be useful. You can even link a zip file if needed. See the issue guide : https://yihui.org/issue/#bug-reports

Thank you !

Therefore, I have implemented a workaround in which function render() is called from outside Shiny. The workaround brought back the long table split accross pages, as well as the pdf Report's missing header and footer.

And this would help me understand this, because if lontable is the issue here, I don't understand the first error message. 😄

drag05 commented 4 years ago

@cderv

The issue appears only when render() is being called within Shiny. Quoting my first entry: "The Rmd works fine when used in stand alone form, either from R Studio, R console or command line, and loads same libraries via a contained R chunk."

  1. Regarding the ! LaTeX Error: Environment longtabu undefined error:

    The word 'longtabu' is mentioned in the kableExtra link that you posted previously

However,

> tlmgr_install('longtabu')

tlmgr install longtabu
tlmgr.pl install: package longtabu not present in repository.
tlmgr.pl: action install returned an error; continuing.
tlmgr.pl: package repository http://ctan.mirror.colo-serv.net/systems/texlive/tlnet (verified)
tlmgr.pl: An error has occurred. See above messages. Exiting.

tlmgr update --self
tlmgr install longtabu

tlmgr.pl install: package longtabu not present in repository.
tlmgr.pl: action install returned an error; continuing.
tlmgr.pl: package repository http://ctan.mirror.colo-serv.net/systems/texlive/tlnet (verified)
tlmgr.pl: An error has occurred. See above messages. Exiting.
> 

This suggests that the presence of word 'longtabu' inside pdfTable_styling function may need be reconsidered.

And what is "longtabu" anyway? If it is an environment inside tabu then why is it mentioned along with longtable and tabu in the ifelse statement?

  1. Regarding the workaround:

    in my Server.R, instead of having

# create and save pdf report

  observeEvent(input$reportPDF, {

                        rmarkdown::render('Report.Rmd'
                                           , output_dir = './www/makeReport'
                     )
            ...

      })

which throws the error

! LaTeX Error: Environment longtabu undefined,

I am sourcing a simple script named "WorkaroundScript.R":

# create and save pdf report

  observeEvent(input$reportPDF, {

                        source('WorkaroundScript.R')
          ....
    })

The WorkaroundScript.R is looking for "Report.Rmd" in the "./data" directory:

try(rmarkdown::render('./data/Report.Rmd'

                              , output_dir = './www/makeReport'))

saving the pdf Report in the '/makeReport' directory.

This is the workaround which obtains the pdf Report as required, without solving the "longtabu undefined" error above.

Thank you!

cderv commented 3 years ago

Sorry for the delay.

I don't think there is anything to do in tinytex. As I said above https://github.com/yihui/tinytex/issues/218#issuecomment-694738017, using kableExtra requires a set of packages in your document. See page 4 of this documentation https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

I believe you are missing the tabu package for CTAN. You need \usepackage{tabu}. I mentioned this several time. Did you try ? Thanks.

kableExtra should load them by default for you but it seems in your usage it is not.

drag05 commented 3 years ago

@cderv : Sorry for the big delay in my answer! I have followed your last advice with the same result. Thank you for your time!

TC-RAC commented 1 year ago

I am having what appear to be very similar problems but find the discussion above does not help.

It appears the kable-styling function is requiring the Latex tabu package, but tabu seems to be irretrievably broken... see this https://github.com/tabu-issues-for-future-maintainer/tabu

Is no-one updating kableExtra to resolve this cal for a defunct package?

cderv commented 1 year ago

Hi @TC-RAC,

Please do open a new issue instead of adding to one that does not help you directly. It is best to open a new one and link to an old one than commenting on a closed thread. Thank you.

Is no-one updating kableExtra to resolve this cal for a defunct package?

If you are using kableExtra and have issue with it with tabu LaTeX package, then you can open an issue in https://github.com/haozhu233/kableExtra

Thank you