8

Is there a way to have LaTeX render the tabs in a tabular environment to determine the columns so that there is no further need for the ampersands & ?

Most of the time the columns only contain a word and I already align them in the source code with tabs. So it would come in handy to be able to leave the & out.

1
  • 3
    It's a very bad idea. Tabs are not set the same width across all editors, for one thing; even if they are set at 8 spaces, an entry more than 8 character long may require two tabs in another entry in the same column: chaos. Andrey's solution will catch those, but what about an empty cell, then?
    – egreg
    Commented Oct 3, 2011 at 17:08

2 Answers 2

14

It certainly can be done, although I do not recommend using this in practice. The reason: you can't clearly tell where the cells' boundaries are anymore. However, if you are the only one editing the document, you are free to do what you like.

\documentclass{article}

\makeatletter
\begingroup
  \catcode`\^^I=\active
  \gdef^^I{\@ifnextchar^^I{}{&}}
  \catcode`\^^M=\active
  \gdef^^M{\\}%
\endgroup
\newenvironment{mytabular}[1]{%
  \catcode`\^^I=\active
  \catcode`\^^M=\active
  \begin{tabular}{#1}%
}{%
  \end{tabular}%
}
\makeatother

\begin{document}

% Note two tabs after b
\begin{mytabular}{lll}
a   b       c
x   yyyyyyy z
\end{mytabular}

\end{document}

As a bonus, the newline ends a row. If you want an empty cell, put {} in place of its content. If you want to split a row into several source lines, append % to them, except for the last one.

1
  • @AndreyVihrov: It sounds like a new package ;-) Commented Oct 3, 2011 at 15:58
1

You can do something like

\newenvironment{tabbedtabular}[1]%
  {\catcode9=4\begin{tabular}{#1}}%
  {\end{tabular}\catcode9=10}

which puts the TAB character (ASCII 9) into category 4 (alignment, like & by default) before the environment, and back to category 10 (space) thereafter.

I'm not sure this is necessarily a good idea, though - it will make it very difficult to see whether, for instance, you've accidentally put two alignment characters in a row, or one in the wrong place. I'm sure others with more TeXpertise than me will chime in if this breaks anything else!

8
  • You don't need to restore the catcode because a LaTeX environment starts a group. Commented Oct 3, 2011 at 15:33
  • And 9 is not the tab character ;-) Commented Oct 3, 2011 at 15:37
  • @Andrey: yes, I saw that I didn't need to but I don't fully understand why: after all, I change the catcode /before/ beginning the group. And changing the catcode /inside/ the group doesn't work! (If this is too complicated to explain in a comment feel free to say so and I can ask it as a new question...)
    – Ant
    Commented Oct 3, 2011 at 15:44
  • 2
    @Andrey. The 9-th ascii char is a tab. @Ant. Dunno why putting \catcode9=4\relax after \begin{tabular}{#1} wouldn't work (note the \relax, to make sure that the assignment is complete before TeX reads further). The "begin" and "end" code for tabbedtabular are run within a group, which has nothing to do with the group created by the tabular environment. Commented Oct 3, 2011 at 15:51
  • 2
    @BrunoLeFloch Putting the \catcode assignment after \begin{tabular}{#1} performs it in the first cell of the alignment only (which is in an implicit group).
    – egreg
    Commented Oct 3, 2011 at 16:26

You must log in to answer this question.

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