7

I am using longtable to draw a table that spans a few pages. I also added code to alternate colors for rows in the table.

  1. Is there a way to colorize the first row with a different color?
  2. How can I ensure that the caption string is not colorized?

My MWE is below

\PassOptionsToPackage{table}{xcolor}
\documentclass{book}
\usepackage{fontspec}
\usepackage{longtable}

\renewcommand{\baselinestretch}{1.5}
\usepackage{titlesec}

\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}
\usepackage[dvipsnames]{xcolor}

\definecolor{light-gray}{HTML}{E5E4E2}
\definecolor{light-cyan}{HTML}{E0FFFF}


\begin{document}
\def\arraystretch{1}
\begin{center}
\rowcolors{1}{light-cyan}{light-gray}
\begin{longtable}{|p{.10\textwidth}| p{.98\textwidth}| p{.10\textwidth}|} 

\hline
serial num. & sutra & sutra number   \\
\hline
\rownumber & foo &1-1-56 \\
\hline
\rownumber  & bar & 1-1-57  \\
\hline
\caption{caption for this table}
\end{longtable}
\end{center}
\end{document}

Output from processing .tex file

6
  • 1
    Is everything in your MWE necessary to reproduce the problem? I ask because you are setting a high bar for getting help right now: you require helpers to have installed Sanskrit 2003 (or to track it down and install it, purchasing it if required). That's quite a lot to expect for somebody to just figure out if they can even reproduce the issue.
    – cfr
    Dec 29 '16 at 1:51
  • @cfr I can edit my example and remove the parts that require Sanskrit2003. Sorry about that. I copy+pasted a portion of my latex source so that it is a good representation of what I'm trying to achieve.
    – linuxfan
    Dec 29 '16 at 1:54
  • 1
    Have you tried \hiderowcolors/\showrowcolors?
    – cfr
    Dec 29 '16 at 1:55
  • 2
    Your table is too wide for the page, by the way. You must get bad box warnings with this.
    – cfr
    Dec 29 '16 at 1:58
  • \hiderowcolors and \showrowcolors answers my question #2. Thanks. I'll fix the table width too.
    – linuxfan
    Dec 29 '16 at 2:02
8

You can change row colour with \rowcolor{} for the header rows and use \hiderowcolors and \showrowcolors to avoid colouring the caption.

For example:

\documentclass{article}
\usepackage{longtable}
\usepackage[dvipsnames,table]{xcolor}

\definecolor{light-gray}{HTML}{E5E4E2}
\definecolor{light-cyan}{HTML}{E0FFFF}

\begin{document}
\rowcolors{1}{light-cyan}{light-gray}
\begin{longtable}{|*{3}{p{.3\textwidth}|}}
  \hiderowcolors
  \caption{Fine caption}\\
  \showrowcolors
  \hline
  \rowcolor{magenta}Head 1 & Head 2 & Head 3\\\hline\endfirsthead
  \hline
  \rowcolor{yellow}Head 1 & Head 2 & Head 3\\\hline\endhead
  \hline\endfoot
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
\end{longtable}
\end{document}

colouring tables

Note that I take no responsibility for failures of typographical aesthetics in this answer which should be understood as demonstrating how to implement a purely hypothetical set of desiderata.

That is, I don't recommend this and wouldn't do it, but if you must, you can.

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.