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

Tables are distorted after rendering #49

Closed drfurtado closed 3 years ago

drfurtado commented 3 years ago

I've encountered an issue that I don't seem to find the solution for.

Although for some tables it renders fine, for other tables it comes up disproportional. Any help would be greatly appreciated.

Screenshots This screenshot shows two tables. The top one is fine but the bottom one did not render correctly. It is stretched to fit the space.

The bottom table from the link above should look like this.

Rmd file:

---
title: "test"
date: "`r Sys.Date()`"
output:
  pdf_document: default
---

```{r warning=TRUE}
library(texPreview)
\begin{table}[h]
    \centering
    \caption{Independent Samples T-Test}
    \label{tab:independentSamplesT-Test}
    {
        \begin{tabular}{lrrrrrr}
            \toprule
            \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{2}{c}{95\% CI for Cohen's d} \\
            \cline{6-7}
              & t & df & p & Cohen's d & Lower & Upper  \\
            \cmidrule[0.4pt]{1-7}
            engagement & 2.365 & 38 & 0.023 & 0.748 & 0.101 & 1.385  \\
            \bottomrule
            % \addlinespace[1ex]
            % \multicolumn{7}{p{0.5\linewidth}}{\textit{Note.} Student's t-test.} \\
        \end{tabular}
    }
\end{table}
\begin{table}[h]
    \centering
    \caption{Independent Samples T-Test}
    \label{tab:independentSamplesT-Test}
    {
        \begin{tabular}{lrrr}
            \toprule
              & W & df & p  \\
            \cmidrule[0.4pt]{1-4}
            gpa & 5482.500 &  & 0.598  \\
            \bottomrule
            % \addlinespace[1ex]
            % \multicolumn{4}{p{0.5\linewidth}}{\textit{Note.} Mann-Whitney U test.} \\
        \end{tabular}
    }
\end{table}
yonicd commented 3 years ago

tldr: because texpreview is built for previewing in the internal viewer there is by default a resizebox around the tables it creates. to switch this off you can set it globally for the document using tex_opts$set(resizebox=FALSE)

longer answer: there is some documentation for using the texpreview engine with pdf outputs here: https://yonicd.github.io/texPreview/articles/engine.html#pdf-documents

I rewrote your doc to reflect such a setup,

the definitions for the chunks are doing the following:

I tend to like this setup because each table is then self contained and you can compartmentalize them for collaboration and documentation adding src footnotes to where the table is located for example.

if you do not want self contained tables you can just do

and the tables will be embedded into the doc without input and texpreview will use the tempdir for managing the tables.

---
title: "test"
date: "`r Sys.Date()`"
output:
  pdf_document:
     keep_tex: true
     extra_dependencies: ["booktabs"]
---

```{r warning=TRUE}
library(texPreview)
tex_opts$set(resizebox = FALSE)
\begin{table}[h]
    \centering
    \caption{Independent Samples T-Test}
    \label{tab:independentSamplesT-Test}
    {
        \begin{tabular}{lrrrrrr}
            \toprule
            \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{2}{c}{95\% CI for Cohen's d} \\
            \cline{6-7}
              & t & df & p & Cohen's d & Lower & Upper  \\
            \cmidrule[0.4pt]{1-7}
            engagement & 2.365 & 38 & 0.023 & 0.748 & 0.101 & 1.385  \\
            \bottomrule
            % \addlinespace[1ex]
            % \multicolumn{7}{p{0.5\linewidth}}{\textit{Note.} Student's t-test.} \\
        \end{tabular}
    }
\end{table}
\begin{table}[h]
    \centering
    \caption{Independent Samples T-Test}
    \label{tab:independentSamplesT-Test2}
    {
        \begin{tabular}{lrrr}
            \toprule
              & W & df & p  \\
            \cmidrule[0.4pt]{1-4}
            gpa & 5482.500 &  & 0.598  \\
            \bottomrule
            % \addlinespace[1ex]
            % \multicolumn{4}{p{0.5\linewidth}}{\textit{Note.} Mann-Whitney U test.} \\
        \end{tabular}
    }
\end{table}

the tex file will look like this:

```tex
\title{test}
\author{}
\date{\vspace{-2.5em}2021-02-18}

\begin{document}
\maketitle

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(texPreview)}
\NormalTok{tex\_opts}\SpecialCharTok{$}\FunctionTok{set}\NormalTok{(}\AttributeTok{resizebox =} \ConstantTok{FALSE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\input{imgs/example.tex}

\input{imgs/example2.tex}

\end{document}
drfurtado commented 3 years ago

Thank you so much. It worked as described. For some reason, the tables do not show when rendering as HTML. Please see yaml header below. Any ideas on how to fix it?

I could not find a solution reading the content of the link you sent in the previous message. Thanks in advance.

title: "test"
date: "`r Sys.Date()`"
output:
  html_document:
    df_print: paged
  pdf_document:
    keep_tex: yes
    extra_dependencies: booktabs
yonicd commented 3 years ago

for rendering varying output types you need to infer which output type is being currently rendered.

when it is html you need to switch the returnType of texPreview to 'html' and you can control the figure size with html.opts.

see example below where tex_return is controlling the chunk option for the returnType.

---
title: "test"
date: "`r Sys.Date()`"
output:
  html_document:
    df_print: paged
  pdf_document:
     keep_tex: true
     extra_dependencies: ["booktabs"]
---

```{r warning=TRUE}
library(texPreview)
tex_opts$set(resizebox = FALSE)
tex_return <- switch(knitr::opts_knit$get("rmarkdown.pandoc.to"),
  'html' = {
    tex_opts$set(opts.html = list(width = '75%', height = '75%'))
    'html'
    },
  'latex'   = 'input'
)
\begin{table}[h]
    \centering
    \caption{Independent Samples T-Test}
    \label{tab:independentSamplesT-Test}
    {
        \begin{tabular}{lrrrrrr}
            \toprule
            \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{2}{c}{95\% CI for Cohen's d} \\
            \cline{6-7}
              & t & df & p & Cohen's d & Lower & Upper  \\
            \cmidrule[0.4pt]{1-7}
            engagement & 2.365 & 38 & 0.023 & 0.748 & 0.101 & 1.385  \\
            \bottomrule
            % \addlinespace[1ex]
            % \multicolumn{7}{p{0.5\linewidth}}{\textit{Note.} Student's t-test.} \\
        \end{tabular}
    }
\end{table}
\begin{table}[h]
    \centering
    \caption{Independent Samples T-Test}
    \label{tab:independentSamplesT-Test2}
    {
        \begin{tabular}{lrrr}
            \toprule
              & W & df & p  \\
            \cmidrule[0.4pt]{1-4}
            gpa & 5482.500 &  & 0.598  \\
            \bottomrule
            % \addlinespace[1ex]
            % \multicolumn{4}{p{0.5\linewidth}}{\textit{Note.} Mann-Whitney U test.} \\
        \end{tabular}
    }
\end{table}
drfurtado commented 3 years ago

Thanks a lot for the quick response. I tried the solution above and it's giving the following error:

Error in switch(knitr::opts_knit$get("rmarkdown.pandoc.to"), html = { : 
  EXPR must be a length 1 vector
yonicd commented 3 years ago

How are you rendering the document?

drfurtado commented 3 years ago

How are you rendering the document?

Thanks for responding so quickly.

Please see gif here.

yonicd commented 3 years ago

In the gif you did not replace the chunk option with texpreview.return = tex_return it is still ‘input’

you can run both at once by running in the console

rmarkdown::render(path_to_file,output=‘all’)

drfurtado commented 3 years ago

In the gif you did not replace the chunk option with texpreview.return = tex_return it is still ‘input’

you can run both at once by running in the console

rmarkdown::render(path_to_file,output=‘all’)

My apologies; I followed your instructions and it worked as described.

Thank you for your help!