11

I have a table where I have to execute the same command for every entry in the table. For example:

\ibibleverse{Matthew}(1:1) & \ibibleverse{Mark}(1:1) & \ibibleverse{Luke}(1:1) \\
\ibibleverse{Matthew}(1:2) & \ibibleverse{Mark}(1:2) & \ibibleverse{Luke}(1:2) \\
\ibibleverse{Matthew}(1:3) & \ibibleverse{Mark}(1:3) & \ibibleverse{Luke}(1:3) \etc.

Is there a way that I can make every entry in the column automatically execute the \ibibleverse command so that my table syntax can look like this instead:

1:1 & 1:1 & 1:1 \\
1:2 & 1:2 & 1:2 \\
1:3 & 1:3 & 1:3 \

The parentheses are part of the \ibibleverse command. The full command looks like \ibibleverse{Matthew}(1:1) and outputs "Matthew 1:1".

Of course, the header of the table should not have the \ibibleverse command.

Does this even make sense? Is it possible without repeating, by hand, \ibibleverse so many times?

David Carlisle
  • 757,742
jlconlin
  • 10,172

3 Answers3

10

You can use the collcell package to collect the cell of each row and apply the appropriate macro to the column. Below I defined the columns type W to apply to apply \ibibleverse{Matthew}{#1} macro, K column type to apply \ibibleverse{Mark}{#1}, and the E column type to apply \ibibleverse{Luke}{#1} (column names based on the last characters of Matthew, Mark, and Luke.

enter image description here

Code:

\documentclass{standalone}
\usepackage{collcell}

\newcommand{\ibibleverse}[2]{#1-#2}%

\newcommand{\Matthew}[1]{\ibibleverse{Matthew}{#1}}% \newcommand{\Mark}[1]{\ibibleverse{Mark}{#1}}% \newcommand{\Luke}[1]{\ibibleverse{Luke}{#1}}%

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

\newcolumntype{W}{>{\collectcell\Matthew}{l}<{\endcollectcell}} \newcolumntype{K}{>{\collectcell\Mark}{l}<{\endcollectcell}} \newcolumntype{E}{>{\collectcell\Luke}{l}<{\endcollectcell}}

\begin{document} \begin{tabular}{W K E} 1:1 & 1:1 & 1:1 \ 1:2 & 1:2 & 1:2 \ 1:3 & 1:3 & 1:3 \ \end{tabular} \end{document}


If you want to exclude header tows you can use the solution from Tables header row's: how to ignore the column type? to only apply these macros in you are not in a header row.

enter image description here

Code:

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

\newtoggle{inTableHeader}% Track if still in header of table \toggletrue{inTableHeader}% Set initial value \newcommand{\StartTableHeader}{\global\toggletrue{inTableHeader}}% \newcommand{\EndTableHeader}{\global\togglefalse{inTableHeader}}%

% Redefine tabular to initialize \StartTableHeader at start and end \let\OldTabular\tabular% \let\OldEndTabular\endtabular% \renewenvironment{tabular}{\StartTableHeader\OldTabular}{\OldEndTabular\StartTableHeader}%

\newcommand{\ibibleverse}[2]{#1-#2}%

\newcommand{\Matthew}[1]{\iftoggle{inTableHeader}{\textcolor{red}{#1}}{\ibibleverse{Matthew}{#1}}}% \newcommand{\Mark}[1]{\iftoggle{inTableHeader}{\textcolor{blue}{#1}}{\ibibleverse{Mark}{#1}}}% \newcommand{\Luke}[1]{\iftoggle{inTableHeader}{\textcolor{brown}{#1}}{\ibibleverse{Luke}{#1}}}%

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

\newcolumntype{W}{>{\collectcell\Matthew}{c}<{\endcollectcell}} \newcolumntype{K}{>{\collectcell\Mark}{c}<{\endcollectcell}} \newcolumntype{E}{>{\collectcell\Luke}{c}<{\endcollectcell}}

\begin{document} \begin{tabular}{W K E} Mathew & Mark & Luke \EndTableHeader\ 1:1 & 1:1 & 1:1 \ 1:2 & 1:2 & 1:2 \ 1:3 & 1:3 & 1:3 \ \end{tabular} \end{document}

Peter Grill
  • 223,288
  • I got your example to work, but I couldn't get my non-mwe example to work. I failed to mention in my original question that the parentheses are part of the \ibibleverse command. Please see modified question. I like this answer otherwise. – jlconlin Jun 14 '12 at 04:51
  • 1
    Can you just provide a complete MWE, especially the definition of \ibibleverse, as it is not clear to me what exactly the problem is. As I see it now, you can just replace the dash in my definition of \ibibleverse with a space. – Peter Grill Jun 14 '12 at 04:57
7

You could also use the array package

enter image description here

\documentclass{article}
\usepackage{array}
\newcommand{\ibibleverse}[1]{#1}
\newcolumntype{B}[1]{>{\ibibleverse{#1} (}c<{)}}

\begin{document}

\begin{table}
    \centering
        \begin{tabular}{B{Matthew}B{Mark}B{Luke}}
            1.1     &   1.1   &   1.1\\
            1.2     &   1.2   &   1.2\\
            1.3     &   1.3   &   1.3
        \end{tabular}
\end{table}
\end{document}
David Carlisle
  • 757,742
cmhughes
  • 100,947
  • This almost works. The trouble is that the parentheses are part of the \ibibleverse command. (Should have stated that the command comes from the bibleref package. The entire command is \ibibleverse{Matthew}(1:1) and it outputs "Matthew 1:1". – jlconlin Jun 14 '12 at 04:36
  • @Jeremy this is one of the main reasons that you should pretty much always provide a MWE – cmhughes Jun 14 '12 at 16:23
  • Sorry. I meant for my example to be complete. I guess I was a little hasty. – jlconlin Jun 15 '12 at 00:46
1

Since there's no formatting required for the contents of the tabular, I would just box it and then pass it to \ibibleverse:

enter image description here

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array
\usepackage{booktabs}% http://ctan.org/pkg/booktabs

\def\ibibleverse#1(#2){#1\ #2}%

\newsavebox{\mybox} \newcolumntype{V}[1]{>{\begin{lrbox}{\mybox}}l<{\end{lrbox}\ibibleverse{#1}(\usebox{\mybox})}}

\begin{document} \begin{tabular}{V{Matthew} V{Mark} V{Luke}} \toprule \multicolumn{1}{c}{Matthew} & \multicolumn{1}{c}{Mark} & \multicolumn{1}{c}{Luke} \ \midrule 1:1 & 1:1 & 1:1 \ 1:2 & 1:2 & 1:2 \ 1:3 & 1:3 & 1:3 \ \bottomrule \end{tabular} \end{document}

The V{<book>} column type is defined using array's \newcolumntype, allowing for a generic column type where you can specify the Biblical <book>. Also, for the sake of presentation, booktabs provided the rules. \multicolumn removes the header from being formatted like the table entries.

Werner
  • 603,163