Prevent word wrap and new lines in table cells

formattinghyphenationline-breakingtablesxltabular

I have a xltabular table which is filled via script with data, so I don't really know what length the text has that is put into the table cells. Is there a function or method which can just cut off the text if it is too long to fit in one line? Here is the example of my table.

\documentclass[
fontsize=11pt,
parskip=half,
firstfoot=off,
foldmarks=off
]{scrlttr2}  
%

\usepackage[T1]{fontenc} 
\usepackage[ngerman]{babel} 
\usepackage{lmodern}  

\usepackage{xltabular}
\usepackage[textwidth=\useplength{firstfootwidth}]{geometry} %showframe
               
\renewcommand*\familydefault{\sfdefault}    




\begin{document} 

%recipient  
\begin{letter}{ 
Max Muste \\
Teststraße 2    \\
55555 Teststadt \\
Schweiz         \\ 
     }
% 
\opening{}
%
    
{\footnotesize
\begin{xltabular}{\textwidth}{@{\hskip0pt}r p{3.1cm} X r r r@{\hskip0pt}}

Menge & Artikelnummer & Bezeichnung & WHG & Einzelpreis & Gesamtpreis \\ \hline 
\endfirsthead

Menge & Artikelnummer & Bezeichnung & WHG & Einzelpreis & Gesamtpreis \\ \hline 
\endhead
\hline
\multicolumn{6}{l}{Bla}
\endlastfoot
1 & T-ERICSSON GF768 & Lötzinn bleifrei mit Silber- und Kupferanteil,Ø 1,0 mm, 12,5 g & 0 N & 12,77300 & 12,77\\ *[\fill]
\end{xltabular}
}

\end{letter} 
\end{document} 

enter image description here

So basically mm, 12,5g should be thrown away, even if it is as text in the cell.

Best Answer

Well, there is not much sense to use an X column if you want to do that, an l ones will work ok, but one option is to use the truncate package (although you need a bit of gimmick to automatically apply it to a cell, here I use collcell package):

\documentclass[
fontsize=11pt,
parskip=half,
firstfoot=off,
foldmarks=off
]{scrlttr2}
%

\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{lmodern}

\usepackage{xltabular}
\usepackage[textwidth=\useplength{firstfootwidth}]{geometry} %showframe
\usepackage[fit, hyphenate]{truncate}
\usepackage{collcell}
\def\mytruncate{\truncate{6cm}}
\newcolumntype{T}{ >{\collectcell\mytruncate}X<{\endcollectcell} }

\renewcommand*\familydefault{\sfdefault}
\begin{document}

%recipient
\begin{letter}{
Max Muste \\
Teststraße 2    \\
55555 Teststadt \\
Schweiz         \\
     }
%
\opening{}

%
{\footnotesize
\begin{xltabular}{\textwidth}{@{\hskip0pt}r p{3.1cm} T r r r@{\hskip0pt}}

Menge & Artikelnummer & Bezeichnung & WHG & Einzelpreis & Gesamtpreis \\ \hline
\endfirsthead

Menge & Artikelnummer & Bezeichnung & WHG & Einzelpreis & Gesamtpreis \\ \hline
\endhead
\hline
\multicolumn{6}{l}{Bla}
\endlastfoot
1 & T-ERICSSON GF768 & Lötzinn bleifrei mit Silber- und Kupferanteil,Ø 1,0 mm, 12,5 g & 0 N & 12,77300 & 12,77\\ *[\fill]
\end{xltabular}
}

\end{letter}
\end{document}

enter image description here

Related Question