Formatting table in latex - 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}

Related

How to build a table in latex that one column can have three subcolumns

I am trying to build a table in Latex that one column which in my case in "Factor Loading" has three subcolumns, and also text in the "Item" column to be wrapped if it too big.
\multicolumn{}{}{} is the way to merge a number of cells but in your case, I would move "Factor loading" to a bottom part with annotations. The table seems to look more balanced without and the information is still there.
Here is the solution with a few improvements
\documentclass{article}
\usepackage{geometry}
\usepackage{tabularx} % Mainly for X-type column. Also loads very useful `array` package
\usepackage{booktabs} % For custom rules: \toprule, \bottomrule, \midrule, \cmidrule and \spacialrule
\usepackage{caption} % For custom formatting of captions
\usepackage{ragged2e} % Adds \RaggedRight and \Centering and improves text typesetting in narrow cells
\usepackage[nopar]{kantlipsum} % Only for summy text - can be removed in final draft
\captionsetup[table]{position=top, skip=1pt} % Adds a small gap between caption and a table
\newcommand\tn[1]{\rlap{\textsuperscript{#1}}} % A custom macro to add an annotation mark
\newcommand\thead[1]{\textbf{#1}} % Formats headings
\renewcommand{\tabularxcolumn}[1]{>{\RaggedRight}p{#1}} % For X formatting. Here, it only adds \RaggedRight
\newcolumntype{F}[1]{>{\Centering}p{#1}} % A custom column-type. Adds centering to default left alignment of p{}
\begin{document}
\begin{table}[tbh]
\renewcommand*{\arraystretch}{1.5} % Stretch a table vertically adding small spacing between rows
\centering
\caption{The table}
\label{tab:table}
\begin{tabularx}{1.0\linewidth}{#{} p{2.2cm} | X | *3{#{}F{1.2cm}} #{}}
\toprule
\multicolumn{1}{#{}p{2.2cm}}{\thead{Construct}} % \multicolumn can also cancel vertical bars around cells
& \multicolumn{1}{c}{\thead{Items}}
& \thead{KR}\tn{*}
& \thead{SG}\tn{*}
& \thead{US}\tn{*} \\
\specialrule{\lightrulewidth}{3pt}{0pt}
Collaborative & \kant[1][1] & 0.798 & 0.814 & 0.878 \\
& \kant[1][2] & 0.749 & 0.874 & 0.838 \\
& \kant[1][3] & 0.865 & 0.878 & 0.924 \\
& \kant[1][4] & 0.893 & 0.834 & 0.820 \\
& \kant[1][5] & 0.910 & 0.86 & 0.898 \\
& \kant[1][6] & 0.770 & 0.903 & 0.921 \\
\specialrule{\heavyrulewidth}{0pt}{0pt}
\multicolumn{5}{#{}l}{\tn{*}\hspace{0.25em} Factor loading}
\end{tabularx}
\end{table}
\end{document}
EDIT.Also, as suggested, tabularray is another alternative. The package gives you very easy interface to actually paint tables. The downside is time of compilation, which is a few times longer than that of regular tables.
Just for fun, here a variation with the tabularray package:
\documentclass{article}
\usepackage{tabularray}
\usepackage{lipsum}
\begin{document}
\begin{table}[tbh]
\caption{The table}
\label{tab:table}
\begin{tblr}{|l|X|c|c|c|}
\hline
Construct & Items & \SetCell[c=3]{} Factor loading &&\\
\hline
& & KR & SG & US \\
\hline
Collaborative & \lipsum[1][1] & 0.798 & 0.814 & 0.878 \\
& \lipsum[1][1] & 0.749 & 0.874 & 0.838 \\
& \lipsum[1][1] & 0.865 & 0.878 & 0.924 \\
& \lipsum[1][1] & 0.893 & 0.834 & 0.820 \\
& \lipsum[1][1] & 0.910 & 0.86 & 0.898 \\
& \lipsum[1][1] & 0.770 & 0.903 & 0.921 \\
\hline
\end{tblr}
\end{table}
\end{document}

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.

making latex table

Can anyone give an idea on how to make this table?
Use \begin{table} [...] \end{table}
Here is a minimal working example:
\begin{table}
\centering
\begin{tabular}{l r}
& 119 Responses\\
Field & Percentage \\
\hline
Very convenient & 63\% \\
Sowewhat convenient & 28\% \\
Neutral & 5\% \\
Somewhat inconvenient & 2\% \\
Very inconvenient & 3\% \\
\end{tabular}
\end{table}
\end{document}
It looks like this:

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}

vertical alignment in texstudio

I am making a table with texstudio, and I have trouble with vertical alignment of the texts. When I have \hline, the text crosses the line.
Here is the code:
\documentclass{article}
\usepackage{siunitx}
\begin{document}
\begin{tabular}{lcccc}
\hline\hline
& \textbf{$d_{(O_{1}-Zn/Mg)}(\SI{}{\angstrom})$}
& \textbf{$d_{(O_{2}-Zn/Mg)}(\SI{}{\angstrom})$}
& \textbf{$d_{(O_{3}-Zn/Mg)}(\SI{}{\angstrom})$}
& \textbf{$d_{(O_{\bot}-Zn/Mg)}(\SI{}{\angstrom})$} \\[1mm]
\hline
\textbf{Pure HB-NWs} & & & & \\
\hline\hline
\end{tabular}
\end{document}
And I have also enclosed the undesired result to make my point clear.
I would suggest using the booktabs package which addresses rules and lines in tables quite well. See its documentation for some notes on table formatting and how to used it. In short, adding
\usepackage{booktabs}
to your preamble will let you use a number of new commands for rules in tables:
\toprule
\midrule
\bottomrule
combining these with your example gives a table looking as follows:
or, if you insist on using double rules at top and bottom:
In both cases I don't see the need for your additional '[1em]' space below the top line - without it things would look like this:
,
This will generate a pdf with all three examples to allow easy comparison:
\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\begin{document}
Using \emph{booktabs} style rules:\\\vskip\baselineskip
\begin{tabular}{lcccc}
\toprule
& \textbf{$d_{(O_{1}-Zn/Mg)}(\SI{}{\angstrom})$}
& \textbf{$d_{(O_{2}-Zn/Mg)}(\SI{}{\angstrom})$} \\[1mm]
\midrule
\textbf{Pure HB-NWs} & & & & \\
\bottomrule
\end{tabular}
\vskip\baselineskip
also works without the additional `[1mm]' spacing at the bottom of the line:
\vskip\baselineskip
\begin{tabular}{lcccc}
\toprule
& \textbf{$d_{(O_{1}-Zn/Mg)}(\SI{}{\angstrom})$}
& \textbf{$d_{(O_{2}-Zn/Mg)}(\SI{}{\angstrom})$} \\
\midrule
\textbf{Pure HB-NWs} & & & & \\
\bottomrule
\end{tabular}
\vskip\baselineskip
\vskip\baselineskip
Using \emph{double rules}:
\vskip\baselineskip
\begin{tabular}{lcccc}
\midrule\midrule
& \textbf{$d_{(O_{1}-Zn/Mg)}(\SI{}{\angstrom})$}
& \textbf{$d_{(O_{2}-Zn/Mg)}(\SI{}{\angstrom})$} \\[1mm]
\midrule
\textbf{Pure HB-NWs} & & & & \\
\midrule\midrule
\end{tabular}
\vskip\baselineskip
and again without the additional `[1mm]' spacing at the bottom of the line:
\vskip\baselineskip
\begin{tabular}{lcccc}
\midrule\midrule
& \textbf{$d_{(O_{1}-Zn/Mg)}(\SI{}{\angstrom})$}
& \textbf{$d_{(O_{2}-Zn/Mg)}(\SI{}{\angstrom})$} \\
\midrule
\textbf{Pure HB-NWs} & & & & \\
\midrule\midrule
\end{tabular}
\end{document}
On a side note: the 'siunitx' package you're using provides a '\si' command specifically for printing only units, rather than using the '\SI' command with an empty argument; i.e. you might want to use the following:
\si{\angstrom}
rather than:
\SI{}{\angstrom}

Resources