Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to redefine the tabular environment, such that each for example

\begin{tabular}{c|c}
AAA&BBB\\
CCC&DDD\\
\end{tabular}

would produce (for example)

AAA,BBB;CCC,DDD;

Question 1 In order to redefine the &, i use the following :

\catcode`\&=\active
\gdef&{,}

Which works, but i don't know if it is safe. In particular, this works only if i put this outside from the definition of tabular, but i would prefer to have it inside, so that this definition vanishes when i quit the Tabular.

Question 2 I don't know how to redefine the \\.

share|improve this question
add comment

2 Answers

If you tack your redefinitions on to the start of the definition of \tabular they will just be in scope for the environment because of the group from \begin \end although catcode changes are never stable mid document, you can make & active and your definition will work in a top level tabular but will not work if the tabular is in a parbox or ams alignment or anywhere else where it has been read as a macro argument so already tokenised. It is better to make the & active at the start of the document and \let to a normal catcode 4 token so that it still works as an alignment point, then you can \def it to , in your special environment. The usual tabular definition of \\ is \@tabularcr (which has different definitions depending if array is loaded.

so if you want & to act as an aligment point most of the time but have a local definition to a comma, do something like the following. Note that the redefinition works even inside the \fbox argument and the active & works before and after the local redefinition. enter image description here

\documentclass{article}



    \let\savedtabular\tabular

    \catcode`&\active
    \catcode`A=4
    \let&A
    \catcode`A=11

\makeatletter
\def\commatable{%
    \def\tabular{\def&{,}\def\@tabularcr{;}\savedtabular}}
\makeatother

\begin{document}

\begin{tabular}{ll}
a&b\\
c&d
\end{tabular}

{\commatable


\begin{tabular}{ll}
a&b\\
c&d
\end{tabular}


\fbox{\begin{tabular}{ll}
a&b\\
c&d
\end{tabular}}

}


\begin{tabular}{ll}
a&b\\
c&d
\end{tabular}


\end{document}
share|improve this answer
    
Could you please explain more about "...\let to a normal catcode token..." ? –  nicolas roy Jun 23 '12 at 11:29
    
added code to show active & acting as a catcode 4 token (catcode 4 A but that doesn't really matter) and then locally switching to be defined to expand to a comma. –  David Carlisle Jun 23 '12 at 13:03
    
ok, now i understand your code. Actually, you let the old tabular act. I would prefer to completely rewrite (and simplify) the definition of tabular. At the end I want to be able to pass it into a \edef and a \write, but maybe it is a different question (see tex.stackexchange.com/q/60851/10157) –  nicolas roy Jun 24 '12 at 14:51
    
well if you redefine tabular not to be "tabular" then the question of redefining \\ in a tabular is moot:-) But still the point remains that its best not to change catcode mod document. –  David Carlisle Jun 24 '12 at 20:31
    
Yes, you're right. Actually, i needed to redefine \\, not only in tabular, but generally speaking. The point is, i did not know i can do it simply with \def\\{new def}... –  nicolas roy Jun 25 '12 at 8:31
add comment
\documentclass[]{report}
\usepackage{array}
\begingroup
\catcode`\&=\active
\gdef&{,}
\endgroup
\newcommand\semikolon{;}
\begin{document}

\begin{tabular}{>{\catcode`\&=\active\let\\\semikolon}c|ccc}
AAA&BBB&CCC&DDD\\
CCC&DDD\\
\end{tabular}

\begin{tabular}{c|ccc}
AAA&BBB&CCC&DDD\\
CCC&DDD\\
\end{tabular}
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.