davidgohel / flextable

table farming
https://ardata-fr.github.io/flextable-book/
554 stars 79 forks source link

Compatibility with rjtools PDF output #553

Closed fawda123 closed 1 year ago

fawda123 commented 1 year ago

I've posted an issue in the rjtools repository (https://github.com/rjournal/rjtools/issues/70), but I also figured there might be a solution on the flextable side. I cannot get a .Rmd file to render to PDF using flextable with the rjtools template.

---
draft: true
type: package
output: 
  rjtools::rjournal_web_article:
    self_contained: yes
    toc: no
header-includes:
- \usepackage{longtable}
---

```{r}
library(flextable)
flextable(head(cars))

It obviously works fine with a normal .Rmd file. 

output_format: pdf_document

library(flextable)
flextable(head(cars))

The error I receive with the rjtools render is from an undefined control sequence for  LaTeX:

! Undefined control sequence.

\Oldarrayrulewidth ``` I've found the code in flextable where these commands are added to LaTeX tabular output in the `knit_to_pdf()` function [here](https://github.com/davidgohel/flextable/blob/9105d3b6107175a0844f1b83ab61b324b2c0a20f/R/printers.R#L374). I think the rjtools package is doing something to the global LaTeX environment, but I'm wondering if there's something I can do to my flextable output to suppress these commands. I appreciate any ideas you might have.
davidgohel commented 1 year ago

I am sorry, I don't feel able to dive into rjtools that I don't know at all and I have little time available.

I believe 'flextable' implementation is correct, although of course there are always a doubt about specific cases. flextable latex dependencies are managed automatically and for manual insertion, add_latex_dep() is provided.

Now, when I run your example naively, I get an HTML output and all is ok. I can read you are talking about latex but I don't understand what is the relation - rjtools::rjournal_web_article is about HTML output.

Capture d’écran 2023-07-09 à 12 12 55
fawda123 commented 1 year ago

Hi @davidgohel, thanks for the reply and I completely understand that this is outside of the scope of flextable. That being said, I should have clarified that the rjtools::rjournal_web_article format renders both an HTML and PDF. As your example shows, the HTML web page works fine, but the PDF does not. You'll see the error I'm referring to if you switch the YAML to only PDF article output.

---
draft: true
type: package
output: 
  rjtools::rjournal_pdf_article:
    self_contained: yes
    toc: no
header-includes:
- \usepackage{longtable}
---

```{r}
library(flextable)
flextable(head(cars))

! Undefined control sequence.

\Oldarrayrulewidth Error: LaTeX failed to compile RJwrapper.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See RJwrapper.log for more info. Execution halted ```
davidgohel commented 1 year ago

The format ignore rmarkdown::latex_dependency(). flextable's dependencies are using rmarkdown functions to make sure it's compliant with R Markdown documents.

% !TeX root = RJwrapper.tex
\title{}
\author{by }

\maketitle

It should contain something like:

\usepackage{multirow}
\usepackage{multicol}
\usepackage{colortbl}
\usepackage{hhline}
\newlength\Oldarrayrulewidth
\newlength\Oldtabcolsep
\usepackage{longtable}
\usepackage{array}
\usepackage{hyperref}
\usepackage{float}
\usepackage{wrapfig}

But they are not there, it explains why you have this error. You can fix the one you reported but then you will have to face other issues because of missing latex package ¯\(ツ)

From reading the documentation, it seems the package is supporting longtable and kable - it does not say other format can be used so I understand you should only use one of those 2.

Capture d’écran 2023-07-09 à 22 30 02
davidgohel commented 1 year ago

I closed the issue because I think rjtools does only accept longtable or kable

fawda123 commented 1 year ago

Thanks @davidgohel, adding the missing packages fixed the issue.

github-actions[bot] commented 8 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.