Making latex multicolumn width wider in Rmarkdown - r

I want to make my LaTeX table (picture below) in Rmarkdown to the full width(so that it fits the caption), which requires the 2ndto 5th columns to be extended. I went through multiple links(one,two) but failed. How can I make the table to the full width, and if the columns 2- have the same width, it would be better.
Here is the code
Preamble:
---
title: "misc"
author: "Me"
date: "`r Sys.Date()`"
output:
pdf_document:
keep_tex: true
extra_dependencies: caption
number_sections: yes
fig_caption: yes
classoption: table
header-includes:
- \usepackage{color}
- \usepackage{xcolor}
- \usepackage{float}
- \usepackage{framed}
- \usepackage{fontawesome}
- \usepackage{caption}
- \usepackage{multirow}
- \usepackage{caption}
- \usepackage{tabularx}
- \usepackage{array}
language:
label:
fig: !expr function(x) sprintf("**Figure %s.** ", x)
fontsize: 11pt
urlcolor: blue
---
LaTeX:
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tabular}{|
>{\columncolor[HTML]{656565}}l |lcccc|}
\hline
\multicolumn{1}{|c|}{\cellcolor[HTML]{FFFFFF}{\color[HTML]{FFFFFF} }} & \multicolumn{5}{c|}{\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF} Type of Distribution}} \\ \hline
\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF} } & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{Lognormal} & \multicolumn{1}{l|}{Exponential} & \multicolumn{1}{l|}{Gamma} & \multicolumn{1}{l|}{Weibull} \\ \cline{2-6}
\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF} } & \multicolumn{1}{l|}{Box-Cox} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{\cellcolor[HTML]{9B9B9B}\textbf{Y}} & \cellcolor[HTML]{9B9B9B}\textbf{Y} \\ \cline{2-6}
\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF} } & \multicolumn{1}{l|}{Exponential} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{Y} & Y \\ \cline{2-6}
\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF} } & \multicolumn{1}{l|}{Simple power} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{\cellcolor[HTML]{9B9B9B}\textbf{Y}} & \multicolumn{1}{c|}{\cellcolor[HTML]{9B9B9B}\textbf{Y}} & \cellcolor[HTML]{9B9B9B}\textbf{Y} \\ \cline{2-6}
\multirow{-5}{*}{\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF} \begin{tabular}[c]{#{}l#{}}Type of Transformation\end{tabular}}} & \multicolumn{1}{l|}{Logarithmic} & \multicolumn{1}{c|}{\cellcolor[HTML]{9B9B9B}\textbf{Y}} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{\textbf{N}} & \textbf{N} \\ \hline
\end{tabular}
\end{table}
Edited to include the code without markups:
\begin{table}[]
\begin{tabular}{|l|lcccc|}
\hline
\multicolumn{1}{|c|}{} & \multicolumn{5}{c|}{Type of Distribution} \\ \hline
\multirow{5}{*}{\begin{tabular}[c]{#{}l#{}}Type of\\ transformation\end{tabular}} & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{Lognormal} & \multicolumn{1}{l|}{Exponential} & \multicolumn{1}{l|}{Gamma} & \multicolumn{1}{l|}{Weibull} \\ \cline{2-6}
& \multicolumn{1}{l|}{Box-Cox} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{\textbf{Y}} & \textbf{Y} \\ \cline{2-6}
& \multicolumn{1}{l|}{Exponential} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{Y} & Y \\ \cline{2-6}
& \multicolumn{1}{l|}{Simple power} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{\textbf{Y}} & \multicolumn{1}{c|}{\textbf{Y}} & \textbf{Y} \\ \cline{2-6}
& \multicolumn{1}{l|}{Logarithmic} & \multicolumn{1}{c|}{\textbf{Y}} & \multicolumn{1}{c|}{Y} & \multicolumn{1}{c|}{\textbf{N}} & \textbf{N} \\ \hline
\end{tabular}
\end{table}

As said before, I would use the tabularray package. This will give a much nicer result. No white gaps between cells, better spacing ...
---
title: "misc"
author: "Me"
date: "`r Sys.Date()`"
output:
pdf_document:
keep_tex: true
extra_dependencies: caption
number_sections: yes
fig_caption: yes
classoption: table
header-includes:
- \usepackage{tabularray}
language:
label:
fig: !expr function(x) sprintf("**Figure %s.** ", x)
fontsize: 11pt
urlcolor: blue
---
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tblr}{
colspec={XXXXXX},
vlines,
hlines,
cells={halign=c},
column{1-2}={halign=l},
vspan=even,
cell{1}{2}={halign=c,bg=gray,fg=white},
cell{2}{1}={bg=gray,fg=white},
cell{3}{5-6}={bg=lightgray,font=\bfseries},
cell{5}{4-6}={bg=lightgray,font=\bfseries},
cell{6}{3}={bg=lightgray,font=\bfseries},
}
& \SetCell[c=5]{} Type of Distribution & & & &\\
\SetCell[r=5]{} Type of Transformation & & Lognormal & Exponential & Gamma & Weibull \\
& Box-Cox & Y & Y & Y & Y \\
& Exponential & Y & Y & Y & Y\\
& Simple power & Y & Y & Y & Y \\
& Logarithmic & Y & Y & N & N \\
\end{tblr}
\end{table}

Related

referencing latex table in quarto

I have the following table in my quarto document and I would like to reference it automatically in my text.
Quarto understands that this is my second table and is able to name it Table 2 in the caption but I can't find a way to reference this table automatically in my text.
I have this line in my YAML to make the table work.
---
format: pdf
header-includes:
- \usepackage{multirow}
---
```{=latex}
\begin{table}[]
\caption{Summary of the different variabilities when comparing marks from same or different tools and zones}
\label{Variabilities}
\begin{center}
\begin{tabular}{ll|cc|}
\cline{3-4}
& & \multicolumn{2}{c|}{\textbf{Tool}} \\ \cline{3-4}
& & \multicolumn{1}{l|}{\textit{Same}} & \multicolumn{1}{l|}{\textit{Different}} \\ \hline
\multicolumn{1}{|c|}{\multirow{2}{*}{\textbf{Zone}}} & \textit{Same} & \multicolumn{1}{c|}{W} & B \\ \cline{2-4}
\multicolumn{1}{|c|}{} & \textit{Different} & \multicolumn{1}{c|}{B} & B \\ \hline
\end{tabular}
\end{center}
\end{table}
```
If anyone knows how to give this table a label that I can call in the text for referencing.
HERE IS THE ANSWER TO THIS
Two more lines have to be introduced to the YAML in order to use the package cleveref
---
format: pdf
header-includes:
- \usepackage{multirow}
- \usepackage{hyperref}
- \usepackage[capitalise,noabbrev]{cleveref}
---
The way the table is written does not change.
To reference the table in the text as "Table X", you have to write
\ref{Variabilities}
(because I have \label{Variabilities} in my table).
Thanks #samcarter_is_at_topanswers.xyz for your help
You can use the \cref{Variabilities} from the cleveref package:
\documentclass{article}
\usepackage{multirow}
\usepackage[capitalise,noabbrev]{cleveref}
\begin{document}
\begin{table}[]
\caption{Summary of the different variabilities when comparing marks from same or different tools and zones}
\label{Variabilities}
\begin{center}
\begin{tabular}{ll|cc|}
\cline{3-4}
& & \multicolumn{2}{c|}{\textbf{Tool}} \\ \cline{3-4}
& & \multicolumn{1}{l|}{\textit{Same}} & \multicolumn{1}{l|}{\textit{Different}} \\ \hline
\multicolumn{1}{|c|}{\multirow{2}{*}{\textbf{Zone}}} & \textit{Same} & \multicolumn{1}{c|}{W} & B \\ \cline{2-4}
\multicolumn{1}{|c|}{} & \textit{Different} & \multicolumn{1}{c|}{B} & B \\ \hline
\end{tabular}
\end{center}
\end{table}
\cref{Variabilities}
\end{document}
Two more lines have to be introduced to the YAML in order to use the package cleveref
---
format: pdf
header-includes:
- \usepackage{multirow}
- \usepackage{hyperref}
- \usepackage[capitalise,noabbrev]{cleveref}
---
The way the table is written does not change. To reference the table in the text as "Table X", you have to write
\ref{Variabilities}
(because I have \label{Variabilities} in my table).
Thanks #samcarter_is_at_topanswers.xyz for your help

R markdown running tex file error regarding toprule

I have the following chunk of codes in markdown file
---
title: "Ag Productivity"
author: "Abdullah Mamun"
date: '2022-06-23'
output:
pdf_document:
includes:
in_header: "table1.tex"
---
and in .tex file I have this
\begin{document}
\begin{table}
\centering
\begin{tabular}[t]{lccc}
\toprule
& Model 1 & Model 2 & Model 3\\
\midrule
lag(YGrowth, 1) & \num{1.000}(\num{0.000})*** & \num{-0.347}(\num{0.030})*** & \num{-0.436}(\num{0.068})***\\
lag(Inputs, 1) & \num{0.000}(\num{0.000}) & \num{-0.100}(\num{0.127}) & \num{2.321}(\num{1.078})*\\
lag(GDPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.270}(\num{0.069})*** & \num{0.341}(\num{0.531})\\
lag(lnPDENS, 1) & \num{0.000}(\num{0.000}) & \num{-0.124}(\num{0.031})*** & \num{0.158}(\num{0.076})*\\
lag(GRRAT, 1) & \num{0.000}(\num{0.000}) & \num{-0.280}(\num{0.095})** & \num{0.858}(\num{0.378})*\\
lag(EMPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.317}(\num{0.138})* & \num{0.708}(\num{1.571})\\
\midrule
Num.Obs. & \num{1070} & \num{1025} & \\
R2 & \num{1.000} & \num{0.141} & \\
R2 Adj. & \num{1.000} & \num{0.076} & \\
AIC & \num{-79950.8} & \num{-2904.1} & \\
BIC & \num{-79781.6} & \num{-2911.2} & \\
RMSE & \num{0.00} & \num{0.06} & \\
\bottomrule
\end{tabular}
\end{table}
I get this error message:
! Undefined control sequence. l.106 \toprule
Appreciate your help.
the table does not belong in the header. You should move it to the body of the document. Even though you try to force the body to start early with \begin{document}, this won't work because rmarkdown will include preamble-only stuff after this
if you want to use special macros like \toprule or \num you must load the package which provide them
your table is missing a floating specifier that tells latex where the table can be placed, I suggest [htbp
rmarkdown file:
---
title: "Ag Productivity"
author: "Abdullah Mamun"
date: '2022-06-23'
output:
pdf_document:
keep_tex: true
header-includes:
- \usepackage{booktabs}
- \usepackage{siunitx}
---
\include{table1}
table1.tex:
\begin{table}[htbp]
\centering
\begin{tabular}[t]{lccc}
\toprule
& Model 1 & Model 2 & Model 3\\
\midrule
lag(YGrowth, 1) & \num{1.000}(\num{0.000})*** & \num{-0.347}(\num{0.030})*** & \num{-0.436}(\num{0.068})***\\
lag(Inputs, 1) & \num{0.000}(\num{0.000}) & \num{-0.100}(\num{0.127}) & \num{2.321}(\num{1.078})*\\
lag(GDPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.270}(\num{0.069})*** & \num{0.341}(\num{0.531})\\
lag(lnPDENS, 1) & \num{0.000}(\num{0.000}) & \num{-0.124}(\num{0.031})*** & \num{0.158}(\num{0.076})*\\
lag(GRRAT, 1) & \num{0.000}(\num{0.000}) & \num{-0.280}(\num{0.095})** & \num{0.858}(\num{0.378})*\\
lag(EMPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.317}(\num{0.138})* & \num{0.708}(\num{1.571})\\
\midrule
Num.Obs. & \num{1070} & \num{1025} & \\
R2 & \num{1.000} & \num{0.141} & \\
R2 Adj. & \num{1.000} & \num{0.076} & \\
AIC & \num{-79950.8} & \num{-2904.1} & \\
BIC & \num{-79781.6} & \num{-2911.2} & \\
RMSE & \num{0.00} & \num{0.06} & \\
\bottomrule
\end{tabular}
\end{table}

Equation in a table in R

I want a table like this and make it as a kable so I can make footnote:
but I didn't manage so I made a table latex like this :
$$
\begin{array}{lccccc}
\hline
{Copula} & {Distribution} & {Parameter range} & {Complete dependence} & {Independence} \\
\hline
{Normal} & {C_{Normal} (u_1,u_2,\rho)=\Phi_{\rho}(\Phi^{-1}(u_1),\Phi^{-1}(u_2))} & {\rho\in(-1,1)} & {\rho=1,or -1} & {\rho=0} \\
\hline
{Student-t} & {C_t (u_1,u_2;\rho,d)=t_{\rho,d}(t^{-1}_d(u_1),t^{-1}_d(u_2))} & {\rho\in(-1,1)} & {\rho=1,or -1} & {\rho=0} \\
\hline
{Gumbel} & {C_{Gumbel} (u_1,u_2,\beta)=exp\{-[(-ln(u_1))^{\frac{1}{\beta}}+(-ln(u_2))^{\frac{1}{\beta}}]^{\beta}\}} & {\beta\in(0,1)} & {\beta=0} & {\beta=1} \\
\hline
{RG} & {C_{RG} (u_1,u_2,\alpha)=u_1+u_2-1+C_{Gumbel} (1-u_1,1-u_2,\alpha)} & {\alpha\in[1,\infty)} & {\alpha\rightarrow\infty} & {\alpha=1} \\
\hline
{Clayton} & {C_{Clayton} (u_1,u_2,\theta)=max((u_1^{-\theta}+u_2^{-\theta}-1)^{-\frac{1}{\theta}},0)} & {\theta\in[-1,\infty)\{0\}} & {\theta\rightarrow\infty} & {\theta \rightarrow 0} \\
\hline
{RC} & {C_{RC} (u_1,u_2,\alpha)=u_1+u_2-1+C_{Clayton} (1-u_1,1-u_2,\theta)} & {\theta\in[-1,\infty)\{0\}} & {\theta\rightarrow\infty} & {\theta \rightarrow 0} \\
\hline
\end{array}
$$
and it looks like this:
It's not bad until I found it's not easy to add footnotes since I used to make kable.
Is there any way I can add a footnote for it? or how can I convert it into a table so I can use kable to display it?

Knitr chunk plots within a latex table - size issues

I am writing a report within R studio using the knitr package. I am trying to succinctly describe a list of variables and there inclusion in an analysis. In order to do this I am using the long table package within latex. I have inserted a chunk into one of the table columns in order to display a histogram of some of the data. The issue is that this disrupts the width of the table column. I have tried setting the table width within the latex code but something is happening within the chunk that is over riding this.
\documentclass{article}
\usepackage{float}
\usepackage{longtable}
\begin{document}
<<ch-req, echo=FALSE,results='hide',include=FALSE>>=
require(datasets)
#
\begin{center}
\begin{longtable}{|c|p{.45\textwidth}|c|}
\caption{Summary table for variables in 'cars' data set} \label{tab:csumTab} \\
\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} & \multicolumn{1}{c|}{\textbf{Variable Summary}} & \multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline\hline
\endfirsthead
\multicolumn{3}{c}%
{{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} &
\multicolumn{1}{c|}{\textbf{Variable Summary}} &
\multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline
\endhead
\hline \multicolumn{3}{r}{{Continued on next page}} \\
\endfoot
\hline \hline
\endlastfoot
`speed' & numeric Speed (mph) of the car & \\
&
\begin{figure}[H]
<<histspeed1,echo=FALSE, fig=TRUE,out.width='2in', fig.align = 'left'>>=
hist(cars$speed)
#
\end{figure}
& \\
\hline
`dist' & This is the numeric stopping distance of the car (ft)& `Distance'\\
& \begin{tabular}{|c|c|c|}
\hline
min & median & max \\
\hline
2 & 36 & 120 \\
\hline
\end{tabular}& \\
\end{longtable}
\end{center}
\begin{center}
\begin{longtable}{|c|p{.45\textwidth}|c|}
\caption{Summary table for variables in `cars' data set} \label{tab:csumTab} \\
\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} & \multicolumn{1}{c|}{\textbf{Variable Summary}} & \multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline\hline
\endfirsthead
\multicolumn{3}{c}%
{{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} &
\multicolumn{1}{c|}{\textbf{Variable Summary}} &
\multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline
\endhead
\hline \multicolumn{3}{r}{{Continued on next page}} \\
\endfoot
\hline \hline
\endlastfoot
`speed' & numeric Speed (mph) of the car & \\
&
\begin{figure}[H]
%<<histspeed2,echo=FALSE, fig=TRUE,out.width='2in', fig.align = 'left'>>=
%hist(cars$speed)
%#
\end{figure}
& \\
\hline
`dist' & This is the numeric stopping distance of the car (ft)& `Distance'\\
& \begin{tabular}{|c|c|c|}
\hline
min & median & max \\
\hline
2 & 36 & 120 \\
\hline
\end{tabular}& \\
\end{longtable}
\end{center}
\end{document}
There are two tables just to show that the table size does work when the plot is not incorporated in the code. I have used out.width='2in' to change the size of the histogram in the table, but there is still a large area reserved for the plot by default(?). You can see this more clearly if you change fig.align = 'left' to fig.align = 'center'. I want the table to look how it does in the second example but with the plot incorporated.
Any help greatly appreciated.

R markdown won't compile a piece of LaTeX code

I have the following piece of code, which I would like to run I R markdown. I have tried to run it in a LaTeX compiler, which works absolutely fine. But when I add it into R markdown it won't compile the piece of code.
---
title: "Titre"
date: Fecha
output:
pdf_document:
keep_tex: true
includes:
in_header: mystyle.sty
---
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{#{\extracolsep{5pt}}lD{.}{.}{-3} }
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{1}{c}{\textit{Dependent variable:}} \\
\cline{2-2}
\\[-1.8ex] & \multicolumn{1}{c}{mpg} \\
\hline \\[-1.8ex]
cyl & -2.876^{***} \\
& (0.322) \\
& \\
Constant & 37.885^{***} \\
& (2.074) \\
& \\
\hline \\[-1.8ex]
Observations & \multicolumn{1}{c}{32} \\
R$^{2}$ & \multicolumn{1}{c}{0.726} \\
Adjusted R$^{2}$ & \multicolumn{1}{c}{0.717} \\
Residual Std. Error & \multicolumn{1}{c}{3.206 (df = 30)} \\
F Statistic & \multicolumn{1}{c}{79.561$^{***}$ (df = 1; 30)} \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}
The file mystyle.sty contains the following
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\usepackage{booktabs}
\newcommand{\mc}[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\usepackage{verbatim}
Are there someone who can explain why R markdown can't compile what's above?
By adding the table code line by line and recompiling each time, I isolated the problem to the lines with carets, e.g. the line that reads cyl & -2.876^{***} \\.
I was able to compile the document by enclosing the superscripted part in two $ signs, i.e.
cyl & -2.876$$^{***}$$ \\
(I don't know why you need two, rather than just one, $.)
This compiles just fine:
---
title: "Titre"
date: Fecha
output:
pdf_document:
keep_tex: true
includes:
in_header: mystyle.sty
---
\begin{table}[!htbp] \centering
\begin{tabular}{#{\extracolsep{5pt}}lD{.}{.}{-3} }
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{1}{c}{\textit{Dependent variable:}} \\
\cline{2-2}
\\[-1.8ex] & \multicolumn{1}{c}{mpg} \\
\hline \\[-1.8ex]
cyl & -2.876$$^{***}$$ \\
& (0.322) \\
& \\
Constant & 37.885$$^{***}$$ \\
& (2.074) \\
& \\
\hline \\[-1.8ex]
Observations & \multicolumn{1}{c}{32} \\
R$^{2}$ & \multicolumn{1}{c}{0.726} \\
Adjusted R$^{2}$ & \multicolumn{1}{c}{0.717} \\
Residual Std. Error & \multicolumn{1}{c}{3.206 (df = 30)} \\
F Statistic & \multicolumn{1}{c}{79.561$^{***}$ (df = 1; 30)} \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}

Resources