3

Let's say I want my tables to have the following look:

Desired table layout

I can do that as follows:

\documentclass{memoir}
\usepackage[table]{xcolor}

\begin{document}

\begin{table}[h!]
    \centering
    \rowcolors{2}{green!15}{gray!5}
    \begin{tabular}{ll}
        \rowcolor{green}
        \textbf{Who} & \textbf{Dynasty}\\
        Djet & First Dynasty\\
        Neferkare VII & Ninth Dynasty \\
        Bakenranef & Twenty-Fourth Dynasty\\
    \end{tabular}
    \caption{Other pharaohs and their dynasties}
\end{table}

\end{document}

But I'd want all the tables in the document to have that look, and doing it directly in each such table seems needlessly error-prone, labor-intensive, and (depending on the size of the document) difficult to change. So I'd like to abstract the table styling out. Something like:

\documentclass{memoir}
\usepackage[table]{xcolor}
\usepackage{xparse}

\begin{document}

\NewDocumentEnvironment{BobTable}{mm}{
    \begin{table}[h!]
        \centering
        \rowcolors{2}{green!15}{gray!5}
        \begin{tabular}{#1}
            \rowcolor{green}
} {
        \end{tabular}
        \caption{#2}
    \end{table}
}

\begin{BobTable}{ll}{Other pharaohs and their dynasties}
    \textbf{Who} & \textbf{Dynasty}\\
    Djet & First Dynasty\\
    Neferkare VII & Ninth Dynasty\\
    Bakenranef & Twenty-Fourth Dynasty\\
\end{BobTable}

\end{document}

Which is good as far as it goes, but note that I'm still directly bolding the individual parts of the header line. I would like to abstract that out as well -- "the individual parts of the first row are always bolded" -- so I can just do something like:

\begin{BobTable}{ll}{Other pharaohs and their dynasties}
    Who & Dynasty\\
    Djet & First Dynasty\\
    Neferkare VII & Ninth Dynasty\\
    Bakenranef & Twenty-Fourth Dynasty\\
\end{BobTable}

I have thus far been unable to figure out a way to do so. Is there some small tweak that can be made to my idea, or some fundamentally different approach, that will let me abstract that out too? Thanks.

  • There's a package called cellprops which tries to achieve this. Note: Due to some lacking updates, it won't work with the latest packages out there, but there's a fixed version… – TeXnician Jul 10 '19 at 4:38
2

The problem is that there are hooks missing at the begin of cells, so it is difficult with the standard columns types. With new columntypes it can be done e.g. like this:

\documentclass{memoir}
\usepackage[table]{xcolor}
\usepackage{xparse}
\usepackage{array}


\ExplSyntaxOn
\tl_new:N\g__bob_tabcellhook
\newcolumntype{\bobl}{>{\g__bob_tabcellhook}l}
\cs_new:Nn\__bob_tabcell_hook_set:
 {
    \tl_gset:Nn \g__bob_tabcellhook {}
 }
\NewDocumentEnvironment{BobTable}{mm}
  {
    \begin{table}[h!]
        \centering
        \rowcolors{2}{green!15}{gray!5}
        \tl_gset:Nn \g__bob_tabcellhook {\bfseries}
        \begin{tabular}{#1<{\__bob_tabcell_hook_set:}}
            \rowcolor{green}
  } 
  {
        \end{tabular}
        \caption{#2}
    \end{table}
  }
\ExplSyntaxOff
\begin{document}
\begin{BobTable}{\bobl\bobl}{Other pharaohs and their dynasties}
    Who & Dynasty\\
    Djet & First Dynasty\\
    Neferkare VII & Ninth Dynasty\\
    Bakenranef & Twenty-Fourth Dynasty\\
\end{BobTable}

\begin{BobTable}{\bobl\bobl}{Other pharaohs and their dynasties}
    Who & Dynasty\\
    Djet & First Dynasty\\
    Neferkare VII & Ninth Dynasty\\
    Bakenranef & Twenty-Fourth Dynasty\\
\end{BobTable}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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