0

I have a table that has 6 columns and some rows. The issue is that some cells in some columns have long texts so they look weired and cannot fit with text width. I tried \makecell to split long text cells using \ manually, but the resulting table looks ood. See the attached figure as an exact example of what i am trying to generate (but with dummy texts). I do not want it to be like Excel. I need a professional way to produce the table in multiple lines per cell.

enter image description here

MWH
  • 199

1 Answers1

2

I don't exactly know how you want your text to be broken into several lines, here are two options I came up with:

Option 1

\hspace{0pt} with tabularx

First option

As you can see, it is very picky about where it breaks the words. If the table gets too narrow, the text starts overflowing.

Option 2

\collectcell\seqsplit...\endcollectcell with tabularx,seqsplit and collcell

Second option

With this, the text can break anywhere. BUT it looks like it deletes spacing between words. If you want this option and need spaces between your words, you can put the spaces inside curly brackets { } and they won't be broken/deleted.

Code

\documentclass[]{article}

\usepackage[margin=1cm]{geometry} % Changing page margin \usepackage{array} \usepackage{tabularx} \usepackage{seqsplit} \usepackage{collcell}

% >{\hsize=.5\hsize} Insert this into the column type definition to change column width (produces some glitches if used incorrectly) %\newcolumntype{s}{>{\hspace{0pt}\raggedright\arraybackslash}X} % OPTION 1 "Human"-like breaking \newcolumntype{s}{>{\collectcell\seqsplit}>{\raggedright\arraybackslash}X<{\endcollectcell}} % OPTION 2 Breaks everything

\renewcommand{\tabularxcolumn}[1]{m{#1}} % Vertical centering in cells \renewcommand{\seqinsert}{\ifmmode\allowbreak\else-\fi} % Inserts hyphens at the breakpoints

\begin{document}

\begin{tabularx}{250pt}{|s|s|s|s|s|} % 6 columns, table width = 250pt \hline Numbers & Long Strings & Numbers & Long Strings & Numbers \ \hline 1000000000{ }Big{ }Number & VeryVeryLongString with Number 524 & 1000000000 Big Number & VeryVeryLongString with Number 524 & 1000000000 Big Number \ \hline \end{tabularx}

\end{document}

enter image description here

BONUS

If you want to center text within the cells, just change \raggedright to \centering inside the column type declaration. If you need columns with different styles, just copy the already existing declaration, change what you need, give it a different letter and use it in the table.