Vertical Alignment of text in a table in Latex (Overleaf)
Instead of manually messing with the row height of your table, you could change the \arraystretch
and all your cells will automatically be centred
Some other points:
-
instead of manually adding space above the caption, have a look at the
caption
package -
please have a look at http://betterposters.blogspot.com/2012/08/the-data-prison.html or https://www.inf.ethz.ch/personal/markusp/teaching/guides/guide-tables.pdf for some guides about nice table layouts. Putting your data into prison is definitely not a nice design
-
don't put multi-letter expressions like
avg
orout
in math mode. This will completely mess up the kerning
\documentclass[hidelinks,a4paper,12pt,oneside]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{caption}
\title{table}
\begin{document}
\maketitle
\begin{table}[ht]
\renewcommand{\arraystretch}{2}
\footnotesize
\centering
\begin{tabular}{|P{0.75cm}|P{1.25cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{1cm}|P{1cm}|P{1cm}|P{1cm}|P{0.75cm}|}
\hline
\multirow{2}{0.75cm}{\centering Test\\ No} & \multirow{2}{*}{Name} & \multicolumn{3}{c|}{Domain} & \multirow{2}{0.75cm}{\centering Cell\\ Size} & \multicolumn{4}{c|}{MLR} & \multirow{2}{*}{$C_1$} & \multirow{2}{*}{$C_2$}\\
\cline{3-5}\cline{7-10}
& & x & y & z & & $\dot{m_t}$ & $\dot{m}_{in}$ & $\dot{m}_{out}$ & $\dot{m}_{avg}$ & & \\
\hline
1 & Mass1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 \\
\hline
2 & Mass2 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & \multicolumn{2}{c|}{0.1} & 0.1 & 0.1 & 0.1 \\
\hline
\end{tabular}
% \vspace{0.5cm}
\caption{Tests}
\end{table}
\end{document}
Emotional Damage
Updated on February 10, 2021Comments
-
Emotional Damage over 3 years
I have the following sample code in overleaf to generate a table.
\documentclass[hidelinks,a4paper,12pt,oneside]{article} \usepackage[utf8]{inputenc} \usepackage[margin=1in]{geometry} \usepackage{multicol} \usepackage{multirow} \usepackage{array} \newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}} \newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}} \usepackage{stackengine} \newcommand\xrowht[2][0]{\addstackgap[0.5\dimexpr#2\relax]{\vphantom{#1}}} \title{table} \begin{document} \maketitle \begin{table}[ht] \footnotesize \centering \begin{tabular}{|P{0.75cm}|P{1.25cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{1cm}|P{1cm}|P{1cm}|P{1cm}|P{0.75cm}|} \hline\xrowht[()]{15pt} \multirow{2}{*}{Test} & \multirow{2}{*}{Name} & \multicolumn{3}{c|}{Domain} & \multirow{2}{*}{Cell} & \multicolumn{4}{c|}{MLR} & \multirow{2}{*}{$C_1$} & \multirow{2}{*}{$C_2$}\\ \cline{3-5}\cline{7-10}\xrowht[()]{15pt} No & & x & y & z & Size & $\dot{m_t}$ & $\dot{m}_{in}$ & $\dot{m}_{out}$ & $\dot{m}_{avg}$ & & \\ \hline\xrowht[()]{15pt} 1 & Mass1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 \\ \hline\xrowht[()]{15pt} 2 & Mass2 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & \multicolumn{2}{c|}{0.1} & 0.1 & 0.1 & 0.1 \\ \hline \end{tabular} \vspace{0.5cm} \caption{Tests} \end{table} \end{document}
This gives me,
Problem: I cannot get the vertical alignement of the texts inside the table to center or any other position for that matter. For example, the texts "Test No", "Name", "Cell Size", "C1" and "C2", are supposed to be vertically center aligned. Further I see that using
\hline\xrowht[()]{15pt}
gives me different box width ,see "Domain" and "MLR" boxes. Can anybody please help me setup this table in a proper way?Thanks everyone
-
escalator about 3 yearsIf I look closely, the cells content is not exactly vertically centered (visible in rows 1 and 2), this is an issue with
\arraystretch
:-( worth a mention: tex.stackexchange.com/a/166370/32813