1

I have a table built in Latex, it's shown larger than the width of the text. Although I tried many solutions available online, but no one of them has worked with them. I think that is because the text in header cells (first row) cannot be broken, they are written in one line. And even its labels starts from 2 instead of 1, for example, Table 2.: Labels instead of Table 1. : Labels.

Here is the code I am using and its output:

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}

\begin{table} \centering \refstepcounter{table} \caption{Example} \begin{tabular}{lllllll} \hline \textbf{No} & \textbf{One-text} & \textbf{One-text} & \textbf{One-or-text} (4,3,4)& \textbf{one-or-text} (8,4,2)& \textbf{One-or-text} (4,3,2)& One-or-text (4,2,2)\ \hline 1- & 0.0028 & 0 & 0 & 0 & 0 & 0 \ 2- & 0.0063 & 0 & 0.0025 & 0.00018 & 0.0000893 & 0 \ \hline \end{tabular} \end{table}

\end{document}

enter image description here

David Carlisle
  • 757,742
Gze
  • 113

1 Answers1

1
  • To fix the numbering issue, omit the instruction \refstepcounter{table}.

  • As you've (re)discovered, the l column type does not allow automatic line breaking. To allow automatic line breaking in the 6 data columns, while simultaneously setting the overall width of the table to \textwidth, I suggest you employ a tabularx envirohment, as is shown in the code below.

enter image description here

\documentclass{article}
\usepackage{tabularx,ragged2e}
\newcolumntype{L}{>{\RaggedRight\hspace{0pt}}X} 
    % allow line breaking, suppress full justfication

\begin{document} \begin{table} %%\centering % <-- not needed %%%%\refstepcounter{table} %<-- delete or comment out this instruction \caption{Example} \begin{tabularx}{\textwidth}{@{} l *{6}{L} @{}} \hline \textbf{No} & \textbf{One-text} & \textbf{One-text} & \textbf{One-or-text} (4,3,4) & \textbf{one-or-text} (8,4,2) & \textbf{One-or-text} (4,3,2) & One-or-text (4,2,2)\ \hline 1- & 0.0028 & 0 & 0 & 0 & 0 & 0 \ 2- & 0.0063 & 0 & 0.0025 & 0.00018 & 0.0000893 & 0 \ \hline \end{tabularx} \end{table} \end{document}

Mico
  • 506,678
  • thank you so much, but could you please explain this line for me \begin{tabularx}{\textwidth}{@{} l *{6}{L} @{}} ? – Gze Aug 11 '21 at 17:31
  • 1
    @Gze - The tabularx environment, unlike the tabular environment, takes two [2] arguments: (i) the desired width (here: \textwidth), and (ii) the column definitions, here: l *{6}{L}, which means "1 column of type l and 6 columns of type L. (The L column type is defined in the preamble as a variant of the X column type that's provided by the tabularx package.) Finally, the @{} particles instruct LaTeX to suppress whitespace padding on the left-hand and right-hand edges of the table. – Mico Aug 11 '21 at 19:59