Right margin in table cells aligned to the right in Latex - latex

I have the following table in Latex
Created with this code:
\begin{table}[h]
\caption{aaaa}
\label{tab:treatments}
\centering
\scalebox{0.8}{
\begin{tabular}{l c c c}
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{Quantificazione log-dim} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\%& 5.4\\
B& A & 2.0\%& 4.6\\
C & N & 5.8\% & 4.6\\
D & N & 3.5\% & 4.26\\
E & K & 22.5\% & 3.7\\
\bottomrule\\
\end{tabular}
}
\end{table}
I would like to have the third column of percentages aligned to the right
At the same time the column label is too large. If I aligned the column to the right it would and up visually weird.
In a Word table I would align the column to the right and than add a right margin to the cells. The numbers would move toward the cell center while keeping their alignment to the right.
How do I do that in Latex? How do I add a margin to the right?

There are a number of options you can try:
Original:
Multi-row header:
Manual alignment of centred columns using \phantoms:
Manual alignment of centred columns using \eqmakeboxes (from eqparbox):
Automated alignment of centred columns using siunitx:
\documentclass{article}
\usepackage{booktabs,graphicx,makecell,siunitx,eqparbox}
\newcommand{\tabhead}{\textbf}
\begin{document}
Original:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{Quantificazione log-dim} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4 \\
B & A & 2.0\% & 4.6 \\
C & N & 5.8\% & 4.6 \\
D & N & 3.5\% & 4.26 \\
E & K & 22.5\% & 3.7 \\
\bottomrule
\end{tabular}
\bigskip
Multi-row header:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4 \\
B & A & 2.0\% & 4.6 \\
C & N & 5.8\% & 4.6 \\
D & N & 3.5\% & 4.26 \\
E & K & 22.5\% & 3.7 \\
\bottomrule
\end{tabular}
\bigskip
Manual alignment of centred columns using \verb|\phantom|s:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4\phantom{0} \\
B & A & \phantom{0}2.0\% & 4.6\phantom{0} \\
C & N & \phantom{0}5.8\% & 4.6\phantom{0} \\
D & N & \phantom{0}3.5\% & 4.26 \\
E & K & 22.5\% & 3.7\phantom{0} \\
\bottomrule
\end{tabular}
\bigskip
Manual alignment of centred columns using \verb|\eqmakebox|es:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & \eqmakebox[log][r]{99.3\%} & \eqmakebox[cint][l]{5.4} \\
B & A & \eqmakebox[log][r]{ 2.0\%} & \eqmakebox[cint][l]{4.6} \\
C & N & \eqmakebox[log][r]{ 5.8\%} & \eqmakebox[cint][l]{4.6} \\
D & N & \eqmakebox[log][r]{ 3.5\%} & \eqmakebox[cint][l]{4.26} \\
E & K & \eqmakebox[log][r]{22.5\%} & \eqmakebox[cint][l]{3.7} \\
\bottomrule
\end{tabular}
\newpage
Automated alignment of centred columns using \verb|siunitx|:
\begin{tabular}{ l c S[table-format = 2.1, table-space-text-post = \%] S[table-format = 1.2] }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4 \\
B & A & 2.0\% & 4.6 \\
C & N & 5.8\% & 4.6 \\
D & N & 3.5\% & 4.26 \\
E & K & 22.5\% & 3.7 \\
\bottomrule
\end{tabular}
\end{document}

Related

How can I make the verticle line in the table without any gaps?

