9

Hey im looking to create a text wrap command to be used inside a table for long word wrapping. so far i have this:

\renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi}
\newcommand{\wrap}[1]{\seqinsert{\seqsplit{#1}}} % used to wrap really long words

This is working good. The problem is the seqsplit is removing the spaces. How to preserve the spaces in the text? The data coming in the table is dynamic, so gluing the words together with "~" or "/ " wont work.

Ive tried using:

\newcommand{\wrap}[1]{\seqinsert{\seqsplit{\StrSubstitute{#1}{ }{\~{}}}}} % used to wrap really long words

but this errors out. Id like to achieve this on the latex side so programmers using my template dont have to worry about formatting. any help will be great thanks

sample code:

\documentclass{report}

%%%%%%%%%%%%%%%%%%%%%%
% Package Inclusions %
%%%%%%%%%%%%%%%%%%%%%%

\usepackage{lastpage}
\usepackage{caption}
\usepackage{geometry}
\usepackage{tabularx}
\usepackage{hhline}
\usepackage[table]{xcolor}
\usepackage{placeins}
\usepackage{setspace}
\usepackage{fancyhdr}
\usepackage{float}
\usepackage{tabu}
\usepackage{longtable}
\usepackage{seqsplit}
\usepackage{xstring}
\usepackage{multicol}
\usepackage{multirow}


%%%%%%%%%%%%%%%%%%%%%%
%   Configurations   %
%%%%%%%%%%%%%%%%%%%%%%

\captionsetup[table]{labelformat=empty}
\definecolor{lightgray}{gray}{0.9}
\newcolumntype{Y}{>{\centering\arraybackslash}X} % Use Y to center align a column
\geometry{
  top=0.75in,            
  inner=0.25in,
  outer=0.25in,
  bottom=0.75in,
  headheight=3ex,      
  headsep=6ex,          
}
\pagestyle{fancy}
\renewcommand{\baselinestretch}{1}
\renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi}
\newcommand{\wrap}[1]{\seqinsert{\seqsplit{#1}}} % used to wrap really long words
\newcommand{\wrapb}[1]{\textbf{\seqinsert{\seqsplit{#1}}}} % used to wrap really long words and bold it

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Titles, Headers and Footers   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\author{XXXXXXXXXX}
\title{XXXXXXX}
\lhead{XXXXXXXXXXXXXXX}
\chead{XXXXXXXXXXXXXX}
\rhead{XXXXXXXXXXXXXXXXXX}
\lfoot{Page \thepage\ of \pageref{LastPage}}
\cfoot{XXXXXXXXXXXXXXXXXXXXXXXX}
\rfoot{\fancyplain{}{\today}}


%%%%%%%%%%%%%%%%%%%%%%
%    Report Start    %
%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\maketitle
\section*{XXXXXXXXXXXXXXXXXXXX}


\begin{table}[H]
\rowcolors{1}{}{lightgray}
\begin{longtabu} to \textwidth {YYYYYYYYYYYYY}
    \hline
    \textbf{XXX} & 
    \textbf{XX} & 
    \textbf{XXX} & 
    \textbf{XXX} &
    \textbf{XXX} &
    \textbf{XXX} &
    \textbf{XXXX} &
    \textbf{XX} &
    \textbf{XXX} &
    \textbf{XXX} &
    \textbf{XXX} &
    \textbf{X} &
    \textbf{XXX}
    \\ \hline \hline \hline 
    12:28:33 & 1 & \wrap{tyry ery,CVBBBdsgsdgsdgdsgsg} & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\      
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\      
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    \hline
\end{longtabu}
\end{table} 

\end{document}
Steven Mathers
  • 355
  • 1
  • 2
  • 9
  • Could you add a complete example? What's \seqsplit? – egreg Jan 07 '14 at 22:46
  • sorry i cannot post a full version the information contained is sensitive. info on seqsplit can be found here: http://ctan.mirror.rafal.ca/macros/latex/contrib/seqsplit/seqsplit.pdf – Steven Mathers Jan 07 '14 at 22:50
  • 1
    I didn't ask for a full version; use an arbitrary sequence of letters. – egreg Jan 07 '14 at 22:51
  • K i posted a similar setup, have a look at the /wrap command – Steven Mathers Jan 07 '14 at 23:03
  • \seqinsert{\seqsplit{#1}} doesn't make any sense. – egreg Jan 07 '14 at 23:06
  • \seqsplit will split the long words to new lines but does not add the hyphen to make the split look natural. so thats why i have seqinsert redefined to insert that hyphen at the break point of the word. are you able to create a pdf from the sample code? you'll see what im talking about when you see the first row in the table. thanks! – Steven Mathers Jan 07 '14 at 23:10

1 Answers1

4

Here's a different implementation of \wrap that doesn't rely on \seqsplit, which is not made for respecting spaces.

\documentclass{report}

%%%%%%%%%%%%%%%%%%%%%%
% Package Inclusions %
%%%%%%%%%%%%%%%%%%%%%%

\usepackage{lastpage}
\usepackage{caption}
\usepackage{geometry}
\usepackage{tabularx}
\usepackage{hhline}
\usepackage[table]{xcolor}
\usepackage{placeins}
\usepackage{setspace}
\usepackage{fancyhdr}
\usepackage{float}
\usepackage{tabu}
\usepackage{longtable}
\usepackage{seqsplit}
\usepackage{xstring}
\usepackage{multicol}
\usepackage{multirow}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\separator}{ }
 {
  \mode_if_math:TF { \allowbreak } { \- }
 }
\NewDocumentCommand{\wrap}{ m }
 {
  \mathers_wrap:n { #1 }
 }

\seq_new:N \l_mathers_input_seq
\seq_new:N \l_mathers_output_seq
\seq_new:N \l_mathers_temp_seq

\cs_new_protected:Npn \mathers_wrap:n #1
 {
  \seq_clear:N \l_mathers_output_seq
  \seq_set_split:Nnn \l_mathers_input_seq { ~ } { #1 }
  \seq_map_inline:Nn \l_mathers_input_seq
   {
    \seq_set_split:Nnn \l_mathers_temp_seq { } { ##1 }
    \seq_put_right:Nx \l_mathers_output_seq
     { \seq_use:Nn \l_mathers_temp_seq { \separator } }
   }
  \seq_use:Nn \l_mathers_output_seq { ~ }
 }
\ExplSyntaxOff

%%%%%%%%%%%%%%%%%%%%%%
%   Configurations   %
%%%%%%%%%%%%%%%%%%%%%%

\captionsetup[table]{labelformat=empty}
\definecolor{lightgray}{gray}{0.9}
\newcolumntype{Y}{>{\centering\arraybackslash}X} % Use Y to center align a column
\geometry{
  top=0.75in,            
  inner=0.25in,
  outer=0.25in,
  bottom=0.75in,
  headheight=3ex,      
  headsep=6ex,          
}
\pagestyle{fancy}
\renewcommand{\baselinestretch}{1}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Titles, Headers and Footers   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\author{XXXXXXXXXX}
\title{XXXXXXX}
\lhead{XXXXXXXXXXXXXXX}
\chead{XXXXXXXXXXXXXX}
\rhead{XXXXXXXXXXXXXXXXXX}
\lfoot{Page \thepage\ of \pageref{LastPage}}
\cfoot{XXXXXXXXXXXXXXXXXXXXXXXX}
\rfoot{\fancyplain{}{\today}}


%%%%%%%%%%%%%%%%%%%%%%
%    Report Start    %
%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\maketitle
\section*{XXXXXXXXXXXXXXXXXXXX}


\begin{table}[H]
\rowcolors{1}{}{lightgray}
\begin{longtabu} to \textwidth {YYYYYYYYYYYYY}
    \hline
    \textbf{XXX} & 
    \textbf{XX} & 
    \textbf{XXX} & 
    \textbf{XXX} &
    \textbf{XXX} &
    \textbf{XXX} &
    \textbf{XXXX} &
    \textbf{XX} &
    \textbf{XXX} &
    \textbf{XXX} &
    \textbf{XXX} &
    \textbf{X} &
    \textbf{XXX}
    \\ \hline \hline \hline 
    12:28:33 & 1 & \wrap{tyry ery,CVBBBdsgsdgsdgdsgsg} & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\      
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\      
    12:28:33 & 1 & tyryery, CVBBB & -3750.00 & 1110.00 & 1110.00 & 1 to 20 & 1109.93 &
        1109.93 & 1 to 20 & 1 to 20 & 4651.59 & 4651.59 \\ 
    \hline
\end{longtabu}
\end{table} 

\end{document}

enter image description here

egreg
  • 1,121,712