[Tex/LaTex] Make every first line of table look the same

captionscolortables

I've been struggling recently with tables. And I've finally managed to get past it. However, I'm now trying to make every first line of my tables look the same (bold white on blue background color). And I really don't know how to do that.

I know I must use a \renewcommand. So my guess is that:

\documentclass[12pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{colortbl}
\usepackage{array}

\definecolor{Tab}{RGB}{79, 129, 189}
\renewcommand{
    tabular{
        <first lign of every tab>
    }{
        \rowcolor{blue} \textbf{\textcolor{white}}
    } \
\begin{document}

\begin{table}
\caption{Exemple of First Colored Lign}
\begin{tabular}{m{3cm}|m{10cm}}
    \rowcolor{Tab} \textbf{\textcolor{white}{HeaderLeft}} & \textbf{\textcolor{white}{HeaderRight}} \\
    \hline Acronym
        & Lorem ipsum dolor sit amet\\
    \hline Acronym
        & Lorem ipsum dolor sit amet \\
    \hline Acronym
        & Lorem ipsum dolor sit amet \\
    \hline 
    \end{tabular}
    \end{table}

    \begin{table}
    \caption{Exemple of Tab without a colored Lign}
    \begin{tabular}{m{3cm}|m{10cm}}
    \hline HeaderLeft & HeaderRight \\
    \hline Acronym
        & Lorem ipsum dolor sit amet\\
    \hline Acronym
        & Lorem ipsum dolor sit amet \\
    \hline Acronym
            & Lorem ipsum dolor sit amet \\
        \hline 
    \end{tabular}
    \end{table}

\end{document}

But, well, it doesn't work. :/ What is the caption for the first line? I'm lost.

Do you know how to apply a specific style to every first header line of every table in a LaTeX document?

Best Answer

Here's one possibility, using the method provided in this page of the UK TeX FAQ:

\documentclass[10pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{colortbl}
\usepackage{array}

\definecolor{Tab}{RGB}{79, 129, 189}

\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
}
\newcommand\MyTabHeadings{%
  \rowcolor{Tab}\rowstyle{\bfseries\color{white}}}

\begin{document}

\begin{table}
\centering
\caption{Exemple of First Colored Line}
\begin{tabular}{$l^l^l}
\MyTabHeadings
HeaderLeft & HeaderCenter & HeaderRight \\
text & text & text \\
text & text & text \\
text & text & text \\
\end{tabular}
\end{table}

\end{document}

enter image description here

The >{declaration} syntax provided by the array package allows to insert declaration directly in front of each entry of the column (there's also <{declaration} to insert declaration right after each entry of the column, but that's not relevant here).

In the format specification for the tabular, the first column has to be preceeded by $ and all other columns by ^ (those characters could be any other characters that don't usually appear in the format specification of a table).

When \MyTabHeadings is issued, \rowstyle is set, the style is applied in this column and stored in \currentrowstyle so it can be applied for the ^ columns.

For a normal row (one in which \MyTabHeadings is not issued), \currentrowstyle is set to \relax, so the ^ does nothing and the row doesn't change.

Related Question