4

I have a document with plenty of tables and I would like to apply the same formatting of the header row for all of them, without making manual modifications for each tabular.

I now use \AtBeginEnvironment and \rowcolors from the etoolbox package based on Bernard's solution for setting the background color in the preamble, but have not found anything similar for the font type and text color.

My current code is the following:

\documentclass{article}
\usepackage[table, svgnames]{xcolor} %\usepackage{array}
\usepackage{cellspace}
\usepackage{etoolbox}
\colorlet{headercolour}{DarkSeaGreen}
\colorlet{textcolour}{white}

\AtBeginEnvironment{tabular}{\rowcolors{1}{\ifnumless{\rownum}{2}{headercolour}{white}}{}}

\begin{document}

\begin{table}
  \centering
  \begin{tabular}{|*{4}{Sc}|}
    \hline
    \textbf{\color{white}Column1} & \textbf{\color{white}Column2} & \textbf{\color{white}Column3} & \textbf{\color{white}Column4} \\%%set formatting manually for each table
    \hline
    A & B & D & E \\
    \hline
        & & & F \\
    \hline
        & & & \\
    \hline
  \end{tabular}
\caption{Table 1}
\end{table}

\begin{table}
    \centering
    \begin{tabular}{|c|c|c|}
        \hline
        Header1 & Header2 & Header3\\
        cell1 & cell2 & cell3\\
        \hline

    \end{tabular}
\caption{Table2}
\end{table}
\end{document} 

With the first table formatted as wanted. That is the style I would like to set for each table automatically.

Any ideas how to do that? Thanks!

4

This is something where ConTeXt really shines. Observe how the actual table content contains zero visual markup

\setupTABLE
  [row][first]
  [background=color,
   backgroundcolor=darkgreen,
   color=white,
   style=bold,
   framecolor=black]

\starttext

\startplacetable[title={Table 1}]
  \startTABLE
    \NC Column1 \NC Column2 \NC Column3 \NC Column4 \NC\NR
    \NC A       \NC B       \NC D       \NC E       \NC\NR
    \NC         \NC         \NC         \NC F       \NC\NR
  \stopTABLE
\stopplacetable

\startplacetable[title={Table 2}]
  \startTABLE
    \NC Header1 \NC Header2 \NC Header3 \NC\NR
    \NC cell1   \NC cell2   \NC cell3   \NC\NR
  \stopTABLE
\stopplacetable

\stoptext

enter image description here

4

I propose to define new column types, L , C, R, which incorporate the font and text colour changes only if we're in first row, since the [table] option of xcolor defines a \rownum counter:

\documentclass{article}
\usepackage[table, svgnames]{xcolor}
\usepackage{cellspace}
\usepackage{etoolbox}
\colorlet{headercolour}{DarkSeaGreen}
\colorlet{textcolour}{yellow}

\AtBeginEnvironment{tabular}{\rowcolors{1}{\ifnumequal{\rownum}{1}{headercolour}{white}}{}}%
\newcolumntype{L}{>{\ifnumequal{\rownum}{1}{\sffamily\bfseries\color{textcolour}}}Sl}
\newcolumntype{C}{>{\ifnumequal{\rownum}{1}{\sffamily\bfseries\color{textcolour}}}Sc}
\newcolumntype{R}{>{\ifnumequal{\rownum}{1}{\sffamily\bfseries\color{textcolour}}}Sr}
\setlength{\cellspacetoplimit}{5pt}
\setlength{\cellspacebottomlimit}{5pt}

\begin{document}

\begin{table}[!htb]
  \centering
  \begin{tabular}{|*{4}{C}|}%
    \hline
Column1 & Column2 & Column3 & Column4 \\%%set formatting manually for each table
    \hline
    A & B & D & E \\
    \hline
        & & & F \\
    \hline
        & & & \\
    \hline
  \end{tabular}
\caption{Table 1}
\end{table}

\begin{table}[!htb]
    \centering
    \begin{tabular}{| CCC|}%
        \hline
 Header1 & Header2 & Header3 \\
  cell1 & cell2 & cell3\\
        \hline
    \end{tabular}
\caption{Table2}
\end{table}

\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.