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.

Is there a way to modify the layout of cells in a tabular?

I would like to have a environment mytable which I would use like a tabular:

\begin{mytable}{l|l}
entry & entry \\
entry & entry 
\end{mytable}

which would create a tabular, and such that for each entry, it would insert some \mycommand at the beginning of each cell.

Is it possible?

share|improve this question
    
Take a look at the array package documentation. –  Alan Munn Dec 21 '11 at 20:16
    
Thanks a lot for the hint. I provides exactly what i need. –  nicolas roy Dec 21 '11 at 20:20
    
Duplicate of How to apply a macro to each column of a table. –  Alan Munn Dec 21 '11 at 20:21
    
@AlanMunn: The array package allows for custom column types, but don't think it provides any way to apply a macro to each cell of a table. That is what collcell is for. –  Peter Grill Dec 21 '11 at 20:25
    
yes it does : So, the solution is indeed with the package array : \begin{tabular}{>{\mycommand}l|>{\mycommand}l} aaaa & bbbb\ cccc & dddd \end{tabular} it works. –  nicolas roy Dec 21 '11 at 20:28
show 2 more comments

2 Answers

up vote 5 down vote accepted

collcell package:

You can use the collcell package to process an entry based on the column type. Below I have defined the L column type applied this to the first column which, in this example, changes the text color to red.

enter image description here

If you want to apply the same macro to each column use the L column type for that column. You can also define additional column types and apply different macros to cells in different columns.

\documentclass{standalone}
\usepackage{xcolor}
\usepackage{collcell}

\newcommand{\MyCommand}[1]{\textcolor{red}{#1}}

\newcolumntype{L}{>{\collectcell\MyCommand}{l}<{\endcollectcell}}

\begin{document}
\begin{tabular}{L | l}
entry & entry \\
entry & entry 
\end{tabular}
\end{document}

array package:

If you want to apply a macro that does not need access to the cell contents you can simply use the array package and don't need collcell.

enter image description here

\documentclass{standalone}
\usepackage{array}

\newcolumntype{L}{>{\begin{math}}l<{\end{math}}}%

\begin{document}
\begin{tabular}{L | l}
entry & entry \\
entry & entry 
\end{tabular}
\end{document}

Macro To Process Each Row:

Alternatively, if you want you could add a macro for each row and apply the particular commands within that macro:

enter image description here

\documentclass{standalone}
\usepackage{xcolor}

\newcommand{\MyCommand}[2]{\textcolor{red}{#1}&\textcolor{blue}{#2}}

\begin{document}
\begin{tabular}{l | l}
\MyCommand{entry}{entry} \\
\MyCommand{entry}{entry} 
\end{tabular}
\end{document}
share|improve this answer
    
Perhaps, if the OP has a fixed structure (like in the original question; a 2x2 matrix), you could also provide, as an alternative, a macro that takes 4 arguments and formats them accordingly. It would be very specific, but it may appeal to the OP. –  Werner Dec 21 '11 at 20:26
    
@Werner: I did not read the question that way since the OP specifically states "insert some at the beginning of each cell". Form this I assume he wants cell by cell processing, not row by row processing. –  Peter Grill Dec 21 '11 at 20:29
add comment

The following solution assumes two things:

  1. a fixed 2x2 matrix structure; and
  2. you want to apply the same command to all the entries.

The following MWE provides \mymatrix[<col spec>]{<tl>}{<tr>}{<bl>}{<br>} that places the four mandatory arguments in a left-to-right, top-down (or row-by-row) fashion in a 2x2 tabular.

enter image description here

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newcommand{​\mymatrix}[5][l|l]{% \mymatrix[<col spec>]{<tl>}{<tr>}{<bl>}{<br>}
  \begin{tabular}{#1}
    \MyCommand{#2} & \MyCommand{#3} \\ \MyCommand{#4} & \MyCommand{#5}
  \end{tabular}
​}
\begin{document}
\newcommand{\MyCommand}[1]{#1}% \MyCommand{<stuff>} just prints <stuff> untouched
\mymatrix{a}{b}{c}{d}

\bigskip

\renewcommand{\MyCommand}[1]{\textcolor{red}{#1}}% \MyCommand{<stuff>} prints <stuff> in red
\mymatrix{a}{b}{c}{d}

\bigskip

\renewcommand{\MyCommand}[1]{\textcolor{blue!50}{\textsf{#1}}}% \MyCommand{<stuff>} prints <stuff> in 50% blue and serif font
\mymatrix[|p{2in}|l|]{a}{b}{c}{d}% Also changed the column specification
\end{document}
​

The optional command to \mymatrix[<col-spec>] allows you to (re)specify the tabular column specification, which defaults to l|l. The last command utilizes this modification.

share|improve this answer
    
thanks for the idea, but i don't want to fix the size of the array in advance. –  nicolas roy Jan 2 '12 at 9:42
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.