latex combining multirow and multicolumn - latex

I noticed some strange behaviour when combining \multirow with \multicolumn:
head 1.1 should vertically centred.
head 1.2 is supposed to be vertically and horizontally centred
Is there an alternative solution to \multirow and \multicolumn for creating more complex headers for LaTeX tables or is there a fix for my problem?
\documentclass{article}
\usepackage{multirow}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash\hspace{0pt}}p{#1}}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{|r|r|r|r|}
\multicolumn{1}{|C{2cm}}{\multirow{3}{*}{head 1.1}} &
\multicolumn{2}{|C{2cm}}{\multirow{2}{*}{head 1.2}} &
\multicolumn{1}{|C{2cm}}{head 1.3 which is longer than expected} \\ \hline
& & & \multicolumn{1}{|C{2cm}}{head 2.3} \\
& \multicolumn{1}{|C{2cm}}{head 2.2.1} &
\multicolumn{1}{|C{2cm}}{head 2.2.2} &
\multicolumn{1}{|C{2cm}}{head 3.3}
\end{tabular}
\end{table}
\end{document}

I'd suggest stacking your multi-level headings/cells using a tabular, which will naturally centre it vertically with respect to the other cells. Such tabular stacking is made easy using makecell:
\documentclass{article}
\usepackage{makecell}
\begin{document}
\begin{tabular}{|r|r|r|r|}
head 1.1 &
\multicolumn{2}{c|}{head 1.2} &
\makecell{head 1.3 \\ which is \\ longer than \\ expected} \\
\hline
& head 2.2.1 & head 2.2.2 & \makecell{head 2.3 \\ head 3.3}
\end{tabular}
\end{document}
Other options also exist in aligning cells to the [t]op or [b]ottom.

Related

Not able to add footnote and caption below a table in Latex

I am creating a table using tabularx package, and adding figtext and caption in the table package. I want both the footnote and the caption below the table, but as soon as I add a caption, the footnotes disappear. My code is as below:
The main.tex file has the below code
\documentclass[12pt]{article}
\usepackage{amssymb,amsmath,amsfonts,eurosym,geometry,ulem,graphicx,caption,color,setspace,sectsty,comment,footmisc,caption,pdflscape,subfigure,array,hyperref,booktabs,dcolumn,threeparttable, adjustbox,apacite,dirtytalk,multirow,tabularx,booktabs}
\usepackage{ulem}
\usepackage{float}
\restylefloat{table}
\begin{frame}
\center
\input{MangoShare}
\label{mangoshare}
\end{frame}
MangoShare.tex is the reference which is being called to the main.tex.
\begin{centre}
\begin{table}[h!]
\\[-1.8ex]\hline
\hline \\\\[-1.8ex]
%\renewcommand{\arraystretch}{1.8}
\begin{tabularx}{1\textwidth}{>{\raggedright\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X}
\small
\\[-1.8ex]
& & \multicolumn{6}{c}{Percentage Share} \\\\[-1.8ex]
\cline{3-8} \\[-1.8ex]
& &\multicolumn{1}{c}{A} &\multicolumn{1}{c}{B} &\multicolumn{1}{c}{C} &\multicolumn{1}{c}{D} &\multicolumn{1}{c}{E} &\multicolumn{1}{c}{F} \\
\midrule
\multicolumn{2}{l}{Mango} & 0.58 & 0.56 & 0.92 & 0.22 & 0.72 & 0.033 \\
\tiny
\end{tabularx}
\hline
\hline %\\[-1.8ex]
\figtext{\\[-1.8ex]\emph{Notes:} Controls Used} \\[-1.8ex]
\caption{Mango Share}
\end{table}
\end{centre}
Any help on this front would be appreciated. Thanks a lot!
There are several problems:
table is a floating environment, meant to move around in your document to get a nice looking output. If you wrap it in a static environment like center or frame it can't do that!
\hline is for lines inside a tabular environment, don't use them outside (or better don't use them at all, the rules from the booktab package are much better)
it makes no sense to wrap cells into \multicolumn{1}... remove all these unnecessary commands. They can destroy alignment of your table cells
don't use \\[-1.8ex] in places where there is no active row. They'll mess up table lines etc, see Why are the vertical lines in my table different lengths?
if you want to change the font size of your tabular, do this outside.
\figtext is not defined.
\cmidrule gives better spacing than \cline
the floating specifier [h!] will most likely result in bad image placement, better use [htbp] to give latex more options
--
\documentclass[12pt]{article}
\usepackage{amssymb,amsmath,amsfonts,eurosym,geometry,ulem,graphicx,caption,color,setspace,sectsty,comment,footmisc,caption,pdflscape,subfigure,array,hyperref,booktabs,dcolumn,threeparttable, adjustbox,apacite,dirtytalk,multirow,tabularx,booktabs}
\usepackage{ulem}
\usepackage{float}
\restylefloat{table}
\begin{document}
%\begin{frame}
%\center
%\begin{centre}
\begin{table}[htbp]
%\\[-1.8ex]\hline
%\hline \\\\[-1.8ex]
%\renewcommand{\arraystretch}{1.8}
\begin{tabularx}{1\textwidth}{#{}>{\raggedright\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X >{\centering\arraybackslash}X#{}}
%\small
%\\[-1.8ex]
& & \multicolumn{6}{c}{Percentage Share} \\%[-1.8ex]
\cmidrule{3-8} %\\[-1.8ex]
& &A &B &C &D &E &F \\
\midrule
\multicolumn{2}{#{}l}{Mango} & 0.58 & 0.56 & 0.92 & 0.22 & 0.72 & 0.033 \\
%\tiny
\end{tabularx}
%\hline
%\hline %\\[-1.8ex]
\bigskip
\emph{Notes:} Controls Used
\caption{Mango Share}
\label{mangoshare}
\end{table}
%\end{centre}
%\end{frame}
\end{document}

How to fix the width of longtable LATEX

I want to set the width of my longtable so it fits within the page and not crops out. I have 206 columns and 17 rows.
I use tablesgenerator.com to get my longtable, but the option "Scale table to text width" doesn't work with longtable, could use some help. (Latex beginner here).
\documentclass{article}
\usepackage{longtable}
\usepackage{booktabs}
\begin{document}
\begin{longtable}{#{}lllllllllllllllll#{}}
\toprule
\textbf{SN} & \textbf{zhelio} & \textbf{zhelio\_un} & \textbf{zCMB} &
\textbf{zCMB\_un} & \textbf{Peak\_MJD} & \textbf{Peak\_MJD\_un} & \textbf{x1}
& \textbf{x1\_un} & \textbf{c} & \textbf{c\_un} & \textbf{mB} &
\textbf{mB\_un} & \textbf{t} & \textbf{t\_un} & \textbf{Host\_logmass} &
\textbf{Host\_logmass\_un} \\* \midrule
\endhead
%table stuff here...
\caption{}
\label{tab:my-table}\\
\end{longtable}
\end{document}
This is what it looks like
Assuming you mean 206 rows and not columns, then you could try the following strategies:
put the table on landscape pages
use a smaller font
reduce the margins of your document to have more space
reduce the space between columns
\documentclass{article}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage[vmargin=2cm]{geometry}
\usepackage{lscape}
\begin{document}
\begin{landscape}
\small
\setlength{\tabcolsep}{2pt}
\begin{longtable}{#{}lllllllllllllllll#{}}
\toprule
\textbf{SN} & \textbf{zhelio} & \textbf{zhelio\_un} & \textbf{zCMB} &
\textbf{zCMB\_un} & \textbf{Peak\_MJD} & \textbf{Peak\_MJD\_un} & \textbf{x1}
& \textbf{x1\_un} & \textbf{c} & \textbf{c\_un} & \textbf{mB} &
\textbf{mB\_un} & \textbf{t} & \textbf{t\_un} & \textbf{Host\_logmass} &
\textbf{Host\_logmass\_un} \\* \midrule
\endhead
%table stuff here...
\caption{}
\label{tab:my-table}\\
\end{longtable}
\end{landscape}
\end{document}

An alternative to write multirow in latex's tabular?

In latex, I know we can use the multirow command like the following,
\begin{table}[!h]
\centering
\begin{tabular}{|c|l|}
\hline
\multirow{2}{*}{A}
& I want to place this sentence in multiple lines, \\
& but don't want to control the linebreak myself \\
\hline
\end{tabular}
\end{table}
I think it is so stupid to control the linebreak myself.
Any other alternative that fits the text width to line width?
\documentclass{article}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabularx}{\linewidth}{|c|X|}
\hline
A
& I want to place this sentence in multiple lines, but don't want to control the linebreak myself \\
\hline
\end{tabularx}
\end{table}
\end{document}

Formatting table in latex

I would like to ask how to format table above in latex - table is pretty decent but I would like to have "model" bit right not at the very end, also I would like to have no space at the very right of the table.
\documentclass{article}
\usepackage[utf8]{inputenc}
\title{table in stack}
\author{petr102030 Hrobar}
\date{November 2019}
\begin{document}
\maketitle
\section{Introduction}
\begin{table}[!htbp] \centering
\label{exp_rmse}
\begin{tabular}{#{\extracolsep{5pt}}lccccccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
Model & \multicolumn{1}{c}{RMSE} & \multicolumn{1}{c}{MAE} & \multicolumn{1}{c}{MAPE} & \\
\hline \\[-1.8ex]
Holt-Winters (add.) & 1256.361 & 924.962 & 3.518 &\\
Holt-Winters (mul.) & 1238.799 & 909.395 & 3.457 &\\
Parab. Trend (model 4) & 2228.310 & 1792.471 & 7.959 &\\
Lin.Trend (model 5) & 3074.307 & 2545.426 & 11.339 &\\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
\end{document}
The "Model" is at the leftmost boarder of your table because the default padding is shallowed by #{\extracolsep{5pt}}. If you remove this, you'll get the padding back. (However the usual advice is to remove this padding, so please consider carefully if you really want to do this)
only specify as many columns as you have in your table and not 8 like in your example
with the siunitx package you can align the numbers nicely by their decimal markers
as already mentioned by Picaud Vincent the booktabs package is very useful to create nice looking tables. Amongst other things, it improves the vertical spacing around rules
using \label{} only makes sense if there also is a caption it can reference
I suggest to remove all the unnecessary \multicolumn{1}{...}, they don't do anything useful and can mess up the formatting
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{siunitx}
\title{table in stack}
\author{petr102030 Hrobar}
\date{November 2019}
\begin{document}
\maketitle
\section{Introduction}
\begin{table}[!htbp]
\centering
\begin{tabular}{#{}lS[table-format=4.3]S[table-format=4.3]S[table-format=2.3]}
\toprule
Model & {RMSE} & {MAE} & {MAPE} \\
\midrule
Holt-Winters (add.) & 1256.361 & 924.962 & 3.518 \\
Holt-Winters (mul.) & 1238.799 & 909.395 & 3.457 \\
Parab. Trend (model 4) & 2228.310 & 1792.471 & 7.959 \\
Lin.Trend (model 5) & 3074.307 & 2545.426 & 11.339 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

How to center my text vertically in a table environment?

I have a table with 3 columns where the first 2 columns are pictures and the 3rd is my comment on those pictures, but the comment "drowns", it is down at the bottom of the cell.
I have tried with m{2cm} instead of c:
\begin{tabularx}{\textwidth}{c c m{2cm}}
but no change.
\begin{table}
\caption{Caption if needed}
\label{tab:lastcheck}
\begin{tabularx}{\textwidth}{c c c}
\textbf{O-Na} & \textbf{Na-Y} & \textbf{Comments} \\
\includegraphics[width=0.3\linewidth]{pic1.png} &
\includegraphics[width=0.3\linewidth]{pic2.png} &
My comments. \\
\end{tabularx}
\end{table}
My text is at the bottom of the cell.
The graphbox provides the handy align=c option to vertically centre images:
\documentclass{article}
\usepackage{tabularx}
\usepackage{array}
\usepackage{graphicx}
\usepackage{graphbox}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\begin{document}
\begin{table}
\caption{Caption if needed}
\label{tab:lastcheck}
\begin{tabularx}{\textwidth}{XXX}
\thead{O-Na} & \thead{Na-Y} & \thead{Comments} \\
\includegraphics[width=\linewidth,align=c]{example-image-duck} &
\includegraphics[width=\linewidth,align=c]{example-image-duck} &
My comments. \\
\end{tabularx}
\end{table}
\end{document}

Resources