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 \\
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 \\
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.
| 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
\row will break with a number of columns different from three.
– egreg
Mar 16 '13 at 23:23
\row a bit more intelligent so it'll work for any number of columns?
–
Mar 17 '13 at 10:50
\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
\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
\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
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

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.