1

I've spent over an hour trying to create a simple 4x3 table on latex. Nothing fancy, jus what I'd have done with word. Here is my code:

\begin{table}[H]
    \centering
    \caption{\textbf{some caption}}
    
  \begin{tabular}{|l|l|l|l|}
    \hline
    
    \textbf{Test (µm)} & \textbf{ (Some really really really long name )} & \textbf{Name name mname} & \textbf{more name)} \\
   \hline  1 & 1 & 1 & 1 \\
   \hline  1 & 1 & 1 & 1 \\
      \hline  1 & 1 & 1 & 1 \\
         \hline  1 & 1 & 1 & 1 \\
\hline
  \end{tabular}
\end{table}

unfortunately, the table keeps overflowing and gets missing as you can see from the screenshot below:

enter image description here

I'd really appreaciate advice about how to fix this.

2 Answers 2

1

First, avoid the use of H for placement. If you don't want the table to float, then don't make it a float by removing the table environment.

If you don't instruct LaTeX that line breaks are possible, then it's normal that columns become too wide. Use a p-type column. For example:

% !TeX spellcheck = fr_FR

\documentclass[science]{titiarticle}

\begin{document}

\begin{table}[!htb]
  \centering
  \caption{\textbf{some caption}}
  \begin{tabular}{|l|p{4cm}|p{4cm}|l|}
    \hline
    \textbf{Test (µm)} & \textbf{ (Some really really really long name )} & \textbf{Name name mname} & \textbf{more name)} \\
    \hline  1 & 1 & 1 & 1 \\
    \hline  1 & 1 & 1 & 1 \\
    \hline  1 & 1 & 1 & 1 \\
    \hline  1 & 1 & 1 & 1 \\
    \hline
  \end{tabular}
\end{table}

\end{document}

If you want more flexibility or other controls, you should use the more modern tabularray package.

0

You need to allow (more or less) automatic line breaking in the header cells. However, the basic column types l, c, and r do not allow automatic line breaking.

There are many ways to allow line breaking in LaTeX tables.. One such way is to load the tabularx package and employ the tabularx environment and the X column type. In the following solution, columns 2 thru 4 employ (a modified form of) the X column type.

enter image description here

\documentclass[12pt]{article}
\usepackage{tabularx} % for tabularx env. and X column type
\usepackage{ragged2e} % for \RaggedRight macro
\newcolumntype{L}{>{\RaggedRight}X} % ragged-right

\begin{document}
\begin{table}[ht]
\caption{Some caption\strut}

\begin{tabularx}{\textwidth}{|l|L|L|L|}
\hline
Test (µm) & (Some really really really long name) & Name name name & more name \\
\hline  1 & 1 & 1 & 1 \\
\hline  1 & 1 & 1 & 1 \\
\hline  1 & 1 & 1 & 1 \\
\hline  1 & 1 & 1 & 1 \\
\hline
\end{tabularx}
\end{table}
\end{document}

You must log in to answer this question.

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