Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the fixed width of columns?

Tags:

latex

tabular

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.

like image 942
neo33 Avatar asked May 23 '16 15:05

neo33


People also ask

How do I keep my column width fixed in HTML?

The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width for the columns can be in pixels, or percentage.

How do you add fixed width?

To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.


1 Answers

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:

screenshot of output

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}|}
like image 184
MattAllegro Avatar answered Oct 22 '22 17:10

MattAllegro