Vertical align in table on Latex - latex

I want to make a table and print the text in the middle of all cases. I use the array package and I wrote:
\begin{table}[H]
\begin{center}
\begin{tabular}{|m{3cm}|m{2.5cm}|m{2.5cm}|m{2.5cm}|m{6cm}|}
But then Latex says that I missed one '$' or one '{' in my table... but there is only text, so I don't understand. When I switched to :
\begin{table}[H]
\begin{center}
\begin{tabular}{|m{3cm}|C{2.5cm}|C{2.5cm}|C{2.5cm}|C{6cm}|}
all is ok and works... but it's ugly. I read a lot of stuff about that on the site already but never fixed my obvious-like problem :/

Here is a minimal example of the layout I use for tables.
Different combinations of sizing and formatting are shown.
I am not sure if this is what you asked for. Please reformulate your question, if it is not.
\documentclass{scrartcl}
\usepackage{booktabs} % \cmidrule in tables
% \usepackage{caption} % Nice Captions
% \usepackage{longtable} % Tables larger than one page
% \usepackage{multirow} % Mergings Cells
% \usepackage{multicol} % Merging Cells
\usepackage{tabularx}
\begin{document}
\begin{table}
\caption{Some caption, for tables always above}
\label{some label}
\begin{tabularx}{0.99\textwidth}{>{\centering}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}p{0.5cm}>{\raggedleft}p{3cm}>{\raggedright}X>{\raggedleft\arraybackslash}X}
\toprule
\textsc{Foobar} & $a$ & $b$ & $c$ & $d$ & $e$ & $f$ \\
\cmidrule(r){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(l){7-7}
1 & 2 & 2 & 2 & 2 & 2 & 2 \\
2 & 4 & 5 & 4 & 4 & 4 & 4 \\
3 & 4 & 3 & 4 & 3 & 3 & 3 \\
\bottomrule
\end{tabularx}
\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}

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}

Extra alignment in tabular array

I am trying to center one of the columns in a table and It says I have an extra column. The table also doesnt look correct i.e.
What it looks like:
F(t) F(s)
u(t)
1/s
e 1/(s-a)
What I expect:
F(t) F(s)
u(t) 1/s
e 1/(s-a)
I think it is because I'm trying to insert an equation in the table but I'm not sure.
Here is the code
\documentclass[12pt, letterpaper, fleqn]{article}
\usepackage{array}
\begin{document}
\begin{table}
% \setlength{\tabcolsep}{6pt}
% \renewcommand{\arraystretch}{1}
\begin{tabular}{p{0.225\textwidth} >{\centering}p{0.15\textwidth}}
\textbf{F(t)} & \textbf{F(s)} \\
\(u(t)\) & \(\frac{1}{s}\) \\
\(e^{at}\) & \(\frac{1}{s-a}\) \\
\end{tabular}
\end{table}
\end{document}
Your use of \centering to centre the column works, but it screws up the way \\ is interpreted. A correction is included in the array package documentation after using alignment switches (like \centering, \raggedright, \raggedleft, etc) using \arraybackslash:
\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{ p{0.225\textwidth} >{\centering\arraybackslash}p{0.15\textwidth} }
$\mathbf{F(t)}$ & $\mathbf{F(s)}$ \\
$u(t)$ & $1 / s$ \\
$e^{at}$ & $1 / (s - a)$
\end{tabular}
\end{document}
If you remove >{\centering}, then your code should compile as expected. Post a comment otherwise! Do you want any column to have centered contents?

latex combining multirow and multicolumn

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.

Aligning column in tabularx

