4

(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?

Werner
  • 603,163
ivo Welch
  • 3,766
  • 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 04 '14 at 19:43

3 Answers3

4

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

egreg
  • 1,121,712
Werner
  • 603,163
2

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

  • 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 05 '14 at 01:08
0

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 \\
Fran
  • 80,769