Skip to content

Commit

Permalink
Fix #254: use \QuartoMarkdownBase64 to print captions and tables in L…
Browse files Browse the repository at this point in the history
…aTeX under quarto
  • Loading branch information
hughjonesd committed Jun 19, 2024
1 parent c604a38 commit ca7b30a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 28 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ URL: https://hughjonesd.github.io/huxtable/
BugReports: https://github.com/hughjonesd/huxtable/issues
Imports:
assertthat,
base64enc,
commonmark,
fansi,
generics,
Expand Down Expand Up @@ -81,7 +82,7 @@ SystemRequirements: LaTeX packages:
VignetteBuilder: knitr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Depends:
R (>= 2.10)
Expand Down
19 changes: 12 additions & 7 deletions R/aaa-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,9 @@ make_label <- function (ht) {
}

if (! is.null(chunk_label) &&
are_we_in_quarto() &&
requireNamespace("quarto", quietly = TRUE) &&
quarto::quarto_version() >= "1.4" &&
getOption("huxtable.knitr_output_format", guess_knitr_output_format()) == "latex"
using_quarto("1.4") &&
getOption("huxtable.knitr_output_format",
guess_knitr_output_format()) == "latex"
) {
msg <- paste(
"quarto cell labels do not work with huxtable in TeX for quarto ",
Expand All @@ -246,9 +245,15 @@ make_label <- function (ht) {
}


are_we_in_quarto <- function () {
requireNamespace("knitr", quietly = TRUE) &&
! is.null(knitr::opts_knit$get("quarto.version"))
using_quarto <- function (min_version = NULL) {
if (! requireNamespace("knitr", quietly = TRUE)) return(FALSE)
if (is.null(knitr::opts_knit$get("quarto.version"))) return(FALSE)
if (is.null(min_version)) return(TRUE)

# this is risky since they could have quarto without the R package
if (! requireNamespace("quarto")) return(FALSE)
qv <- quarto::quarto_version()
return(qv >= min_version)
}


Expand Down
6 changes: 6 additions & 0 deletions R/latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ build_latex_caption <- function (ht, lab) {

lab <- if (is.na(lab) || cap_has_label) "" else sprintf("\\label{%s}\n", lab)
cap <- paste(cap, lab)
if (using_quarto(min_version = "1.4") &&
getOption("huxtable.knitr_output_format",
guess_knitr_output_format()) == "latex") {
cap <- sprintf("\\QuartoMarkdownBase64{%s}",
base64enc::base64encode(charToRaw(cap)))
}

return(cap)
}
Expand Down
12 changes: 5 additions & 7 deletions R/package-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,18 @@ NULL
#' like `label: tbl-foo` and refer to them via `@tbl-foo`.
#'
#' In quarto versions 1.4 and above, when compiling to PDF,
#' quarto cross-referencing no longer works, and labels starting with
#' `tbl-` will cause an error. (This is a quarto
#' issue.) Instead, set labels within huxtable using [label()] or
#' quarto cross-referencing no longer works.
#' Instead, set labels within huxtable using [label()] or
#' [set_label()] and refer to them with TeX-only referencing using
#' `\ref{label}`. You must also set a caption, either via quarto or via
#' huxtable.
#' `\ref{label}`. You must also set a caption.
#'
#' Here's an example:
#'
#' ````
#' A reference to Table \ref{tab:jams}.
#' A reference to Table \ref{tbl-jams}.
#'
#' ```{r}
#' label(jams) <- "tab:jams"
#' label(jams) <- "tbl-jams"
#' caption(jams) <- "Some jams"
#' jams
#' ```
Expand Down
24 changes: 17 additions & 7 deletions man/huxtable-FAQ.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 28 additions & 6 deletions scripts/quarto-test.qmd
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
---
title: "Quarto tester"
format: pdf
format:
pdf:
keep-tex: true
html-table-processing: none
---

Quarto version: `r quarto::quarto_version()`.

Reference to @tbl-jams
# Builtin huxtable labels/captions

```{r}
#| tbl-cap: quarto cap
#| label: tbl-jams
library(huxtable)
caption(jams) <- "huxtable caption"
caption(jams) <- "Table of jams"
label(jams) <- "tbl-jams"
jams
# 1.5.45
# You need to enclose caption and label in \QuartoMarkdownBase64{}.
# Then you can use any label, although Quarto processing still won't work.
# 1.4.549
# works with no label/caption at all
# works with only quarto caption, only huxtable caption, or both:
Expand All @@ -25,4 +32,19 @@ jams
# works with quarto caption and label, so long as label is not tbl-
```

Post-reference to \ref{jams}.
Quarto reference to @tbl-jams.
LaTeX reference to \ref{tbl-jams}.

# Quarto labels/captions

```{r}
#| label: tbljams2
#| tbl-cap: Table of Jams 2
data(jams) # clean version
jams
```

Quarto reference to @tbljams2.
LaTeX reference to \ref{tbljams2}.

0 comments on commit ca7b30a

Please sign in to comment.