yonicd/texPreview

Tables are distorted after rendering

drfurtado opened this issue · 8 comments

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)
```

```{texpreview, echo = 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}
```

```{texpreview, echo = FALSE}
\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}

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:

  • texpreview.return = 'input' : returns an input call to the tex document pointing it to a self contained tex file that hosts the table
  • texpreview.path = 'imgs': path to the file (in this case ./imgs)
  • texpreview.stem = 'example': name of the file

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

  • texpreview.return = 'tex'

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)
```

```{texpreview, echo = FALSE,texpreview.return = 'input', texpreview.path = 'imgs',texpreview.stem = 'example'}
\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}
```

```{texpreview, echo = FALSE,texpreview.return = 'input', texpreview.path = 'imgs',texpreview.stem = 'example2'}
\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:

\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}

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

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)
```

```{r}
tex_return <- switch(knitr::opts_knit$get("rmarkdown.pandoc.to"),
  'html' = {
    tex_opts$set(opts.html = list(width = '75%', height = '75%'))
    'html'
    },
  'latex'   = 'input'
)

```

```{texpreview, echo = FALSE,texpreview.return = tex_return,texpreview.path = 'imgs',texpreview.stem = 'example'}
\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}
```

```{texpreview, echo = FALSE,texpreview.return = tex_return,texpreview.path = 'imgs',texpreview.stem = 'example2'}
\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}
```

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

How are you rendering the document?

How are you rendering the document?

Thanks for responding so quickly.

Please see gif here.

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’)

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!