6

I want to make a table where all the cells will have equal width.enter image description here

Here the cells width of automobile is bigger than the others.So i want to take all the cells have the width of automobile. I tried this code but don't know how to specify width and height of a cell:

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|l|l|l|l|}
\hline
labels     & airplane & automobile & bird & cat & deer \\ \hline
airplane   &          &            &      &     &      \\ \hline
automobile &          &            &      &     &      \\ \hline
bird       &          &            &      &     &      \\ \hline
cat        &          &            &      &     &      \\ \hline
\end{tabular}
\end{table}
3
  • Either use tabularx package and the X column or replace l with p{3cm} or something like that
    – user31729
    Commented Apr 20, 2016 at 18:03
  • can u show me an example?? Commented Apr 20, 2016 at 18:05
  • The vertical lines are not really nice
    – user31729
    Commented Apr 20, 2016 at 18:13

3 Answers 3

3

The cleanest way is using the X columntype from tabularx package -- it calculates the column width from the width speficied as 1st argument to tabularx.

enter image description here

\documentclass{article}

\usepackage{tabularx}


\begin{document}

\begin{table}
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|*{6}{p{1.5cm}|}}
\hline
labels     & airplane & automobile & bird & cat & deer \\ \hline
airplane   &          &            &      &     &      \\ \hline
automobile &          &            &      &     &      \\ \hline
bird       &          &            &      &     &      \\ \hline
cat        &          &            &      &     &      \\ \hline
\end{tabular}
\end{table}


\begin{table}
\centering
\caption{My caption}
\label{my-other-label}
\begin{tabularx}{\linewidth}{|*{6}{X|}}
\hline
labels     & airplane & automobile & bird & cat & deer \\ \hline
airplane   &          &            &      &     &      \\ \hline
automobile &          &            &      &     &      \\ \hline
bird       &          &            &      &     &      \\ \hline
cat        &          &            &      &     &      \\ \hline
\end{tabularx}
\end{table}


\end{document}
8
  • ! Extra alignment tab has been changed to \cr. why am i getting this error?? Commented Apr 20, 2016 at 18:19
  • @setubasak: You have too much & in your example. My posted code works
    – user31729
    Commented Apr 20, 2016 at 18:22
  • I have 10x10 table.. yes i have lot of & this.. so what can i do then?? Commented Apr 20, 2016 at 18:23
  • Replace the 6 from my code with a 10 then ;-) What do you think the 6 does there? ;-)
    – user31729
    Commented Apr 20, 2016 at 18:24
  • how can i control the height of the cell?? Commented Apr 20, 2016 at 18:32
2

A nicer-looking version using booktabs:

enter image description here

\documentclass{article}
\usepackage{tabularx,booktabs}
\begin{document}

\begin{table}
\centering
\caption{My caption}
\label{my-other-label}
\begin{tabularx}{\linewidth}{l*{5}{X}}
\toprule
labels     & airplane & automobile & bird & cat & deer \\ \midrule
airplane   &          &            &      &     &      \\ 
automobile &          &            &      &     &      \\ 
bird       &          &            &      &     &      \\ 
cat        &          &            &      &     &      \\ \bottomrule
\end{tabularx}
\end{table}

\end{document}
3
  • how can i increase or decrease overall table size as my table will be 10x10?? Commented Apr 20, 2016 at 18:25
  • @setubasak - You can rotate your table by 90 degrees using the sidewaystable environment after loading the rotating package.
    – AboAmmar
    Commented Apr 20, 2016 at 18:54
  • @AboAmmar If I need to centralize one column, what should be the structure of \begin{tabularx}{\linewidth}{l*{5}{X}}?
    – AtanuCSE
    Commented Apr 8, 2022 at 7:18
1

With {NiceTabular} of nicematrix, you have a key columns-with to fix the width of all the columns. With the value auto, all the columns will have a width equal to the width of the widest cell.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{caption}

\begin{document}
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{NiceTabular}{llllll}[hvlines,columns-width=auto]
labels     & airplane & automobile & bird & cat & deer \\ 
airplane   &          &            &      &     &      \\ 
automobile &          &            &      &     &      \\ 
bird       &          &            &      &     &      \\ 
cat        &          &            &      &     &      \\ 
\end{NiceTabular}
\end{table}
\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .