mattwarkentin / torontodown

University of Toronto thesis using R Markdown and bookdown
Other
4 stars 1 forks source link

Table captions cause LaTeX compiling failure #10

Closed ashinonyx closed 6 hours ago

ashinonyx commented 1 year ago

Alright, I'm having a new issue now!

When I try to render, I get the following error:

! Missing $ inserted.
<inserted text> 
                $
l.748 ...for _Panthera tigris_ and _Panthera leo_}

Try to find the following text in Reynolds_Ashley_R_2023_PhD_thesis.Rmd:
  ...for _Panthera tigris_ and _Panthera leo_} 

You may need to add $ $ around a certain inline R expression `r ` in Reynolds_Ashley_R_2023_PhD_thesis.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.
Error: LaTeX failed to compile Reynolds_Ashley_R_2023_PhD_thesis.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See Reynolds_Ashley_R_2023_PhD_thesis.log for more info.
In addition: Warning message:
In readLines(input) :
  incomplete final line found on '/Users/ashleyreynolds/Desktop/reynolds_thesis/chapters/pre/acknowledgements.Rmd'
Execution halted

This is one of the code blocks that throws this error:

table1 <- tibble(Taxon = c("_Panthera tigris_", "_Panthera leo_", "_Panthera tigris altaica_",
                             "_Panthera tigris jacksoni_", "_Panthera tigris sumatrae_", 
                             "_Panthera tigris tigris_", "_Panthera leo krugeri_", "_Panthera leo leo_",
                             "_Panthera leo massaica_", "_Panthera leo melanochaita_", 
                             "_Panthera leo persica_", "_Panthera leo senegalensis_"),
                 Origin = c("wild", "wild", 
                            "captive", "captive", "captive", "captive",
                            "captive", "captive", "captive", "captive", "captive", "captive"),
                 `Number of Females` = c(nrow(filter(unnest(slaght_tigers, data), sex == "Female")),
                                         nrow(filter(unnest(smuts_lions, data), sex == "Female")),
                                         nrow(filter(tiger_data, Sex == "Female", subspecies == "Panthera tigris altaica")),
                                         nrow(filter(tiger_data, Sex == "Female", subspecies == "Panthera tigris jacksoni")),
                                         nrow(filter(tiger_data, Sex == "Female", subspecies == "Panthera tigris sumatrae")),
                                         nrow(filter(tiger_data, Sex == "Female", subspecies == "Panthera tigris tigris")),
                                         nrow(filter(lion_data, Sex == "Female", subspecies == "Panthera leo krugeri")),
                                         nrow(filter(lion_data, Sex == "Female", subspecies == "Panthera leo leo")),
                                         nrow(filter(lion_data, Sex == "Female", subspecies == "Panthera leo massaica")),
                                         nrow(filter(lion_data, Sex == "Female", subspecies == "Panthera leo melanochaita")),
                                         nrow(filter(lion_data, Sex == "Female", subspecies == "Panthera leo persica")),
                                         nrow(filter(lion_data, Sex == "Female", subspecies == "Panthera leo senegalensis"))),
                 `Number of Males` = c(nrow(filter(unnest(slaght_tigers, data), sex == "Male")),
                                         nrow(filter(unnest(smuts_lions, data), sex == "Male")),
                                         nrow(filter(tiger_data, Sex == "Male", subspecies == "Panthera tigris altaica")),
                                         nrow(filter(tiger_data, Sex == "Male", subspecies == "Panthera tigris jacksoni")),
                                         nrow(filter(tiger_data, Sex == "Male", subspecies == "Panthera tigris sumatrae")),
                                         nrow(filter(tiger_data, Sex == "Male", subspecies == "Panthera tigris tigris")),
                                         nrow(filter(lion_data, Sex == "Male", subspecies == "Panthera leo krugeri")),
                                         nrow(filter(lion_data, Sex == "Male", subspecies == "Panthera leo leo")),
                                         nrow(filter(lion_data, Sex == "Male", subspecies == "Panthera leo massaica")),
                                         nrow(filter(lion_data, Sex == "Male", subspecies == "Panthera leo melanochaita")),
                                         nrow(filter(lion_data, Sex == "Male", subspecies == "Panthera leo persica")),
                                         nrow(filter(lion_data, Sex == "Male", subspecies == "Panthera leo senegalensis"))),
                 Source = c("Slaght et al. 2006", "Smuts et al. 1980",
                            "ZIMS", "ZIMS", "ZIMS", "ZIMS",
                            "ZIMS", "ZIMS", "ZIMS", "ZIMS", "ZIMS", "ZIMS"))

