TeX - LaTeX Asked on August 18, 2021
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.
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
collectcellseqsplit
...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=.5hsize} Insert this into the column type definition to change column width (produces some glitches if used incorrectly)
%newcolumntype{s}{>{hspace{0pt}raggedrightarraybackslash}X} % OPTION 1 "Human"-like breaking
newcolumntype{s}{>{collectcellseqsplit}>{raggedrightarraybackslash}X<{endcollectcell}} % OPTION 2 Breaks everything
renewcommand{tabularxcolumn}[1]{m{#1}} % Vertical centering in cells
renewcommand{seqinsert}{ifmmodeallowbreakelse-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.
Answered by Trolobezka on August 18, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP