yonicd / texPreview

Efficiently iterate, refine and share snippets of LaTeX in R with ease
https://yonicd.github.io/texPreview/
Other
52 stars 7 forks source link

texPreview cropping to viewer window, not scaling #43

Closed annacnev closed 4 years ago

annacnev commented 4 years ago

Description of the issue:

Texpreview is cropping the LaTeX output to the viewer window size when previewing and outputting .png files, which results in the preview being not representative of how the LaTeX would actually render in a standard pdf document generated by knitr/pdflatex. If I use texpreview to see this table in the viewer window, it appears as shown:

Screen Shot 2020-05-12 at 12 51 18 PM

Resizing the viewer window to be larger does not correct for the width being cropped:

Screen Shot 2020-05-12 at 12 51 32 PM

If I save use texPreview to save it as a png, it is still cropped: fruit_table

However, when I render a pdf using rmarkdown, my table is shown in full:

fruit_table_rmarkdown

Is there a way to implement scaling to viewer in texPreview rather than cropping so tables that fit standard page width but not necessarily viewer window width can appear in full?

Reproducible Code Snippet (Rmarkdown file)

---
title: "Fruit Table"
output: pdf_document
---

```{r message = FALSE}
library(mrggt)
library(tidyverse)
library(texPreview)
tex_opts$set(
  fileDir    = file.path("."), # path to save output
  imgFormat  = 'png' # return png images
  )
df <- dplyr::tribble( ~grpname, ~count, ~color,
                               'apple', 1, 'red',
                               'banana', 2, 'yellow',
                               'grape', 3, 'purple',
                               'pear', 4, 'green',
                               'orange', 5, 'orange')

tbl_gt <-
  gt(data = df) %>%
  tab_style(
    style = cell_fill(color = "#ffcccb"),
    locations = cells_body(
      columns = everything(),
      rows = startsWith(grpname, 'a')
    )
  ) %>%
  tab_style(
    style = cell_fill(color = "#ffffed"),
    locations = cells_body(
      columns = everything(),
      rows = startsWith(grpname, 'b')
    )
  ) %>%
  tab_style(
    style = cell_fill(color = "#b19cd9"),
    locations = cells_body(
      columns = everything(),
      rows = startsWith(grpname, 'g')
    )
  ) %>%
  tab_style(
        style = cell_fill(color = "#90ee90"),
        locations = cells_body(
          columns = everything(),
          rows = startsWith(grpname, 'p')
        )
   ) %>%
  tab_style(
    style = cell_fill(color = "#fed8b1"),
    locations = cells_body(
      columns = everything(),
      rows = startsWith(grpname, 'o')
    )
  ) %>%
  cols_label(.list = list(grpname = 'Fruit',
                          count = 'Total',
                          color = 'Color')) %>%
  tab_source_note(c('Source code: /scripts/fruit_table.R',
                    'Table code: /tables/fruit_table.tex')) %>%
  as_latex()
tbl_gt
build_pkgs <- purrr::map_chr(attributes(tbl_gt)$knit_meta, 
                             function(pkg){pkg$name})

tbl_gt %>% 
  as.character() %>%
  tex_preview(usrPackages = texPreview::build_usepackage(build_pkgs),
              stem = 'fruit_table')

**Session Info**

sessioninfo::session_info() ─ Session info ────────────────────────────────────────────────────────────────────────────────────── setting value
version R version 3.6.2 (2019-12-12) os macOS Catalina 10.15.2
system x86_64, darwin15.6.0
ui RStudio
language (EN)
collate en_US.UTF-8
ctype en_US.UTF-8
tz America/New_York
date 2020-05-12

─ Packages ────────────────────────────────────────────────────────────────────────────────────────── package version date lib source
assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0)
backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.0)
base64enc 0.1-3 2015-07-28 [1] CRAN (R 3.6.0)
broom 0.5.2 2019-04-07 [1] CRAN (R 3.6.0)
callr 3.3.2 2019-09-22 [1] CRAN (R 3.6.0)
cellranger 1.1.0 2016-07-27 [1] CRAN (R 3.6.0)
checkmate 1.9.4 2019-07-04 [1] CRAN (R 3.6.0)
cli 1.1.0 2019-03-19 [1] CRAN (R 3.6.0)
colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.0)
crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0)
digest 0.6.22 2019-10-21 [1] CRAN (R 3.6.1)
dplyr
0.8.3 2019-07-04 [1] CRAN (R 3.6.0)
evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.0)
forcats 0.4.0 2019-02-17 [1] CRAN (R 3.6.0)
generics 0.0.2 2018-11-29 [1] CRAN (R 3.6.0)
ggplot2
3.3.0 2020-03-05 [1] CRAN (R 3.6.0)
glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.0)
gridExtra 2.3 2017-09-09 [1] CRAN (R 3.6.0)
gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.0)
haven 2.1.1 2019-07-04 [1] CRAN (R 3.6.0)
hms 0.5.1 2019-08-23 [1] CRAN (R 3.6.0)
htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.0)
httr 1.4.1 2019-08-05 [1] CRAN (R 3.6.0)
inline 0.3.15 2018-05-18 [1] CRAN (R 3.6.0)
jsonlite 1.6 2018-12-07 [1] CRAN (R 3.6.0)
knitr 1.25 2019-09-18 [1] CRAN (R 3.6.0)
lattice 0.20-38 2018-11-04 [2] CRAN (R 3.6.2)
lifecycle 0.1.0 2019-08-01 [1] CRAN (R 3.6.0)
loo 2.1.0 2019-03-13 [1] CRAN (R 3.6.0)
lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.6.0)
magick 2.2 2019-08-26 [1] CRAN (R 3.6.0)
magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0)
matrixStats 0.55.0 2019-09-07 [1] CRAN (R 3.6.0)
modelr 0.1.5 2019-08-08 [1] CRAN (R 3.6.0)
mrggt 0.2.0 2020-05-08 [1] Github (metrumresearchgroup/mrggt@09a5544) munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.0)
nlme 3.1-142 2019-11-07 [2] CRAN (R 3.6.2)
pillar 1.4.2 2019-06-29 [1] CRAN (R 3.6.0)
pkgbuild 1.0.6 2019-10-09 [1] CRAN (R 3.6.0)
pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.0)
prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.6.0)
processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.0)
ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.0)
purrr
0.3.3 2019-10-18 [1] CRAN (R 3.6.0)
R6 2.4.0 2019-02-14 [1] CRAN (R 3.6.0)
Rcpp 1.0.2 2019-07-25 [1] CRAN (R 3.6.0)
readr 1.3.1 2018-12-21 [1] CRAN (R 3.6.0)
readxl 1.3.1 2019-03-13 [1] CRAN (R 3.6.0)
rematch2 2.1.0 2019-07-11 [1] CRAN (R 3.6.0)
rlang 0.4.0 2019-06-25 [1] CRAN (R 3.6.0)
rmarkdown 1.16 2019-10-01 [1] CRAN (R 3.6.0)
rstan 2.19.2 2019-07-09 [1] CRAN (R 3.6.0)
rstudioapi 0.10 2019-03-19 [1] CRAN (R 3.6.0)
rvest 0.3.4 2019-05-15 [1] CRAN (R 3.6.0)
scales 1.0.0 2018-08-09 [1] CRAN (R 3.6.0)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0)
StanHeaders 2.19.0 2019-09-07 [1] CRAN (R 3.6.0)
stringi 1.4.3 2019-03-12 [1] CRAN (R 3.6.0)
stringr
1.4.0 2019-02-10 [1] CRAN (R 3.6.0)
svgPanZoom 0.3.3 2016-09-26 [1] CRAN (R 3.6.0)
texPreview 1.3.2 2019-05-05 [1] CRAN (R 3.6.0)
tibble
2.1.3 2019-06-06 [1] CRAN (R 3.6.0)
tidyr 1.0.0 2019-09-11 [1] CRAN (R 3.6.0)
tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.6.0)
tidyverse
1.2.1 2017-11-14 [1] CRAN (R 3.6.0)
vctrs 0.2.0 2019-07-05 [1] CRAN (R 3.6.0)
whisker 0.4 2019-08-28 [1] CRAN (R 3.6.0)
withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0)
xfun 0.10 2019-10-01 [1] CRAN (R 3.6.0)
xml2 1.2.2 2019-08-09 [1] CRAN (R 3.6.0)
yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0)
zeallot 0.1.0 2018-01-28 [1] CRAN (R 3.6.0)

[1] /Users/AnnaN/rpkgs/3.6 [2] /Library/Frameworks/R.framework/Versions/3.6/Resources/library



Thank you!
dpastoor commented 4 years ago

@yonicd have you had a chance to look into this yet? Thanks

yonicd commented 4 years ago

can you paste in a comment the setting you have for tex_opts$get() in the session that you are running.

thanks

yonicd commented 4 years ago

the tex for the table is

> tbl_gt %>% 
+   as.character() %>%
+   cat(sep = '\n')
\definecolor{FFCCCB}{rgb}{1,0.8,0.796078431372549} 
\definecolor{FFFFED}{rgb}{1,1,0.929411764705882} 
\definecolor{B19CD9}{rgb}{0.694117647058824,0.611764705882353,0.850980392156863} 
\definecolor{90EE90}{rgb}{0.564705882352941,0.933333333333333,0.564705882352941} 
\definecolor{FED8B1}{rgb}{0.996078431372549,0.847058823529412,0.694117647058824} 
\setlength{\tabcolsep}{3pt}
\captionsetup[table]{labelformat=empty,skip=0pt}
\begin{longtable}{p{6.05333333333333cm}p{5.58333333333333cm}p{5.96333333333333cm}} 
\toprule
Fruit & Total & Color \\ 
\midrule
\cellcolor{FFCCCB}{apple} & \cellcolor{FFCCCB}{1} & \cellcolor{FFCCCB}{red} \\ 
\cellcolor{FFFFED}{banana} & \cellcolor{FFFFED}{2} & \cellcolor{FFFFED}{yellow} \\ 
\cellcolor{B19CD9}{grape} & \cellcolor{B19CD9}{3} & \cellcolor{B19CD9}{purple} \\ 
\cellcolor{90EE90}{pear} & \cellcolor{90EE90}{4} & \cellcolor{90EE90}{green} \\ 
\cellcolor{FED8B1}{orange} & \cellcolor{FED8B1}{5} & \cellcolor{FED8B1}{orange} \\ 
\bottomrule
\\ 
 \begin{minipage}{18cm}
Source code: /scripts/fruit\_table.R\\ 
Table code: /tables/fruit\_table.tex\\ 
\end{minipage}
\end{longtable}

I am not sure how to control the width of the columns that are pretty wide and go beyond what the template margins are.

\begin{longtable}{p{6.05333333333333cm}p{5.58333333333333cm}p{5.96333333333333cm}}

if you set the right margin of the template document then the table is captured properly

tbl_gt %>% 
  as.character() %>%
  cat(sep = '\n')
  tex_preview(usrPackages = texPreview::build_usepackage(build_pkgs),
              stem = 'fruit_table',margin = my_margins)

image

resize isnt catching because texpreview doesn't support longtable because the premise is to have a table that fits on a single page and that is why in the package template a standalone document, where the assumption of using longtable is to go beyond a single page.

If there is a native way within the tex generating package to add resize box that may also probably fix the problem.

An aside, I would be cautious of overloading the packages in the document. There are duplicates in the preloaded packages and what you are adding with usrPackages

build_pkgs <- purrr::map_chr(attributes(tbl_gt)$knit_meta, 
                              function(pkg){pkg$name})

intersect(build_pkgs,texPreview::tex_requirements())
# [1] "amsmath"  "booktabs" "caption"  "xcolor"   "array"  

using setdiff would be cleaner

build_pkgs <- setdiff(build_pkgs,texPreview::tex_requirements())
build_pkgs
# [1] "longtable" "amssymb"   "color"     "colortbl"  "mathptmx"  "tikz"   
dpastoor commented 4 years ago

Thanks - so correct my "short" answer if this is not precise - all things considered, by manually setting margins we can get a passable behavior within the viewer, however mrggt with the longtable config is at odds with some of the design decisions, so that could still only take us so far, esp if the generated table would want to spill off the single page. As such it's like advise-able to provide an alternative pattern for those looking to preview a table as it would look in a report given the longable environment

yonicd commented 4 years ago

second part first, texpreview was not designed to be used for longtables. so that would need it's own preview vehicle.

first part, in my opinion manual margins should be a last resort. Having a simple mechanism to invoke resize/scalebox to the generated tex that is passed into texpreview would be easier for the end user and a more robust solution because you don't need to calculate what the right margin needs to be for every table.

yonicd commented 4 years ago

It also looks like to use minipage with standalone documents more specifications need to be set to get the footnote to be justified correctly.

https://tex.stackexchange.com/a/103111/43246

annacnev commented 4 years ago

@yonicd hey Yonni! So I was looking into this because I wanted to incorporate texPreview into our mrggt fork for rendering previews and .pngs within the package. Right now, we have it set so users can specify the margins and page size and it will print a table optimized to that available length (available length = page size - left margin - right margin)... but unfortunately the default size for latex document class preview is not really giving me much leeway to work with in terms of having these packages go hand and hand.

I did some digging and there's a varwidth argument to the document class of preview. So I am thinking that maybe texPreview default could be whatever you have it set to now, and we could figure out some way to add this to the preamble (presumably through an optional argument)? That way mrggt and texPreview are really synergetic and easy for our scientists to use!

Here's what the declaration in the preamble would need to look like:

\documentclass[preview, varwidth=x]{standalone}

with x being something mrggt could specify in the texPreview call.

Also, I can change the minipage usage to something else (likely a multicolumn that spans the entire table) easily! That was something forked from the og gt that I am not a huge fan of... so I have been looking to change it out, but just hasn't been a priority since right now it works fine.

yonicd commented 4 years ago

It seems like there are better options per the standalone documentation,

sections 4.2 and 5.2

\documentclass[crop]{standalone}

where the border by default is set to 0 (which is good)

@dpastoor I also see there is a multi option which would accommodate the longtable outputs.

annacnev commented 4 years ago

@yonicd Hey Yonni, yea whatever you think is best- I am not super familiar with standalone, I just found the varwidth option while briefing the docs. If you come up with a solution that you want me to test with mrggt let me know! Thank you so much!

yonicd commented 4 years ago

closing issue. original question was answered.