How to set the fixed width of columns? - latex

Hello everyone I am creating a table on latex my code looks like this:
\begin{table}[H]
\centering
\caption{caption}
\label{my-label}
\begin{tabular}{lll}
\hline
\multicolumn{3}{|c|}{\cellcolor[HTML]{34CDF9}{\color[HTML]{000000} Matriz confusión Genero.}} \\ \hline
\multicolumn{1}{|l|}{} & \multicolumn{1}{l|}{M} & \multicolumn{1}{l|}{F} \\ \hline
\multicolumn{1}{|l|}{M} & \multicolumn{1}{l|}{43} & \multicolumn{1}{l|}{7} \\ \hline
\multicolumn{1}{|l|}{F} & \multicolumn{1}{l|}{11} & \multicolumn{1}{l|}{39} \\ \hline
\end{tabular}
\end{table}
It works well but the problem comes when I try to fix the width of the columns I tried:
\begin{tabular}{l{2cm}|l{2cm}|l{2cm}}
The result is the same table, with variable length of columns, I would like to fix the length of the columns, I would like to appreciate any suggestion to solve this problem.

Consider the following code:
\documentclass[a4paper]{article}
\usepackage{}
\begin{document}
\begin{table}%[H]
\centering
\caption{caption}
\label{my-label}
\begin{tabular}{|p{20mm}|p{15mm}|p{10mm}|}
\hline
% \multicolumn{3}{|c|}{\cellcolor[HTML]{34CDF9}{\color[HTML]{000000} Matriz confusión Genero.}} \\ \hline
\multicolumn{3}{|c|}{Matriz confusión Genero.} \\ \hline
& M & F \\ \hline
M & 43 & 7 \\ \hline
F & 11 & 39 \\ \hline
\end{tabular}
\end{table}
\end{document}
that outputs the following table:
You may be interested in particular in the line
\begin{tabular}{|p{20mm}|p{15mm}|p{10mm}|}
implementing paragraph alignment for the contents of a column of given width (here 20, 15 and 10 mm respectively).
To make it simpler, you should just get rid of all of those \multicolumn{1}{}{} and change
\begin{tabular}{l{2cm}|l{2cm}|l{2cm}}
to
\begin{tabular}{|p{2cm}|p{2cm}|p{2cm}|}

Related

Latex table alignment problem with multicolumn and multirow

How can I align this table. It does not algin column two and three at center.
I am using following code:
\begin{table}[htbp]
\caption{Comparison with the hybrid architecture}
\label{tab:comparison}
\begin{center}
\begin{tabular}{|l|c|c|}
\hline
\multicolumn{1}{|c|}{} & \multicolumn{2}{c|}{\textit{Average Episode Reward for 10 Runs (deviation)}} \\ \cline{2-3}
\multicolumn{1}{|c|}{} & \multicolumn{2}{c|}{\textit{Disturbance Magnitude}} \\
\cline{2-3}
\multicolumn{1}{|c|}{\multirow{-3}{*}{\textit{Architecture}}}
& 65\% & 70\% \\ \hline
PID & -17.71 & -23.54 \\
CoL-DRL & -2.58 & -12.25 \\
\rowcolor[HTML]{9AFF99} CoL-DRPRL & -1.85 & -6.25 \\ \hline
\end{tabular}
\end{center}
\end{table}

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:

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}

Getting weird form of table

Hi all if anyone can help me. I am new to latex. I am trying to add table in my latex. I know the format but i don't know what I am doing wrong because my table is not in the form I want moreover it is giving me the error when I add caption.
I need some thing like this in the image
but I am getting this when I compile my latex code
Here is my code:
\begin{center}
\begin{tabular}[!ht]{||c c ||}
\caption{Dataset Specifications}
\centering \label{data}
\hline
Dataset & Samples \\ [0.5ex]
\hline\hline
Vovid & 349 \\
\hline
noncovid & 397 \\ [1ex]
\hline
\end{tabular}
\end{center}
My minimal code:
\documentclass{article}
\setlength{\tabcolsep}{1em}
\renewcommand{\arraystretch}{1.5}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabular}{|c|c|}
\hline
Dataset & Samples \\
\hline
Covid & 349 \\
Non Covid & 397 \\
\hline
\end{tabular}
\caption{Dataset Specifications}
\label{tab:data}
\end{table}
\end{document}
and its output:
Please comment below here if any feature is missing or something is not clear to you :)
How about that:
\begin{table}[!ht]
\centering
\begin{tabular}{|c |c|}
\hline
Dataset & Samples \\ [0.5ex]
\hline
Vovid & 349 \\
noncovid & 397 \\ [1ex]
\hline
\end{tabular}
\caption{Dataset Specifications}
\label{tab:data}
\end{table}
So, the trick is 1) to pack the tabular in a table environment, 2) play with \hline and | to modify the lines around the table cells. More ways to polish tables in https://en.wikibooks.org/wiki/LaTeX/Tables

Wrong column spacing due to multicolumn in latex

in the LaTeX table below, the third column gets too way much spacing due to the long \multicolumn cell in the last line. Can anyone think of a way of getting LaTeX to distribute the additional spacing needed evenly across columns?
\documentclass[11pt]{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabular}{lrrr}
\toprule
& ICE-GB & ICE-SING & ICE-EA \\
\midrule
NP & 619 & 595 & 496 \\
\textbf{Arithmetic mean}& \textbf{1.36} & \textbf{1.33} & \textbf{1.22} \\
SD&0.73 & 0.71 & 0.53 \\
Variation coefficient& 0.54 & 0.52 & 0.44 \\
\midrule
Kruskal Wallis rank sum test&\multicolumn{3}{l}{$H = 16.3941, df = 2, p= 0.0002755123$} \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
Thanks!
Excess width \multicolumns end up assigning the excess to the last column. You can avoid this by either using a fixed-width \multicolumn - that is, use a p{<len>} column, or for even spreading of columns use a fixed-width column for the non-\multicolumn columns:
\documentclass[11pt]{article}
\usepackage{booktabs,array}
\begin{document}
\noindent
\begin{tabular}{l*{3}{>{\raggedleft\arraybackslash}p{5em}}}
\toprule
& ICE-GB & ICE-SING & ICE-EA \\
\midrule
NP & 619 & 595 & 496 \\
\textbf{Arithmetic mean} & \textbf{1.36} & \textbf{1.33} & \textbf{1.22} \\
SD & 0.73 & 0.71 & 0.53 \\
Variation coefficient & 0.54 & 0.52 & 0.44 \\
\midrule
Kruskal Wallis rank sum test & \multicolumn{3}{r}{$H = 16.3941, df = 2, p= 0.0002755123$} \\
\bottomrule
\end{tabular}
\end{document}
In the above example, the last three columns each have width 5em, and are \raggedleft (similar to an r-column specification).

Resources