Not able to add footnote and caption below a table in Latex - 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}

Related

LaTex table - Column size and text alignment

I am quite a beginner in LaTex and generated the following table:
\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|p{2,5cm}||p{3,5cm}|p{2cm}| p{2cm}|}
\hline
\multicolumn{4}{|c|}{Initial Run of Models} \\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines \\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tabular}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
Is there any way to maintain the width of the columns and still align the text in the columns to be on the right side?
You could use the array package and add >{\raggedleft}:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|>{\raggedleft}p{2.5cm}||>{\raggedleft}p{3.5cm}|>{\raggedleft}p{2cm}| >{\raggedleft\arraybackslash}p{2cm}|}
\hline
\multicolumn{4}{|c|}{Initial Run of Models} \\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines \\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tabular}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
Personally, I would suggest to use the tabularray package instead. This package makes it easy to set the alignment for row/columns or individual cells.
You could use Q[2.5cm] to specify a width, but if you don't, tabularray will determine a much better width for you.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularray}
\begin{document}
\begin{table}[h]
\centering
\begin{tblr}{
colspec={|Q||Q|Q|Q|},
rows={halign=r},
column{1}={halign=l},
row{1}={halign=c},
row{2}={halign=l},
}
\hline
\SetCell[c=4]{} Initial Run of Models &&& \\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines \\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tblr}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
If you use the package array as suggested in the first answer, then you can define in your header one or more newcolumntypes, for example this one called R, right aligned with the width to be assigned to each column. More details here.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|R{2,5cm}||R{3,5cm}|R{2cm}|R{2cm}|}
\hline
\multicolumn{4}{|c|}{Initial Run of Models}\\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines\\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tabular}
\caption{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
Here I changed to R all of the four columns but they don't have to be all of the same kind.
Make a search because surely there are many "duplicates" of this question here and on TeX SE.

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 avoid the line break in my column-headings?

I want to create a table with (at least) 6 columns. Right now, I automatically get a line break after 4 columns and i don't know how to avoid it.
I thought I'd already have my solution with /resizebox, but obviously it doesn't change anything.
\begin{table}[htb]
\caption{Evaluationstabelle}
\resizebox{\textwidth}{!}{%
\begin{tabular}{cccc}\toprule
\textbf{Anzahl Features} &\textbf{Entfernte Features} &\textbf{Filter} &\textbf{Precision} &\textbf{Recall} &\textbf{F-Score} \\
\midrule
08.05. & Ausarbeitung & Formuliere & Bsp & ok & shit \\[20pt]
\bottomrule
\end{tabular}
}
\end{table}
This is how it looks right now:
But I'd want all Columns in one line. If it doesn't fit, maybe by reducing the font size or something.
Whatever you do, don't use \resizebox for elements that contain text, see https://tex.stackexchange.com/questions/425453/why-not-scale-elements-that-contain-text for more details.
One possibility would be to use a tabularx and let latex decide on the best columns widths and linebreaks. In case this is not enough to fit your table in your available textwidth, this can be combined with a smaller font (commented in the code below)
\documentclass{article}
\usepackage{caption}
\usepackage{geometry}
\usepackage{array}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{makecell}
\renewcommand\theadfont{\normalfont\bfseries}
\renewcommand\theadalign{YY}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\begin{document}
\begin{table}[htbp]
\caption{Evaluationstabelle}
%\small
\begin{tabularx}{\linewidth}{#{}YYYYYY#{}}
\toprule
\thead{Anzahl Features} & \thead{Entfernte Features} &\thead{Filter} &\thead{Precision} & \thead{Recall} & \thead{F-Score}\\
\midrule
08.05. & Ausarbeitung & Formuliere & Bsp & ok & test\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

Resources