13

Looking at another question, I was wondering if there is a way to set a whole row of a tabular to math mode (without repeating $...$ for every cell) or a whole row of an array to text mode (without repeating \text{...} for every cell).

In Format whole row of table as bold this setting is done for a bold/italic/any-other-switch rows but can it be done also when the cell content should be the argument of a macro or the body of an environment?

\documentclass{article}
\usepackage{amsmath}

\begin{document}
    For example, how to set, once for all, the second row to text here:

    \[
    \begin{array}{*{14}{c}}
    \dfrac{17}{91} & \dfrac{78}{85} & \dfrac{19}{51} & \dfrac{23}{38} & \dfrac{29}{33} & & & & & & & & & \dfrac{55}{1} \\[10pt]
    \text{A} & \text{B} & \text{C} & \text{D} & \text{E} & \text{F} & \text{G} & \text{H} & \text{I} & \text{J} & \text{K} & \text{L} & \text{M} & \text{N} \\
    \end{array}
    \]

    Or the first row to math here:
    \begin{center}
    \begin{tabular}{*{14}{c}}
        $\dfrac{17}{91}$ & $\dfrac{78}{85}$ & $\dfrac{19}{51}$ & $\dfrac{23}{38}$ & $\dfrac{29}{33}$ & & & & & & & & & $\dfrac{55}{1}$ \\[10pt]
        A & B & C & D & E & F & G & H & I & J & K & L & M & N \\
    \end{tabular}   
    \end{center}

\end{document}

enter image description here

CarLaTeX
  • 62,716

2 Answers2

8

For the second case (make the whole row math) you can do something in spirit of the first answer in Format whole row of table as bold:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularx}
\newcommand\setrow[1]{\gdef\rowmac{\begin{#1}}\gdef\erowmac{\end{#1}}\begin{#1}\ignorespaces}
\newcommand\clearrow{\global\let\rowmac\relax \global\let\erowmac\relax}
\clearrow

\begin{document}
Or the first row to math here:
\begin{center}
\begin{tabular}{*{13}{>{\rowmac}c<{\erowmac}}>{\rowmac}c<{\erowmac\clearrow}}
\setrow{math} \dfrac{17}{91} &\dfrac{78}{85} & \dfrac{19}{51} &
\dfrac{23}{38} & \dfrac{29}{33} & & & & & & & & & \dfrac{55}{1} \\[10pt]
A & B & C & D & E & F & G & H & I & J & K & L & M & N \\
\setrow{bf}A & B & C & D & E & F & G & H & I & J & K & L & M & N \\
\end{tabular}   
\end{center}
\end{document}

produces

enter image description here

6

The following enables you to specify a command which's argument should be the content of each cell of the row for this it uses collcell. I also specified the two column types e and E both taking one argument which should be the column type to which the effects should be applied. E should be the last column in a row which should be affected as it resets the definition.

With this approach you can use amsmath's \text in an array or \ensuremath in a tabular to achieve both goals.

I created a macro \setrowC which takes a command as its argument and a \setrowE which takes an environment as its argument. They both work on the e and E type columns.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularx}
\def\setrowC#1#2&{%
  \gdef\rowmac{#1}\rowmac{#2}&}
\def\setrowE#1#2&{%
  \gdef\rowmac##1{\begin{#1}##1\end{#1}}%
  \rowmac{#2}&}
\newcommand*\clearrow{%
  \global\let\rowmac\relax}
\clearrow

\usepackage{collcell}
\newcolumntype{e}[1]{>{\collectcell\rowmac}#1<{\endcollectcell}}
\newcolumntype{E}[1]{e{#1}<{\clearrow}}

\begin{document}
Or the first row to math here:
\begin{center}
  \begin{tabular}{*{13}{e{c}}E{c}}
\setrowC{\ensuremath} \dfrac{17}{91} &\dfrac{78}{85} & \dfrac{19}{51} &
\dfrac{23}{38} & \dfrac{29}{33} & & & & & & & & & \dfrac{55}{1} \\[10pt]
A & B & C & D & E & F & G & H & I & J & K & L & M & N \\
\setrowE{bf}A & B & C & D & E & F & G & H & I & J & K & L & M & N \\
\end{tabular}   
\end{center}
\[
  \begin{array}[]{e{c}E{c}}
    \setrowC{\text} text & text\\
    \frac{5}{4} & \frac{4}{5}
  \end{array}
\]
\end{document}

If you want to have a \setrow with a starred and unstarred version, you can use the following (neither the builtin \@ifstar nor xparse did work so I defined \myifstar):

\def\setrow#1{%
  \myifstar{#1}%
    {\setrowE}%
    {\setrowC{#1}}%
  }
\makeatletter
\def\myifstar#1{%
  \if*#1
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}
\makeatother
Skillmon
  • 60,462