kable(table1, caption = "Sample sizes of observational data for _Panthera tigris_ and _Panthera leo_")

I'm also attaching the log file here. Reynolds_Ashley_R_2023_PhD_thesis.log

If I render the Rmd file outside of the thesis project, the table code blocks run and render just fine, so it seems to be an issue with bookdown/torontodown. I know of this issue and have tried the fixes there, to no avail.

(Not related to the error, but how would I go about removing the chapter title or using an abbreviated title in the header? Most of my chapter titles are too long to fit in the header space, so it looks ugly as of right now)

mattwarkentin commented 1 year ago

Seems like its related to the underscores in the caption. Are you trying to italicize these words? What happens if you use two asterisks instead (e.g., Sample sizes of observational data for *Panthera tigris* and *Panthera leo*)? The rendering of Markdown captions to LaTeX is handled by https://github.com/mattwarkentin/torontodown/blob/master/R/render_caption.R, which is not a function I wrote.

If this doesn't fix things, my suggestion would be to use LaTeX syntax for italicizing.

ashinonyx commented 1 year ago

The underscores indeed seem to be the issue. Unfortunately for whatever reason, the asterisks don't render as italics but rather as, e.g., *Panthera tigris* (iirc that's why I used the underscores in the first place, since I usually use asterisks).

If I try to use LaTeX syntax, I get the following error:

(*) NOTE: I saw chunk options " echo=FALSE, out.width="90%", fig.cap="Growth curve for wild \emph{Panthera tigris}.""
 please go to https://yihui.org/knitr/options
 (it is likely that you forgot to quote "character" options)
Error: '\e' is an unrecognized escape in character string starting ""Growth curve for wild \e"
mattwarkentin commented 1 year ago

Try adding escape = FALSE to the knitr::kable() function.

mattwarkentin commented 1 year ago

You may also need to add some extra backslashes, because R and backslashes don't play nicely together.

ashinonyx commented 1 year ago

No luck on either case.

mattwarkentin commented 1 year ago

This example works for me:

table1 <- tibble::tibble(Taxon = c("_Panthera tigris_", "_Panthera leo_", "_Panthera tigris altaica_",
                             "_Panthera tigris jacksoni_", "_Panthera tigris sumatrae_", 
                             "_Panthera tigris tigris_", "_Panthera leo krugeri_", "_Panthera leo leo_",
                             "_Panthera leo massaica_", "_Panthera leo melanochaita_", 
                             "_Panthera leo persica_", "_Panthera leo senegalensis_"),
                 Origin = c("wild", "wild", 
                            "captive", "captive", "captive", "captive",
                            "captive", "captive", "captive", "captive", "captive", "captive"))

table1 |> 
  mutate(
    Taxon = str_remove_all(Taxon, '_'),
    Taxon = paste0('\\textit{', Taxon, '}')
  ) |> 
  knitr::kable(
    caption = "Sample sizes of observational data for \\textit{Panthera tigris} and \\textit{Panthera leo}",
    escape = FALSE)
mattwarkentin commented 1 year ago

I had to trim down the data because I didn't have access to some required objects for the full data set. But this hopefully will provide some guidance.