TransWikia.com

Automatic line break in tabularx environment

TeX - LaTeX Asked by LourdesRiquelme on December 5, 2020

I’m trying to get a table that automatically inserts a line-break so some very long text can fit in the cell, instead of overflowing to the margins like it’s doing now. I tried with makecell but didn’t work.

I’ve also tried breaking the line manually (even if it’s not the ideal solution) with but again, didn’t work either.

It’s a complex table because it has some combined cells.

This is my attempt:

usepackage{multirow}
usepackage{tabularx}

begin{document}

begin{table}[htbp]
    centering
begin{tabularx}{textwidth}{|l|l|X|}
hline
     Allele & Guide RNA & Repair template 
     hline
     hline
     allele1 & GuideRNA#1 & ATTATCCATGGAACGTTCACCGTATTGTTCATCTTCAGcTGaCAACTTATATCGATTCTCGCGAATCTTCCACTTGC
     hline
     allele2 & begin{tabular}{@{}l@{}}
                   GuideRNA2.1 
                   GuideRNA2.2
                 end{tabular} & AATATTATACAAACgTCGAATATTGTACaTTGTCGATATTGTCGAACATATATTAATCCATATGTATATCTTCCTGAT
    hline
     allele3 & begin{tabular}{@{}l@{}}
                   GuideRNA3.1 
                   GuideRNA3.2 
                 end{tabular} & GACACTCGAAGAGAAGCTTCCGCAATATGTATGCTTTCAGTCAACACTACTCTATACCTCGAGCAAGGGTGATCGTCGTATTCGAGTACATACAATGTGCCTT
    hline
    allele4 & GuideRNA4 & AACGTTCACCGTATTGTTCATCTTCAGTTGGCAACTTATATCGATTCTCGaGAATCTTCCAtTgGCaTTCTACCACATTTACACATACGCCAAACGCCCAGTGATGTCTGGACCAGG
     hline
end{tabularx}
end{table}
end{center}

end{document}

but this is how it looks like:
[1

Ideally, it’d look like this one (made in Word):

enter image description here

One Answer

The strings in the final column aren't human-language "words" and thus won't be line-broken sensibly by TeX's default hyphenation algorithm. I suggest you use the seqsplit macro (provided by the seqsplit package) to force the automated insertion of suitable linebreaks.

I'd also like to encourage you to give the table a more open "look" by getting rid of all vertical rules and by employing fewer, but well-spaced, horizontal rules, using the macros of the booktabs package.

enter image description here

documentclass{article}
usepackage{tabularx,seqsplit}
renewcommandtabularxcolumn[1]{m{#1}}
usepackage{booktabs}

begin{document}
begin{table}[htbp]
begin{tabularx}{textwidth}{@{} llX @{}}
toprule
Allele & Guide RNA & Repair template 
midrule
allele1 & GuideRNA1 & 
seqsplit{ATTATCCATGGAACGTTCACCGTATTGTTCATCTTCAGcTGaCAACTTATATCGATTCTCGCGAATCTTCCACTTGC}
addlinespace
allele2 & begin{tabular}{@{}l@{}}
             GuideRNA2.1 
             GuideRNA2.2
          end{tabular} & 
seqsplit{AATATTATACAAACgTCGAATATTGTACaTTGTCGATATTGTCGAACATATATTAATCCATATGTATATCTTCCTGAT}
addlinespace
allele3 & begin{tabular}{@{}l@{}}
            GuideRNA3.1 
            GuideRNA3.2 
          end{tabular} & 
seqsplit{GACACTCGAAGAGAAGCTTCCGCAATATGTATGCTTTCAGTCAACACTACTCTATACCTCGAGCAAGGGTGATCGTCGTATTCGAGTACATACAATGTGCCTT}
addlinespace
allele4 & GuideRNA4 & 
seqsplit{AACGTTCACCGTATTGTTCATCTTCAGTTGGCAACTTATATCGATTCTCGaGAATCTTCCAtTgGCaTTCTACCACATTTACACATACGCCAAACGCCCAGTGATGTCTGGACCAGG}
bottomrule
end{tabularx}
end{table}
end{document}

Correct answer by Mico on December 5, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP