Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ importFrom(htmltools,htmlEscape)
importFrom(htmltools,save_html)
importFrom(htmltools,tagList)
importFrom(htmltools,tags)
importFrom(jsonlite,read_json)
importFrom(knitr,asis_output)
importFrom(knitr,fig_path)
importFrom(knitr,include_graphics)
Expand Down Expand Up @@ -329,6 +330,7 @@ importFrom(officer,table_width)
importFrom(officer,to_html)
importFrom(officer,to_rtf)
importFrom(officer,to_wml)
importFrom(quarto,quarto_version)
importFrom(ragg,agg_png)
importFrom(rlang,call_args)
importFrom(rlang,enquo)
Expand Down
21 changes: 16 additions & 5 deletions R/latex_str.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ list_latex_dep <- function(float = FALSE, wrapfig = FALSE) {
return(list())
}

is_quarto <- is_in_quarto()

fonts_ignore <- flextable_global$defaults$fonts_ignore
fontspec_compat <- get_pdf_engine() %in% c("xelatex", "lualatex")
if (!is_quarto && !fonts_ignore && !fontspec_compat) {
if (!fonts_ignore && !fontspec_compat) {
warning("fonts used in `flextable` are ignored because ",
"the `pdflatex` engine is used and not `xelatex` or ",
"`lualatex`. You can avoid this warning by using the ",
Expand All @@ -43,7 +41,7 @@ list_latex_dep <- function(float = FALSE, wrapfig = FALSE) {

x <- list()

if (fontspec_compat || is_quarto) {
if (fontspec_compat) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I insist that variable is_quarto there is not useful. In the first use case, only used for triggering a warning. In the second use case, it's only fontspec_compat, as returned by function get_pdf\engine() that shoudl be used -> indeed, if the rendering is quarto, but the user-defined pdf-engine is pdflatex, you'll add fontspeclatex package dependancy, which is not compliant with the use of pdflatex (will even return the error I've raised in the issue).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eli-daniels (and @davidgohel ) sorry for my delayed answers. I've:

  • reverted the formatting changes induced by airstyler
  • precisely detail which chnages I have brought to R/latex_str.R
  • fetch the latest Quarto changes, and ensure that with my latest changes, rendering with Quarto is compliant with the choice of pdflatex engine

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accordingly, i believe that now, you can finally incporate my suggested PR.

x$fontspec <- latex_dependency("fontspec")
}
x$multirow <- latex_dependency("multirow")
Expand Down Expand Up @@ -433,11 +431,24 @@ get_pdf_engine <- function() {
if (length(rd)) {
engine <- pandoc_args[rd + 1]
} else if (is_in_quarto()) {
engine <- rmarkdown::metadata$`pdf-engine`
quarto_v <- quarto::quarto_version()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eli-daniels the core changes are brought there.

Depending on the retrieved quarto version, two processes to retrieve metadata of a document:

  • Sys.getenv("QUARTO_EXECUTE_INFO") since Quarto v1.8, as described in details here
  • rmarkdown::metadata, the old-fahsioned way.

if (quarto_v >= "1.8.0") {
# Modern and recommended way to retrieve metadata, from https://github.com/quarto-dev/quarto-cli/discussions/13371
quarto_metadata <- jsonlite::read_json(
Sys.getenv("QUARTO_EXECUTE_INFO")
)
# engine <- quarto_metadata$format$pandoc$`pdf-engine` -> this field is never empty
engine <- quarto_metadata$format$metadata$format$pdf$`pdf-engine` # this field has a value only if the yaml key `pdf-engine` is explicitly mentionned
} else {
# Old trick for calling metadata, only works if the rendering engine is knitr (and not jupyter)
quarto_metadata <- rmarkdown::metadata
engine <- quarto_metadata$format$pdf$`pdf-engine`
}
if (is.null(engine) || engine == "") {
engine <- "xelatex"
}
} else {
# Default pdf-engine for RMarkdown
engine <- "pdflatex"
}
engine
Expand Down
Binary file added tests/testthat/qmd/quarto_metadata.rds
Binary file not shown.
Binary file added tests/testthat/qmd/quarto_metadata_second_way.rds
Binary file not shown.
Binary file added tests/testthat/qmd/rmarkdown_metadata.rds
Binary file not shown.
Binary file added tests/testthat/qmd/rmarkdown_pandoc_args.rds
Binary file not shown.
Binary file added tests/testthat/qmd/use-printer-with-pdflatex.pdf
Binary file not shown.
69 changes: 69 additions & 0 deletions tests/testthat/qmd/use-printer-with-pdflatex.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: "Pdflatex not working with flextable"
engine: knitr
format:
pdf:
pdf-engine: pdflatex # tectonic, latexmk, xelatex, lualatex
keep-tex: true
---


```{r}
#| label: setup

library(flextable)


# different ways to access quarto metadata ----
## Many things listed in quarto_metadata, like default options, but not the current options used in the yaml
quarto_metadata <- knitr::opts_current$get()
saveRDS(quarto_metadata, "./quarto_metadata.rds")

## Only works for quarto version beyond 1.8, but lots of nice execution operations
quarto_metadata_second_way <- jsonlite::read_json(
Sys.getenv("QUARTO_EXECUTE_INFO"))
# two ways to reach the pdf-engine
all.equal(quarto_metadata_second_way$format$pandoc$`pdf-engine`,
quarto_metadata_second_way$format$metadata$format$pdf$`pdf-engine`)
saveRDS(quarto_metadata_second_way, "./quarto_metadata_second_way.rds")

## This command returns exactly the yaml of the header, great!! :-)
rmarkdown_metadata <- rmarkdown::metadata
saveRDS(rmarkdown_metadata, "./rmarkdown_metadata.rds")

## Returns only the output file
rmarkdown_pandoc_args <- knitr::opts_knit$get("rmarkdown.pandoc.args")
saveRDS(rmarkdown_pandoc_args, "./rmarkdown_pandoc_args.rds")

## programamtic ways to run qmd, or Rmd scripts ----
# rmarkdown::render("./tests/testthat/qmd/use-printer-with-pdflatex.qmd",
# output_format = c("pdf_document"),
# output_file = "C:/Users/basti/OneDrive/04. BNCL (Post Doc)/Open-Source Development/flextable/tests/testthat/qmd/use-printer-with-pdflatex.qmd")
# quarto::quarto_render("./tests/testthat/qmd/use-printer-with-pdflatex.qmd")
```


- `flextable` with Equations, see @tbl-flextable:

```{r}
#| label: tbl-flextable


eqs_flextable <- c(
"(ax^2 + bx + c = 0)",
"a \\ne 0",
"x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}")
df <- tibble::tibble(`Y \\sim W` = eqs_flextable)


ft <- flextable(df) |>
compose(j = 1, part = "header",
value = as_paragraph(as_equation(`Y \\sim W`, width = 2, height = .5)),
use_dot = TRUE) |>
compose(j = 1, part = "body",
value = as_paragraph(as_equation(`Y \\sim W`, width = 2, height = .5))) |>
align(align = "center", part = "all")

ft

```
Loading