The verticle line in the third column has 2 gaps. How do I do I make it look full?
Here's the code:
\begin{table}[H]
\centering
\caption{Enzymatic cocktails and the respective tested concentrations in \%(v/v) used in Combined Hydrolysis}
\label{enzymatic cocktails}
\begin{tabular}{#{}ccc#{}}
\toprule
\textbf{Cocktail} & \textbf{Enzymes} & \textbf{Concentration \%(v/v)} \\ \midrule
\multirow{2}{*}{A} & \multicolumn{1}{c|}{Cellulase} & \multirow{8}{*}{\begin{tabular}[c]{#{}c#{}}0.25\\ 0.50\\ 1.00\end{tabular}} \\
& \multicolumn{1}{c|}{$\boldsymbol{\beta}$-glucosidase} & \\ \cmidrule(r){1-2}
\multirow{3}{*}{B} & \multicolumn{1}{c|}{Cellulase} & \\
& \multicolumn{1}{c|}{$\boldsymbol{\beta}$-glucosidase} & \\
& \multicolumn{1}{c|}{xylanase} & \\ \cmidrule(r){1-2}
\multirow{3}{*}{C} & \multicolumn{1}{c|}{Cellulase} & \\
& \multicolumn{1}{c|}{$\boldsymbol{\beta}$-glucosidase} & \\
& \multicolumn{1}{c|}{$\boldsymbol{\beta}$-glucanase and xylanase} & \\ \bottomrule
\end{tabular}
\end{table}
Vertical lines like in c| and horizontal lines such as toprule, bottomrule, midrule, cmidrule from the package booktabs do not match.
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}%[H]
\centering
\caption{Enzymatic cocktails and the respective tested concentrations in \%(v/v) used in Combined Hydrolysis}
\label{enzymatic_cocktails}
\begin{tabular}{#{}ccc#{}}
\hline
\textbf{Cocktail} & \textbf{Enzymes} & \textbf{Concentration \%(v/v)}\\
\hline
\multirow{2}{*}{A} & \multicolumn{1}{c|}{Cellulase} & %
\multirow{8}{*}{\begin{tabular}[c]{#{}c#{}}0.25\\ 0.50\\ 1.00\end{tabular}}\\
& \multicolumn{1}{c|}{$\mathbb{\beta}$-glucosidase} & \\
\cline{1-2}
\multirow{3}{*}{B} & \multicolumn{1}{c|}{Cellulase} & \\
& \multicolumn{1}{c|}{$\boldsymbol{\beta}$-glucosidase} & \\
& \multicolumn{1}{c|}{xylanase} & \\
\cline{1-2}
\multirow{3}{*}{C} & \multicolumn{1}{c|}{Cellulase} & \\
& \multicolumn{1}{c|}{$\mathbb{\beta}$-glucosidase} & \\
& \multicolumn{1}{c|}{$\mathbb{\beta}$-glucanase and xylanase} & \\
\hline
\end{tabular}
\end{table}
\end{document}
So I went back to hlines in my code. Better would be to use the above mentioned package booktabs and avoid vertical lines instead.

How do you add vertical lines to a table?

I am making a table and I am not able to make vertical lines to appear. I followed a few examples and they are still not appearing.
\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||}
\hline
\multicolumn{4}{|c|}{Telescope Data} \\
\hline
Channels & \makecell{Beam Size\\ (arcsec)} & \makecell{Noise \\ (mJy/beam)} & Position of Peak Flux \\ [0.5ex]
\hline
1-10 & \makecell{\textbf{min:} 1.42 \\ \textbf{max:} 1.95} & 4.9928 & \makecell{\textbf{peak(x):} 15 \\ \textbf{peak(y):} -13.5 \\ \textbf{peak(flux, max):} 0.002846} \\ [3ex]
\hline
52-61 & \makecell{\textbf{min:} 1.423 \\ \textbf{max:} 1.95} & 0.0018912 & \makecell{\textbf{peak(x):} -0.25 \\ \textbf{peak(y):} 0 \\ \textbf{peak(flux, max):} 0.029341} \\ [3ex]
\hline
103-112 & \makecell{\textbf{min:} 1.427 \\ \textbf{max:} 1.956} & 0.000802855 & \makecell{\textbf{peak(x):} -16 \\ \textbf{peak(y):} 0 \\ \textbf{peak(flux, max):} 0.00241892} \\ [3ex]
\hline
\end{tabular}
\end{table}

How to merge cells with multirow in LaTeX?

I have the following code:
\begin{table}[ht]
\begin{center}
\begin{tabular}{ | l | l | l | l | l | l | l | }
\hline
\multirow{2}{*}{Oprava} & \multirow{2}{*}{zkratka} & \multirow{2}{*}{alg.} & chybovost \% & & & \\ \hline
& & & MAE & RSE & RMSE & RRSE \\ \hline
velikosti & vel. & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
bilateralne & bilat. & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
oprava & zkratka & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
oprava & zkratka & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
\end{tabular}
\end{center}
\caption{Multi-row table}
\label{tab:multicol}
\end{table}
And what I get is this:
I need to merge the cells, so that the text is not crossed out. The same for the rows with the repeated algorithms names - NBM, kNN etc ..... they should me merged and centered. Pls help.
Thanks
The solution to your problem is very simple. You should use \cline{4-7} instead of \hline. Namely \hline makes a horizontal line over the complete width of the table, while \cline{4-7} will only make a horizontal line from column 4 until 7, and thus not crossing the first ones.
To get the text in front of the repeated words centered and merged, you can use multirow{}{}, again in combination with \cline{}. More info about these commands at this link.
An example of the code is given below:
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|l|l|l|l|l|}
\hline
\multirow{2}{*}{Oprave} & \multirow{2}{*}{Zkratka} & \multirow{2}{*}{Alg.} & \multicolumn{4}{l|}{Chybovost \%} \\ \cline{4-7}
& & & MAE & RSE & RMSE & RRSE \\ \hline
\multirow{4}{*}{velikosti} & \multirow{4}{*}{vel.} & NBM & & & & \\ \cline{3-7}
& & c4.5 & & & & \\ \cline{3-7}
& & kNN & & & & \\ \cline{3-7}
& & SMO & & & & \\ \hline
\multirow{4}{*}{bilateralne} & \multirow{4}{*}{bilat.} & NBM & & & & \\ \cline{3-7}
& & c4.5 & & & & \\ \cline{3-7}
& & kNN & & & & \\ \cline{3-7}
& & SMO & & & & \\ \hline
\multirow{4}{*}{oprava} & \multirow{4}{*}{zkratka} & NBM & & & & \\ \cline{3-7}
& & c4.5 & & & & \\ \cline{3-7}
& & kNN & & & & \\ \cline{3-7}
& & SMO & & & & \\ \hline
\multirow{4}{*}{oprava} & \multirow{4}{*}{zkratka} & NBM & & & & \\ \cline{3-7}
& & c4.5 & & & & \\ \cline{3-7}
& & kNN & & & & \\ \cline{3-7}
& & SMO & & & & \\ \hline
\end{tabular}
\end{table}
Resulting into:
Note: if you have struggles with table generation, or you want a more quick way to easily make tables as you would do in Excel, you can use an online table generator for Latex.
I often use http://www.tablesgenerator.com/

Latex table multiple row and multiple column

I'm trying to create a table in Latex but without success. I tried different solutions but no one solves my problem.
I would like create a table like the picture below:
Can anyone show how to do this in Latex please?
One first sketch may be the following:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|c|c|c|}
\hline
\multirow{3}{*}{A} & \multicolumn{2}{c|}{User B} & %
\multicolumn{2}{c|}{User C} & \multirow{3}{*}{D}\\
\cline{2-5}
& \multicolumn{2}{c|}{Value} & \multicolumn{2}{c|}{Value} & \\
\cline{2-5}
& B1 & B2 & C1 & C2 & \\
\hline
& & & & & \\
\hline
& & & & & \\
\hline
% etc. ...
\end{tabular}
\end{document}
It produces:
Addendum:
\documentclass{article}
\usepackage{multirow}
\begin{document}
{\sffamily %
\begin{tabular}{|c|c|c|c|c|c|c|}% seven columns now, not six...
\hline
\multirow{3}{*}{A} & \multicolumn{2}{c|}{User B} & \multirow{3}{*}{X} & %
\multicolumn{2}{c|}{User C} & \multirow{3}{*}{D}\\
\cline{2-3}\cline{5-6}
& \multicolumn{2}{c|}{Value} & & \multicolumn{2}{c|}{Value} & \\
\cline{2-3}\cline{5-6}
& B1 & B2 & & C1 & C2 & \\
\hline
& & & & & & \\
\hline
& & & & & & \\
\hline
% etc. ...
\end{tabular}
}%
\end{document}
produces:
Please, critically check all the differences between the original code and this latter.
Don't forget that several LaTeX packages can help you improving style, dimensions and spacing of your table: among these, I advise you to have a look at bigstrut.
Also:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|c|c|c|c|}
\hline
\multirow{3}{*}{A} & \multirow{3}{*}{X} & \multicolumn{2}{c|}{User B} & \multicolumn{2}{c|}{User C} & \multirow{3}{*}{D}\\
\cline{3-6}
& & \multicolumn{2}{c|}{Value} & \multicolumn{2}{c|}{Value} & \\
\cline{3-6}
& & B1 & B2 & C1 & C2 & \\
\hline
& & & & & & \\
\hline
\end{tabular}
\end{document}
You should now be able to operate on your own further changes to the model of table.
Check out the multirow package:
http://texblog.org/2012/12/21/multi-column-and-multi-row-cells-in-latex-tables/
You have to include the library:
%multi-column
\multicolumn{number cols}{align}{text} % align: l,c,r
%multi-row
\usepackage{multirow}
\multirow{number rows}{width}{text}
Then it looks like this:
\documentclass[11pt]{article}
\usepackage{multirow}
\begin{document}
\begin{table}[ht]
\caption{Multi-column and multi-row table}
\begin{center}
\begin{tabular}{ccc}
\hline
\multicolumn{2}{c}{\multirow{2}{*}{Multi-col-row}}&X\\
\multicolumn{2}{c}{}&X\\
\hline
X&X&X\\
\hline
\end{tabular}
\end{center}
\label{tab:multicol}
\end{table}
\end{document}
note: code examples from the link provided

How to rotate table-headline in Latex table

is there a way to rotate the "Demo 1", "Demo2" and "Demo 3" headlines 90° in the following LaText table?
\documentclass[a4paper,twoside,10pt]{report}
\begin{document}
\begin{tabular}{|l|l|l|l|}
\hline
& Demo1 & Demo2 & Demo3 \\
\hline
Person 1 & x & & \\
\hline
Person 2 & x & & x \\
\hline
Person 3 & x & x & \\
\hline
Person 4 & & x & x \\
\hline
\end{tabular}
\end{document}
Thanks
Yes. As Dr. Andrew J. Page wrote on his blog,
Put this at the top of your code:
\usepackage{rotating}
Simply to create a table with rotated column text use this code:
\begin{tabular}{|r|r|}\hline
\begin{sideways}Paper\end{sideways} &\begin{sideways}Static\end{sideways} \\
\hline
HAR1994j & Journal \\
SWRT1996c & Conference \\
\hline
\end{tabular}

Resources