3

Some time ago i referred to this stack exchange to solve the problem of wrapping long text in a longtabu cell. The provided solution worked, but now my requirements have changed to having to be able to print Spanish characters as well as English. So the wrap function no longer works. i was wondering how to accomplish this? im able to print Spanish characters outside the wrap function as shown in the example. Thanks!

\documentclass[6pt]{extarticle}

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

\usepackage{longtable, tabu}
\usepackage{xparse} % used for \ExplSyntaxOn

%used to print Spanish characters in the text
\usepackage[utf8x]{inputenc}
\usepackage{amssymb}
\usepackage[english,spanish]{babel}

%%%%%%%%%%%%%%%%%%%
% CONFIGURATIONS  %
%%%%%%%%%%%%%%%%%%%

\tabulinesep=5mm % used to set the space between bottem and top of cell in a longtabu using option m in column definition

%%%%%%%%%%%%%%%%%%%
% Custom Commands %
%%%%%%%%%%%%%%%%%%%

\ExplSyntaxOn
\NewDocumentCommand{\separator}{ }
 {
  \mode_if_math:TF { \allowbreak } { \- }
 }
\NewDocumentCommand{\wrap}{ m } % use this command to wrap text \wrap{text goes here}
 {
  \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

%%%%%%%%%%%%%%%%
% Report Start %
%%%%%%%%%%%%%%%%
\begin{document}

Spanish Characters: á, é, í, ó, ú, ü, ñ, ¿, ¡

\begin{longtabu} to \textwidth {
    |X[1,l,m]|
    X[1,l,m]|
    X[1,l,m]|
    }
    \hline
    \wrap{á tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} 
    \\\hline
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} 
    \\\hline
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} 
    \\\hline
\end{longtabu}
\end{document}

enter image description here

11
  • Welcome to TeX.SX! A failing example would be better. – egreg Jul 21 '14 at 16:27
  • Hey, So i added a Spanish character in the wrap function inside the longtabu. the code is now failing. thanks – SteveM Jul 21 '14 at 16:32
  • 1
    The easiest workaround is to type the special characters inside braces, like S{á}nchez. This only in the argument to \wrap, of course. – egreg Jul 21 '14 at 16:32
  • 1
    And \usepackage[utf8]{inputenc} (utf8x isn't really recommended). A single letter (separated from the rest by spaces, or at the beginning or end) needs two sets of braces. – egreg Jul 21 '14 at 16:38
  • 1
    k so, \wrap{{{á}} tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} & works. So i assume it will be alot more difficult to allow them automatically? – SteveM Jul 21 '14 at 16:45
2

For Unicode characters the routine to be used is more complex. This seems to work, at least for the characters you need. Please, don't use utf8x.

\documentclass[6pt]{extarticle}

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

\usepackage{longtable, tabu}
\usepackage{xparse} % used for \ExplSyntaxOn

%used to print Spanish characters in the text
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage[english,spanish]{babel}

%%%%%%%%%%%%%%%%%%%
% CONFIGURATIONS  %
%%%%%%%%%%%%%%%%%%%

\tabulinesep=5mm % used to set the space between bottem and top of cell in a longtabu using option m in column definition

%%%%%%%%%%%%%%%%%%%
% Custom Commands %
%%%%%%%%%%%%%%%%%%%

\ExplSyntaxOn
\NewDocumentCommand{\separator}{ }
 {
  \mode_if_math:TF { \allowbreak } { \- }
 }
\NewDocumentCommand{\wrap}{ m } % use this command to wrap text \wrap{text goes here}
 {
  \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
\tl_new:N \l_mathers_word_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
   {
    \mathers_scan_word:n { ##1 }
   }
  \seq_use:Nn \l_mathers_output_seq { ~ }
 }

\cs_new_protected:Npn \mathers_scan_word:n #1
 {
  \tl_clear:N \l_mathers_word_tl
  \__mathers_scan_word_aux: #1 \q_stop
  \seq_put_right:NV \l_mathers_output_seq \l_mathers_word_tl
 }

\cs_new_protected:Npn \__mathers_scan_word_aux:
 {
  \peek_meaning:NTF { \q_stop }
   {
    \use_none:n
   }
   {
    \peek_meaning:NTF ^^c3
     { \mathers_eat:NN }
     {
      \peek_meaning:NTF ^^c2
       { \mathers_eat:NN }
       { \mathers_eat:N }
     }
   }
 }
\cs_generate_variant:Nn \peek_meaning:NTF { c }
\cs_new_protected:Npn \mathers_eat:NN #1 #2
 { \tl_put_right:Nn \l_mathers_word_tl {#1#2\separator}\__mathers_scan_word_aux: }
\cs_new_protected:Npn \mathers_eat:N #1
 { \tl_put_right:Nn \l_mathers_word_tl {#1\separator}\__mathers_scan_word_aux: }


\ExplSyntaxOff

%%%%%%%%%%%%%%%%
% Report Start %
%%%%%%%%%%%%%%%%
\begin{document}

Spanish Characters: á, é, í, ó, ú, ü, ñ, ¿, ¡

\begin{longtabu} to \textwidth {
    |X[1,l,m]|
    X[1,l,m]|
    X[1,l,m]|
    }
    \hline
    \wrap{á éíóúüñ¿¡áé íóúüñ¿¡áé í óúüñ¿¡áéíóúüñ¿¡áéíóúüñ¿¡áéíóúüñ¿¡áéíóúüñ¿¡ é} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} 
    \\\hline
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} 
    \\\hline
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} &
    \wrap{ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt} 
    \\\hline
\end{longtabu}
\end{document}

enter image description here

Supporting other characters may require more checks with \peek_meaning:NTF; these are sufficient for Spanish characters.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.