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 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?

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

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}
share|improve this answer
    
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. –  Jeremy Jun 14 '12 at 4: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 4:57
add comment

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.

share|improve this answer
add comment

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}
share|improve this answer
    
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". –  Jeremy Jun 14 '12 at 4: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. –  Jeremy Jun 15 '12 at 0:46
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.