Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

(I am rolling my own little system to take my LaTeX file and convert it to reasonable HTML and Epub. I have run into the following problem.)

I would like to keep my tabulars very simple. There are some (header) cells that I would like to have broken at spaces.

\begin{tabular}{lr}
                & long column name \\
    interesting & 2
\end{tabular}

inside a tabular, using a \\ is hazardous. I also do not want to make my tabular programming very complex. Ideally, I want a macro

\begin{tabular}{lr}
                & \cellspacebreak{long column name} \\
    interesting & 2
\end{tabular}

that breaks spaces to lines, as if I had had \begin{minipage}{\widestwidth} long\\column\\name \end{minipage}. this is probably easier in LuaTeX, but I need a solution for pdfTeX. I also have a preference, not absolute, to stick to the standard tabular environment. both of these are necessitated by the fact that a number of users will use this, and they may not have very recent TeX installations or sophistication.

Does this exist?

share|improve this question
    
In order to generate the HTML or Epub, are you at all looking at the LaTeX code, or just processing the PDF output? –  Werner Mar 4 at 19:43

3 Answers 3

up vote 4 down vote accepted

Here are two options:

  1. Replace the space character with \\ using xstring:

    \documentclass{article}
    
    \usepackage{xstring}% http://ctan.org/pkg/xstring
    
    % Ideas from Output generated macro content with newline (http://tex.stackexchange.com/q/124810/5764)
    \newcommand{\cellspacebreak}[1]{%
      \saveexpandmode\expandarg\exploregroups
      \StrSubstitute{#1}{ }{\noexpand\\}[\cellwithlinebreaks]%
      \restoreexpandmode%
      \show\cellwithlinebreaks
      \begin{tabular}[t]{@{}c@{}}
        \cellwithlinebreaks
      \end{tabular}
    }    
    
    \begin{document}
    
    \begin{tabular}{lr}
                    & \cellspacebreak{long column name} \\
        interesting & 2
    \end{tabular}
    
    \end{document}
    
  2. Use LaTeX3 to iterate over your "space-separated list" if items:

    \documentclass{article}
    \usepackage{xparse}% http://ctan.org/pkg/xparse
    \ExplSyntaxOn
    
    % Taken from How to iterate over a comma separated list? (http://tex.stackexchange.com/q/159118/5764)
    \NewDocumentCommand{\cellspacebreak}{ m }
     {
      \begin{tabular}[t]{@{}c@{}}
      \xyz_break_items:n { #1 }
      \end{tabular}
     }
    
    \seq_new:N \l_xyz_input_seq
    \cs_new_protected:Npn \xyz_break_items:n #1
     {
      \seq_set_split:Nnn \l_xyz_input_seq { ~ } { #1 }
      \seq_use:Nn \l_xyz_input_seq { \\ }
     }
    
    \ExplSyntaxOff
    
    \begin{document}
    
    \begin{tabular}{lr}
                    & \cellspacebreak{long column name} \\
        interesting & 2
    \end{tabular}
    
    \end{document}
    

In both instances the output resembles:

enter image description here

share|improve this answer
    
I've taken the liberty of simplifying your LaTeX3 code –  egreg Mar 4 at 21:35
    
@egreg: Awesome! –  Werner Mar 4 at 21:40
    
Bruno deserves all credits for \seq_use:Nn, I believe. –  egreg Mar 4 at 21:42

A \Longstack could be the thing. The [l] causes left alignment in the stack ([c] and [r] are options, too, for center- and right-alignment). I also need to reset the longstack gapsize in the preamble, because \baselineskip is set to 0pt in a tabular. The default \Longstack uses spaces as the end-of-line separators, unless reset with \setstackEOL{\\} or some such reassignment.

\documentclass{article}
\usepackage{stackengine}
\edef\tmp{\the\baselineskip}
\setstackgap{L}{\tmp}
\def\cellspacebreak{\Longstack[l]}
\begin{document}
\begin{tabular}{lr}
                & \cellspacebreak{long column name} \\
    interesting & 2
\end{tabular}
\end{document}

enter image description here

share|improve this answer
1  
the darn system only allows me to count one answer as being the solution. this is a good second solution (and in fact the one I am using now). –  ivo Welch Mar 5 at 1:08

I am not sure if you are really looking for a p{} column, but at least for your example table this basic solution is enough:

\documentclass{article}
\begin{document}
\begin{tabular}{lp{1.5cm}}
                & long column name \\
    interesting & 2
\end{tabular}
\end{document}

MWE

Note that inside a wider column (p{5cm}, for example) you can force the line breaks with the \newline command:

                & long \newline column \newline  name \\
share|improve this answer
    
thx. indeed, this is different. good for reference for readers who do not know this. I did. –  ivo Welch Mar 4 at 23:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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