I want to align the last column properly. There is extra space towards the end. Please let me know how to do that. Thanks.
\documentclass[11pt]{article}
\usepackage{setspace} %double spacing and spacing in tables
\usepackage{amsmath} %equations etc. in latex
\usepackage[capposition=top]{floatrow} %so that the caption for figures appear at the top
\usepackage[tablename=TABLE,figurename = FIGURE,labelsep=newline,aboveskip=0pt,font=bf,justification=centering]{caption} %so that caption looks cool
\usepackage{booktabs} %midrule etc. which adds space around lines. Make tables look good.
\usepackage{tabularx} %use tabularx environment for creating one page tables
\usepackage[margin=1in]{geometry} %defining the margin for the page
\usepackage[autostyle]{csquotes} %for quotes. alternative would be " and "
\usepackage[table]{xcolor} %rowcolor, cellcolor, color for references
\usepackage{pdflscape} %landscape and keep the pages straight
\usepackage{everypage} %For AddEveryHookPage
\usepackage{hanging} %For references
\usepackage{longtable} %For multiple pages
\usepackage{multirow} %Valuable package as can be seen from table 5,8, and 10
\usepackage{graphicx} %created line using this package
\usepackage{bm} %bold and italics in the math enviornment at the same time
\usepackage{dcolumn} % a package actually used in this example
\def\hang{\par\noindent\makebox[1.5em][l]{ }\hangindent1.5em} %hanging indent
\makeatletter
\setlength{\#fptop}{0pt} %to make tables and figures start at the top of the page
\makeatother
\newcommand{\Lpagenumber}{\ifdim\textwidth=\linewidth\else\bgroup %to correct the page numbering for landscape pages
\dimendef\margin=0
\ifodd\value{page}\margin=\oddsidemargin
\else\margin=\evensidemargin
\fi
\raisebox{\dimexpr -\topmargin-\headheight-\headsep-0.5\linewidth}[0pt][0pt]{%
\rlap{\hspace{\dimexpr \margin+\textheight+\footskip}%
stabu \llap{\rotatebox{90}{\thepage}}}}%
\egroup\fi}
\AddEverypageHook{\Lpagenumber}% %to correct the page numbering for landscape pages
\newcolumntype{Y}{>{\centering\arraybackslash}X} %to center the columns for tabularx environment
\newcolumntype{Z}{>{\centering\arraybackslash}p{0.75in}} %to center the columns for tabularx environment
\newcolumntype{K}{>{\centering\arraybackslash}p{0.3in}} %to center the columns for tabularx environment
\newcommand{\gmc}[3]{\multicolumn{#1}{#{}#2#{}}{#3}} %short form for multicolumn
\doublespacing %make lines double spaced
\begin{document}
\setcounter{page}{-1} %start numbering at page 3 (2 - n where n = -1)
\thispagestyle{empty} %suppress page number
\begingroup % keep any font size changes local to group
\captionof{table}{\textbf{Descriptive Statistics}}
\singlespacing
\footnotesize
\centering
\setlength\tabcolsep{3pt} %default
\renewcommand{\arraystretch}{1}
\begin{tabularx}{\textwidth}{#{\extracolsep{\fill}}l*{2}{c}}
\gmc{3}{l}{\textbf{Panel A: Sample Representation across Economic Sectors}} \\\midrule
\textbf{Sector} & \textbf{N} & \textbf{\% Frequency} \\\midrule
Energy & 524 & 1.65\% \\
Materials & 2,648 & 8.34\% \\
Industrials & 5,905 & 18.60\% \\
Consumer Discretionary & 5,549 & 17.48\% \\
Consumer Staples & 2,174 & 6.85\% \\
Health Care & 5,167 & 16.28\% \\
Financials & 0 & 0.00\% \\
Information Technology & 9,774 & 30.79\% \\
Telecommunication Services & 0 & 0.00\% \\
Utilities & 0 & 0.00\% \\\midrule
Total & 31,741 & 100.00\% \\\midrule
\end{tabularx}
\endgroup
\end{document}
If you just want it to have no space for the last column you can
replace
\begin{tabularx}{\textwidth}{#{\extracolsep{\fill}}l*{2}{c}}
with:
\begin{tabularx}{\textwidth}{l Y r}

Resources