3

I'd like to color the rows of a table to make it easy readable. Some cells have multiple lines, so I use the makecell package, and planned to use the xcolor for coloring, but they are clashing. Sample code:

\documentclass{article}
\usepackage{makecell}
%\usepackage[table]{xcolor}
\usepackage{bpchem}

\begin{document}
    %\rowcolors[1]{gray!25}{white}
    \begin{tabular}{ccc}
        \BPChem{H\_2O}&\BPChem{H\_3N}&\BPChem{H\_4C}\\
        \makecell{\BPChem{H\_2O\_2}\\unstable}&\BPChem{H\_4N\_2}&\BPChem{H\_6C\_2}\\
        not exists&\makecell{\BPChem{H\_5N\_3}\\unstable}&\BPChem{H\_8C\_3}\\
    \end{tabular}
\end{document}
0

2 Answers 2

2

The \rowcolors command needs 3 mandatory arguments. As I don't have bpchem installed, I loaded chemformula, which anyway has a simpler syntax for compounds. I took the liberty to change the shade of grey for a nicer one, and added some vertical padding at the top of rows:

\documentclass{article}
\usepackage{makecell}
\usepackage[table, svgnames]{xcolor}
\usepackage{chemformula}

\begin{document}

\begin{table}\setlength\extrarowheight{2pt}
 \rowcolors{1}{Lavender!80!gray}{white}
    \begin{tabular}{ccc}
    \ch{H2O}&\ch{H3N}&\ch{H4C}\\
        \makecell[t]{\ch{H2O2}\\unstable}&\ch{H4N2}&\ch{H6C2}\\
        not exists&\makecell[t]{\ch{H5N3}\\unstable}&\ch{H8C3}\\
    \end{tabular}
\end{table}

\end{document} 

enter image description here

4
  • I didn't take to the '\rowcolors' error, because any sequence brings option clash. Sep 5, 2016 at 17:40
  • Ooops, your sample worked for me too... Mine too... Then I found that the xcolor clashes not with makecell, but the beamer class. Thanks for the help and sorry for wasting your time! Sep 5, 2016 at 17:48
  • 1
    It's a classical problem: you want to load a package with some options, and another package already loads it with other (non incompatible) options. Two solutions: either load your options via the document class, or add to the preamble \PassOptionsToPackage{options list}{packagename}.
    – Bernard
    Sep 5, 2016 at 17:55
  • 1
    For beamer, one should add xcolor=table to the class options.
    – egreg
    Sep 5, 2016 at 17:59
1

With {NiceTabular} of nicematrix (you won't see any white lines in the colored panels).

\documentclass{article}
\usepackage{nicematrix}
\usepackage{chemformula}

\begin{document}

\begin{table}\setlength\extrarowheight{2pt}
\begin{NiceTabular}{ccc}
\CodeBefore
\rowcolors{1}{gray!15}{}
\Body
   \ch{H2O}&\ch{H3N}&\ch{H4C}\\
   \Block[t]{}{\ch{H2O2}\\unstable}&\ch{H4N2}&\ch{H6C2}\\
   not exists&\Block[t]{}{\ch{H5N3}\\unstable}&\ch{H8C3}\\
\end{NiceTabular}
\end{table}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

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.