8

I want to define a simple tabular environment that treats spaces as the column separators. So something like a b c d should be treated as a & b & c & d. I've tried using \StrSubstitute but it doesn't seem possible to cheat it to replace the space with an &. Is there any way to achieve this?

Thanks.

3
  • 3
    Could you add some background and motivation? What did you try with \StrSubstitute?
    – egreg
    Commented Nov 2, 2015 at 7:49
  • dee: Could you give feedback on the solutions here?
    – user31729
    Commented Nov 5, 2015 at 16:01
  • 1
    On trying again with \StrSubstitute, it actually does work. In other words something like \StrSubstitute{ }{&}{blah blah} does produce blah&blah, and works fine as long as you do the substitution in each line of the table. I was trying to use \StrSubstitute inside a {parse lines} environment, and that was failing. So \StrSubstitute is still not a good solution to create a new environment.
    – dee
    Commented Nov 6, 2015 at 19:48

3 Answers 3

16

The problem with a b c d approach is that multiple spaces between the content would be interpreted as additional column separators, i.e. erroneously typed space characters could be treated as 'extra tab alignment' characters

However, here is way how to do this:

The & character has catcode 4 (alignment tab) whereas space has catcode 10 (space).

It's necessary to give space the catcode 4 then (and & another one, say 10, to be enabled to use 'spaces' still in a column.

I applied this redefinition of catcodes inside of a new tabular environment which replicates basically the traditional tabular. If other features from other tabular environments should be used -- add them please yourself, I leave that as homework ;-)

Since this redefinition occurs in a group, it does not leak outside.

I've shown this approach and a traditional table, where the vertical lines are just used to provide the look of table and showing the additional spacings.

\documentclass{article}


\newenvironment{mytabular}[2][c]{%
  \catcode`\&=10
  \catcode`\ =4
  \begin{tabular}[#1]{#2}
}{%
\end{tabular}
}

\begin{document}

\begin{mytabular}{|*{5}{c|}}
a b c d a&word 
\end{mytabular}

\vskip\baselineskip
\begin{tabular}{|*{5}{c|}}
a & b &  c & d & a word
\end{tabular}


\end{document}

enter image description here

3
  • 1
    +1. Just in case it isn't entirely obvious to all, you may want to state explicitly that with this swapping of the catcodes of & and ` ` (space), multiple spaces are no longer allowed as they'd get misinterpreted as multiple column separators.
    – Mico
    Commented Nov 2, 2015 at 7:54
  • @Mico: Hm, well, I thought it was obvious, but I can add it explicitly ;-) Thanks for suggestion
    – user31729
    Commented Nov 2, 2015 at 7:57
  • This is beautiful, thanks! Exactly the sort of stuff I had in mind.
    – dee
    Commented Nov 6, 2015 at 19:46
10

Depending on the content, pgfplotstable might be appropriate:

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
    \pgfplotstabletypeset[col sep=space]{
A B C D
-2.35 -2.27 -32.94 134.32
}
\end{document}

enter image description here

1
  • This works for my purposes too!
    – dee
    Commented Nov 6, 2015 at 19:50
6

Here's a LuaLaTeX-based solution. It allows both single and multiple instances of whitespace characters -- space, tab, and newline characters -- to indicate a column separator. It works with all tabular, tabular*, tabularx, and tabulary environments.

Two comments:

  • If a cell contains two or more words separated by spaces, these spaces will need to be replaced with a special character before processing. In the example below, I suggest using the # character for "real" spaces.

  • To generate empty cells, use either {} or && (the latter without adjoining spaces). See the example below for applications.

The solution works as follows: A Lua function called sp2amp (short for "space to ampersand") is set up. It first checks if the input line being processed is inside a tabular, tabular*, tabularx, or tabulary environment; if so, it sets the Boolean variable ok_2_process to "true". (If you need to extend the scope of the function to include longtable environments, do let me know -- it's not difficult to generalize the if conditions.) If and only if ok_2_process is equal to "true", all instances of one more more whitesspace characters in the input line are replaced with &, and all instances of # are replaced with (a single space character). By assigning the Lua function to the process_input_buffer callback, its work is done at a very early stage of processing, before TeX's "eyes" -- let alone its "mouth" and "stomach" -- get to do their work.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{luacode,luatexbase}
\begin{luacode}
ok_to_process = false
function sp2amp ( line )
  if string.find ( line , "\\begin{tabular[%*xy]?}" ) then
     ok_to_process = true
  elseif string.find ( line , "\\end{tabular[%*xy]?}" ) then
     ok_to_process = false
  elseif ok_to_process then
      line = string.gsub ( line , "%s+", "&" )
      line = string.gsub ( line , "#", " " )
  end
  return line
end
luatexbase.add_to_callback( "process_input_buffer", sp2amp, "sp2amp")
\end{luacode}

\begin{document}
\begin{tabular}{|*{5}{l|}}
a  b c  
   d    A#complete#sentence.\\
e&&g {} i
\end{tabular}
\end{document} 
1
  • Thanks! I'm not familiar with Lua so I prefer the above solutions, but this works too!
    – dee
    Commented Nov 6, 2015 at 19:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .