15

I was wondering if it is possible to use the pipe character, |, instead of the & to separate cells.

Something like this:

c1 | c2 | c3 \\
c4 | c5 | c6 \\

instead of this:

c1 & c2 & c3 \\
c4 & c5 & c6 \\
Moriambar
  • 11,466
nunos
  • 1,019

3 Answers3

20

If you are not using | for anything else

\catcode`\|=4 
David Carlisle
  • 757,742
13

Instead of (inherently fragile) catcode trickery, as suggested by David, you could also define a simple helper macro with delimited arguments:

\documentclass{scrartcl}
\usepackage{array}
\begin{document}

  \def\row#1|#2|#3\\{#1 & #2 & #3 \tabularnewline}
  \begin{tabular}{ccc}
    \row c1 | c2 | c3 \\
    \row c4 | c5 | c6 \\
  \end{tabular}
\end{document}

Even though the extra \row at the beginning of each row is a bit more to type, it also brings you some extra flexibility: You now can easily rearrange, skip, or format columns, by just changing the definition of \row: For instance, \def\row#1|#2|#3\\{\textbf{#1} & & #3 \tabularnewline} would typset the first column in boldface and (maybe temporarily) skip the second column.

Moriambar
  • 11,466
Daniel
  • 37,517
  • Nice, but redefining the catcode of | is just as fragile as using & (assuming one does not use | anywhere else, which is quite probable. In fact, even \verb&verbatim text& works under the normal LaTeX catcode regime, by the way, so David's answer does not conflict with the usual \verb|blah|.) – mbork Mar 16 '13 at 22:40
  • Of course, your \row will break with a number of columns different from three. – egreg Mar 16 '13 at 23:23
  • 2
    @egreg: Sure, the idea is to define it for each table as needed. – Daniel Mar 17 '13 at 07:34
  • Having to redefine the command poses a bit of a burden on the user and may introduce errors. Why not make the \row a bit more intelligent so it'll work for any number of columns? –  Mar 17 '13 at 10:50
  • @MarcvanDongen: In my actual applications, \row is also used to define the formatting of table columns, which often is specific to a table anyway. In many cases I find this less of a burden and more understandable than dealing with \newcolumntype and other trickery. – Daniel Mar 17 '13 at 11:24
  • @Daniel Using TeX's pattern matching (delimiters) you can define the \row command parse the arguments and insert the columns and column separators on the fly. You have the newline (\\) at the end of the row and you can use this to detect the last column. –  Mar 17 '13 at 11:32
  • @MarcvanDongen: I know that I could do this. My point is that (a) I don't think redefining \row per table is much of a burden, especially as (b) I often use it also to implement formatting options for certain columns (such as math mode, a surrounding TikZ node, ...) which IMHO is way easier to do in the \row macro than by a fancy table preamble. So \row is specifically defined for each table anyway. – Daniel Mar 18 '13 at 16:25
7

Here's a ConTeXt solution using the database module. The idea is to define a separated list with a vertical bar as separator and table macros as delimiters. Example:

\usemodule [database]

\defineseparatedlist
  [Table]
  [separator=|,
   before=\bTABLE, after=\eTABLE,
   first=\bTR, last=\eTR,
   left=\bTD, right=\eTD]

\starttext
  \startTable
    alpha | beta   | gamma
    first | second | third
  \stopTable
\stoptext

result

As separators the strings comma (default), tab, or space can be used or just any character like I did with the vertical bar here.

The same technique can be used to typeset CSV data as well since the macros, separators and the quoting can be configured.

Marco
  • 26,055