Flextable seems to be incompatible with multicol LaTex package

Viewed 225

When running the following rmardown code

```{r echo=FALSE}
secondc <- c("X.*","X",NA,"X",NA,NA)
thrirdc <- c("Y","Y",NA,"X",'Y',NA)

tabela <- data.frame(firstc,secondc, thrirdc)
ft <- flextable(tabela)
ft <- access(ft)
ft <- hline_top(ft)

ft <- fit_to_width(ft, max_width = 2.2)
ft <- align(ft, i = 4, j=2:ncol(tabela), align = "center")
ft <- set_table_properties(ft, layout = "autofit", width = 1)
```

\begin{enumerate}
\item My first line.
\begin{multicols}{2}
\begin{enumerate}
\item `r ft`
\item `r ft`
\item `r ft`
\item `r ft`
\end{enumerate}
\end{multicols}
\end{enumerate}

I get the following error:

output file: flextable.knit.md

! Package longtable Error: longtable not in 1-column mode.

Error: LaTeX failed to compile flextable.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See flextable.log for more info. Execution halted

1 Answers

Flextable appears to have several weaknesses when the output is a PDF document. My question, which I am now answering, is an example of one of those weaknesses.

One idea to get around these congenital Flextable weaknesses is to always use HTML as output and then use Pagedown package to convert the table to PDF, subsequently inserting the table into the document as an image.

Since the PDF produced by Pagedown package is vector-based, then no graphical quality is lost.

Below is an example of how to get a table created by Flextable into a high quality PDF. The last 3 lines of code could be encapsulated in a function of Flextable package (similarly to quick_pdf function of Huxtable package).

It's a shame that Flextable works so limitedly with PDF and at the same time has a superb syntax!

library(flextable)
library(pagedown)

m <- rbind(
  c("A","B","C"),
  c("D","E","F")
)

dados <- as.data.frame(m)

ft <- flextable(dados)
ft <- theme_box(ft)

save_as_html(ft,path = "/tmp/lixo.html")
chrome_print("/tmp/lixo.html","/tmp/lixo.pdf")

system("cd /tmp; pdfcrop lixo.pdf")
Related