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.
-
Do you want a line break in the cells with long text? Can you be more descriptive with what you're trying achieve. – likethevegetable Nov 23 '20 at 01:48
-
thanks. i want an automatic way of adjusting the coloumn width to fit the page width. for that, the text for some cells may appear in multiple lines. I do not want to split the text manual as what i did using \. – MWH Nov 23 '20 at 01:50
-
1Look into p columns, and the tabularx package. – likethevegetable Nov 23 '20 at 02:07
-
i did try these but the texts show very small – MWH Nov 23 '20 at 13:08
-
Add an excerpt of your code and show us what it looks like, and what you want changed. – likethevegetable Nov 23 '20 at 13:14
1 Answers
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
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
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}
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.
- 33



