vertical alignment in texstudio - latex

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}

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 Overfull /hbox when inserting table. Cannot change width without changing table structure

When I insert my table into latex I get an overfull /hbox error and cannot figure out how to change the width without changing the structure of my table. The code of my table follows as such:
\begin{center}
\begin{tabular}{||c c c||}
\hline
Function() & Computational Cost (Gas)& USD Equivalent \\ [0.5ex]
\hline\hline
Total Deployment & 8342204 & 1.195559526 \\
\hline
Security Registration & 3849155 & 0.5516400614 \\
\hline
\textit{MintSecurity} & 76293 & 0.01093389983 \\
\hline
\textit{TransferFrom} & 55146 & 0.007903226248 \\
\hline
\textit{Swap} & 89343 & 0.0128041552 \\
\hline
\textit{AddLiquidity} & 185368 & 0.02656593847 \\
\hline
\textit{Remove Liquidity} & 67137 & 0.009621711468 \\ [1ex]
\hline
\end{tabular}
\end{center}
In the template you are using, tables are set in a smaller font size, so the problem is solved automatically if you use a table environment instead of center.
In addition, you can use the tabularray package, so the table will automatically match the text width:
\documentclass[conference]{IEEEtran}
\usepackage{tabularray}
\usepackage{lipsum}
\begin{document}
\lipsum[2]
\begin{table}[htbp]
\begin{tblr}{||c X[c] c||}
\hline
Function() & Computational Cost (Gas)& USD Equivalent \\
\hline\hline
Total Deployment & 8342204 & 1.195559526 \\
\hline
Security Registration & 3849155 & 0.5516400614 \\
\hline
\textit{MintSecurity} & 76293 & 0.01093389983 \\
\hline
\textit{TransferFrom} & 55146 & 0.007903226248 \\
\hline
\textit{Swap} & 89343 & 0.0128041552 \\
\hline
\textit{AddLiquidity} & 185368 & 0.02656593847 \\
\hline
\textit{Remove Liquidity} & 67137 & 0.009621711468 \\
\hline
\end{tblr}%
\end{table}
\lipsum[2]
\end{document}

How to create a multirow multicolumn table with subheadings below the first row

