rjournal / rjtools

Tools for AUTHORS to use for checking and submitting articles to the R Journal
https://rjournal.github.io/rjtools/
Other
31 stars 14 forks source link

While knitting to rjournal_web_article, I get this error: "! LaTeX Error: Environment longtable undefined." #73

Closed technOslerphile closed 1 year ago

technOslerphile commented 1 year ago

I am trying to compile an article for R journal using the rjtools package. I have a table in the .rmd file and without the table, the knitting works just fine. But when I include the table, the following error is seen in RJwrapper.log file:

! LaTeX Error: Environment longtable undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.74 \begin{longtable}

I have tried to add a new preamble.tex file explictly stating \usepackage{longtable} in the tex file, but this didn't work out.

output: 
  rjtools::rjournal_web_article:
    self_contained: yes
    toc: no
    includes:
      in_header: preamble.tex

Can someone help me fix this issue?

dicook commented 1 year ago

@technOslerphile please give a small reproducible example.

This will only work for pdf output, though. You will need to have another table format for the HTML, and a conditional code block.

technOslerphile commented 1 year ago

@dicook I have created a dummy .rmd file which has the same error. The RMD file is attached here

The HTML file is getting created fine without any issues. But when the rendering to pdf is initiated (rmarkdown::render(input, output_format = "rjtools::rjournal_pdf_article"), the execution halts. In the RStudio console the error goes like this:

Error: 
! error in callr subprocess
Caused by error: 
! LaTeX failed to compile RJwrapper.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See RJwrapper.log for more info.
---
Backtrace:
1. rmarkdown::render("C:/…
2. output_format$on_exit()
3. local overlay()
4. callr::r(function(input) { …
5. callr:::get_result(output = out, options)
6. callr:::throw(callr_remote_error(remerr))
7. callr:::callr_remote_error(remerr)
8. callr:::throw(err, parent = remerr[[3]])
---
Subprocess backtrace:
 1. rmarkdown::render(input, output_format = "rjtools::rjournal_pdf_article")
 2. output_format$post_processor(front_matter, input, output_file, …
 3. tinytex::latexmk("RJwrapper.tex", fmt$pandoc$latex_engine, pdf_file = xfun…
 4. tinytex:::latexmk_emu(file, engine, bib_engine, engine_args, min_times, …
 5. local run_engine()
 6. tinytex:::system2_quiet(engine, c("-halt-on-error", "-interaction=batchmode", …
 7. local on_error()
 8. tinytex:::show_latex_error(file, logfile)
 9. base::stop(e, " See ", logfile, " for more info.", call. = FALSE)
10. | base::.handleSimpleError(function (e) …
11. global h(simpleError(msg, call))
Execution halted

And when I check the RJwrapper.log file, I can see the exact error. And this is:

! LaTeX Error: Environment longtable undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.13 \begin{longtable}
                      []{@{} 
Here is how much of TeX's memory you used:
 26115 strings out of 477196
 485036 string characters out of 5811767
 1917238 words of memory out of 5000000
 46634 multiletter control sequences out of 15000+600000
 572338 words of font info for 82 fonts, out of 8000000 for 9000
 14 hyphenation exceptions out of 8191
 117i,3n,131p,1006b,488s stack positions out of 10000i,1000n,20000p,200000b,200000s

!  ==> Fatal error occurred, no output PDF file produced!
dicook commented 1 year ago

@technOslerphile would you email me the Rmd file. The download link doesn't work for me.

s-u commented 1 year ago

error-latex.md (rename to .rmd since GH doesn't like rmd extensions)

dicook commented 1 year ago

It does knit for me, if I add

preamble: 
  \usepackage{longtable}

to the YAML.

test.pdf

(I am using the GitHub version of rjtools, that has not yet been made available on CRAN.)

But there is a better way to do your table, so that it displays well in both html and pdf. I'll map it out and upload.

s-u commented 1 year ago

Yes, but this is still a bug, because the longtable is not created by the author, but inserted by pandoc. We already have a conditional for this in the template https://github.com/rjournal/rjtools/blob/5a163c956614a2de6feadadc1786d6cfe67e9c29/inst/tex/RJwrapper.tex#L17-L19 but the variable $tables that the template expects is not set in that case. So this is either a bug in pandoc or a bug in the template (or missing API). I have not found $tables in pandoc documented variables, so someone familiar with the code may want to comment.

s-u commented 1 year ago

Looking at pandoc docs is says that longtable is always required for TeX output so either it should be unconditional in the template, or we have to specify more clearly that tables have to be declared in the yaml part (and how). We shouldn't require manual insertion of \usepackage as it should really be handled by the template (as we do already except it's unclear how to enable it).

dicook commented 1 year ago

This is the better way to do the table. Then you have more control over the format in both html and pdf. (Purely markdown tables, as you initially coded it, are not great in either format.)

```{r echo=FALSE}
t1 <- data.frame(
  Parameter = c("$J$", "$\\Delta$", "$\\sigma_b^2$", "$\\sigma_w^2$"),
  Definition = c("Number of readers", 
                 "The difference in FOM (effect size) of readers when using one modality as compared to the other modality.",
                 "Variance of the FOM of readers when using the same modality for the same sample of patients (inter-reader variability).", 
                 "Variance of the FOM of a reader when using the same imaging technique for the same sample of patients in different occasions (intra-reader variability)."),
  Comments = c("A minimum of 5 is recommended for any MRMC study.",
               "In sample size estimations, $\\Delta$ has a great effect on sample size. Typically used $\\Delta$ range from 0.04 to 0.06 corresponding to a 4 to 6 percent difference in the FOM of readers between the two modalities. Smaller the $\\Delta$, larger the required number of cases and readers.",
               "This is not intuitive to conjecture as this is an estimate of variance.",
               "This is not intuitive to conjecture as this is an estimate of variance. ")
)
knitr::kable(t1, format = "html", caption = "Parameters and definitions")
library(kableExtra)
knitr::kable(t1, format = "latex", align = "l", escape=F, caption = "Parameters and definitions") %>%
  column_spec(2, width = "15em") %>%
  column_spec(3, width = "15em")
dicook commented 1 year ago

And the output

test.pdf

technOslerphile commented 1 year ago

This is the better way to do the table. Then you have more control over the format in both html and pdf. (Purely markdown tables, as you initially coded it, are not great in either format.)

```{r echo=FALSE}
t1 <- data.frame(
  Parameter = c("$J$", "$\\Delta$", "$\\sigma_b^2$", "$\\sigma_w^2$"),
  Definition = c("Number of readers", 
                 "The difference in FOM (effect size) of readers when using one modality as compared to the other modality.",
                 "Variance of the FOM of readers when using the same modality for the same sample of patients (inter-reader variability).", 
                 "Variance of the FOM of a reader when using the same imaging technique for the same sample of patients in different occasions (intra-reader variability)."),
  Comments = c("A minimum of 5 is recommended for any MRMC study.",
               "In sample size estimations, $\\Delta$ has a great effect on sample size. Typically used $\\Delta$ range from 0.04 to 0.06 corresponding to a 4 to 6 percent difference in the FOM of readers between the two modalities. Smaller the $\\Delta$, larger the required number of cases and readers.",
               "This is not intuitive to conjecture as this is an estimate of variance.",
               "This is not intuitive to conjecture as this is an estimate of variance. ")
)
knitr::kable(t1, format = "html", caption = "Parameters and definitions")
library(kableExtra)
knitr::kable(t1, format = "latex", align = "l", escape=F, caption = "Parameters and definitions") %>%
  column_spec(2, width = "15em") %>%
  column_spec(3, width = "15em")

Thanks, this approach worked fine. However, it requires a lot of work though especially for large tables. Earlier the 'visual' mode in RStudio allowed me to prepare the table intuitively in like in MS Word, but unfortunately the longtable error was popping up. If this error can be fixed instead, then that will be of great help.

s-u commented 1 year ago

@technOslerphile as mentioned before (see https://github.com/rjournal/rjtools/issues/73#issuecomment-1641279734) the error is easily avoided by simply adding longtable to the preamble or in the RJwrapper.tex with no adverse effects.

technOslerphile commented 1 year ago

@technOslerphile as mentioned before (see #73 (comment)) the error is easily avoided by simply adding longtable to the preamble or in the RJwrapper.tex with no adverse effects.