Home > Back-end >  Making latex multicolumn width wider in Rmarkdown
Making latex multicolumn width wider in Rmarkdown

Time:08-09

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(enter image description here

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}

CodePudding user response:

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}

enter image description here

  • Related