I am attempting to create a table in latex as below, although I am a bit confused about where and when the \multirow and \multicolumn commands are to be used inside a tabular environment. Col_1, Col_2, and Col_3 are supposed to be subheadings but they need to appear in the second row.
What I started off with is, (the columns are duplicated, so I wanted to split it into multiple rows as shown in the image) :
\begin{table}[H]
\begin{tabular}{lSSSSSSSS}
\toprule
\multirow{2}{*}{\textbf{A}} &
\multicolumn{3}{c}{\textbf{Model_1}} &
\multicolumn{5}{c}{\textbf{Model_2}} \\
& {Col_1} & {Col_2} & {Col_3} & {Col_1} & {Col_2} & {Col_3} & {Col_4} & {Col_5} \\
\midrule
x\% & a\% b\% & c\% & x\% & y\% & z\% & 0. & 0. \\
\bottomrule
\end{tabular}
\caption{results}
\end{table}
You can place your subheaders in a row of their own, no need to worry about mutirows and such
\documentclass{article}
\usepackage{siunitx}
%\usepackage{booktabs}
\usepackage{float}
\begin{document}
\begin{table}[H]
\begin{tabular}{l|S|S|S}
\textbf{A} & \multicolumn{3}{c}{x\%} \\
\hline
& {Col\_1} & {Col\_2} & {Col\_3} \\
Model\_1 & a\% & b\% & c\% \\
\hline
Model\_2 & x\% & y\% & z\% \\
\end{tabular}
\caption{results}
\end{table}
\end{document}
Instead of the layout you show in your image, I rather suggest to avoid vertical lines for a more professional looking result:
\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabular}{#{}lSSSSSSSS#{}}
\toprule
\textbf{A} &
\multicolumn{3}{c}{\textbf{Model\_1}} &
\multicolumn{5}{c}{\textbf{Model\_2}} \\
& {Col\_1} & {Col\_2} & {Col\_3} & {Col\_1} & {Col\_2} & {Col\_3} & {Col\_4} & {Col\_5} \\
\cmidrule(r){2-4}\cmidrule(l){5-9}
x\% & a\% & b\% & c\% & x\% & y\% & z\% & 0. & 0. \\
\bottomrule
\end{tabular}
\caption{results}
\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 define a cellsize in LaTeX for wraptable?

I have created the following table in LaTex with wrap table from Package wrapfigure.
\documentclass[a4paper,11pt,oneside,numbers=noendperiod,setspace,parskip=half]{scrbook}
\usepackage[ngerman, english]{babel}
\usepackage{blindtext}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{wrapfig}
\usepackage[table,xcdraw]{xcolor}
\begin{document}
\blindtext
\begin{wraptable}{l}{8cm}
\begin{tabular}{lcccc}
\hline
\rowcolor[HTML]{656565}
\multicolumn{1}{|c|}{\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF}
\textbf{Surface geometry}}} & \multicolumn{4}{c|}{\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF} \textbf{Terrain Classes}}} \\ \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}fine texture,\\ high convexity\end{tabular}}} & \multicolumn{1}{c|}{\cellcolor[HTML]{643403}\textbf{1}} & \multicolumn{1}{c|}{\cellcolor[HTML]{F8A102}\textbf{5}} & \multicolumn{1}{c|}{\cellcolor[HTML]{009901}\textbf{9}} & \multicolumn{1}{c|}{\cellcolor[HTML]{34FF34}\textbf{13}} \\ \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}fine texture,\\ low convexity\end{tabular}}} & \multicolumn{1}{c|}{\cellcolor[HTML]{CE6301}\textbf{3}} & \multicolumn{1}{c|}{\cellcolor[HTML]{FFCC67}\textbf{7}} & \multicolumn{1}{c|}{\cellcolor[HTML]{34CDF9}\textbf{11}} & \multicolumn{1}{c|}{\cellcolor[HTML]{3531FF}\textbf{15}} \\ \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}coarse texture,\\ low convexity\end{tabular}}} & \multicolumn{1}{c|}{\cellcolor[HTML]{F007F4}\textbf{2}} & \multicolumn{1}{c|}{\cellcolor[HTML]{A34894}\textbf{6}} & \multicolumn{1}{c|}{\cellcolor[HTML]{C5C783}\textbf{10}} & \multicolumn{1}{c|}{\cellcolor[HTML]{FCFF2F}\textbf{14}} \\ \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}coarse texture,\\ low convexity\end{tabular}}} & \multicolumn{1}{c|}{\cellcolor[HTML]{F690E5}\textbf{4}} & \multicolumn{1}{c|}{\cellcolor[HTML]{F6C8EC}\textbf{8}} & \multicolumn{1}{c|}{\cellcolor[HTML]{DFE360}\textbf{12}} & \multicolumn{1}{c|}{\cellcolor[HTML]{FFFC9E}\textbf{16}} \\ \hline
& \multicolumn{1}{l}{\textit{steep}} & \multicolumn{2}{l}{} & \multicolumn{1}{l}{\textit{gentle}}
\end{tabular}
\end{wraptable}
\blindtext
\end{document}
This looks currently like that:
Table in document:
How is it possible, to change, or set a specific cell size of the columns for all the "Terrain Classes" 1 to 16? Like alls cells have the same width 1.5cm?
Here are my proposed changes to your code:
Change the tabular command to fixed-width columns
Change cells from \multicolumn{1}{c}{<stuff>} to simply <stuff>
Change \\ at end of tabular row to \tabularnewline
I have also taken the liberty to choose 1cm instead of 1.5cm (too wide) and increase the wrapping size to accommodate the wider table.
\documentclass[a4paper,11pt,oneside,numbers=noendperiod,setspace,parskip=half]{scrbook}
\usepackage[ngerman, english]{babel}
\usepackage{blindtext}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{wrapfig}
\usepackage[table,xcdraw]{xcolor}
\begin{document}
\blindtext
\begin{wraptable}{l}{10cm}
\begin{tabular}{|l*{4}{|>{\centering}p{1cm}}|}
\hline
\rowcolor[HTML]{656565}
\multicolumn{1}{|c|}{\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF}
\textbf{Surface geometry}}} & \multicolumn{4}{c|}{\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF} \textbf{Terrain Classes}}} \\ \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}fine texture,\\ high convexity\end{tabular}}} &
\cellcolor[HTML]{643403}\textbf{1} &
\cellcolor[HTML]{F8A102}\textbf{5} &
\cellcolor[HTML]{009901}\textbf{9} &
\cellcolor[HTML]{34FF34}\textbf{13} \tabularnewline \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}fine texture,\\ low convexity\end{tabular}}} &
\cellcolor[HTML]{CE6301}\textbf{3} &
\cellcolor[HTML]{FFCC67}\textbf{7} &
\cellcolor[HTML]{34CDF9}\textbf{11} &
\cellcolor[HTML]{3531FF}\textbf{15} \tabularnewline \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}coarse texture,\\ low convexity\end{tabular}}} &
\cellcolor[HTML]{F007F4}\textbf{2} &
\cellcolor[HTML]{A34894}\textbf{6} &
\cellcolor[HTML]{C5C783}\textbf{10} &
\cellcolor[HTML]{FCFF2F}\textbf{14} \tabularnewline \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}coarse texture,\\ low convexity\end{tabular}}} &
\cellcolor[HTML]{F690E5}\textbf{4} &
\cellcolor[HTML]{F6C8EC}\textbf{8} &
\cellcolor[HTML]{DFE360}\textbf{12} &
\cellcolor[HTML]{FFFC9E}\textbf{16} \tabularnewline \hline
\multicolumn{1}{l}{} & \multicolumn{1}{l}{\textit{steep}} & \multicolumn{2}{l}{} & \multicolumn{1}{l}{\textit{gentle}}
\end{tabular}
\end{wraptable}
\blindtext
\end{document}
Here's the result:

Resources