davidgohel / flextable

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

hline chips away part of vertical lines within table #490

Closed ltcamara closed 1 year ago

ltcamara commented 1 year ago

The following RMarkdown example reproduces the problem that I have. It can directly be used and encircles the problem I am facing in my current use case.

---
title: "Display issues with hline translation to LaTex"
header-includes:
  \usepackage{picture}
output:
  pdf_document:
    latex_engine: xelatex
---
\begin{picture}(0, 0)(0, 0)
  \put(125, -159){\circle{10cm}}
  \put(180, -159){\circle{10cm}}
  \put(290, -159){\circle{10cm}}
  \put(345, -159){\circle{10cm}}
\end{picture}

```{r setup, include=FALSE}
library(flextable)
library(dplyr)
# Please set your directory here
setwd("SET_YOUR_DIRECTORY_HERE")

data_test <- flextable(mtcars[1:20, 1:6])

data_test <- data_test %>%
  fontsize(size = 9, part = "all") %>%
  hline_bottom(part = "header") %>% # add the header bottom line
  border_inner_v(part = "all") # add vertical lines

data_test <- data_test %>%
  hline(i = 7, j = 4, part = "body",
        border = officer::fp_border("red"))
\``` <----- eliminate escape to run

```{r, ft.align = "center", message=FALSE, echo=FALSE}
data_test
\``` <----- eliminate escape to run

Description of the problem Since flextable translates hline into the "\hhline" command of LaTex, it does it by making use of the tilde "~" for cells without a line and for the cell with a line it assigns the corresponding element. In my case, this translates to the following, in LaTex: \hhline{~~~>{\arrayrulecolor[HTML]{FF0000}\global\arrayrulewidth=1pt}-~~} The issue: It chips away vertical lines from the other columns as the circles in the executed markdown illustrate.

image

Potential solution If the lines are converted to \hhline{~|~|~>{\arrayrulecolor[HTML]{FF0000}\global\arrayrulewidth=1pt}-|~|~} the problem will go away. Issue here: it needs to be known where the "|" must be set.

**Session info**
> sessionInfo()
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rmarkdown_2.14

loaded via a namespace (and not attached):
 [1] rstudioapi_0.13  xml2_1.3.3       knitr_1.39       magrittr_2.0.3   uuid_1.1-0       tidyselect_1.1.2
 [7] munsell_0.5.0    colorspace_2.0-3 R6_2.5.1         rlang_1.0.6      fastmap_1.1.0    fansi_1.0.3     
[13] dplyr_1.0.9      tools_4.2.2      grid_4.2.2       gtable_0.3.0     xfun_0.31        utf8_1.2.2      
[19] cli_3.4.1        htmltools_0.5.2  ellipsis_0.3.2   yaml_2.3.5       digest_0.6.29    tibble_3.1.7    
[25] lifecycle_1.0.1  crayon_1.5.1     zip_2.2.0        officer_0.4.4    purrr_0.3.4      ggplot2_3.3.6   
[31] vctrs_0.4.1      glue_1.6.2       evaluate_0.15    compiler_4.2.2   pillar_1.7.0     generics_0.1.2  
[37] scales_1.2.0     pkgconfig_2.0.3 
davidgohel commented 1 year ago

Thanks a lot, that is very useful - I will try to fix soon.

ltcamara commented 1 year ago

I just had another idea, that may be helpful. Maybe \cline is easier to adapt than \hhline for this? Just an idea.

ltcamara commented 1 year ago

Exchanging the relevant line in the generated .tex code:

Screenshot from 2022-12-03 17-08-36
leads to the desired result:

Screenshot from 2022-12-03 17-09-04

davidgohel commented 1 year ago

It should be fixed now. It was hard. Thanks for the clues you gave, it was very helpful.

Capture d’écran 2023-01-10 à 23 13 30

However, now many display issues related to 'flextable' and latex borders are fixed (and it feels good).

One issue remains with your test case when the table has a background color. I hope to fix it later, not sure if I can.

Capture d’écran 2023-01-10 à 23 16 27
ltcamara commented 1 year ago

Thanks a lot, David.

github-actions[bot] commented 1 year 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.