0

I've been looking around on the web since yesterday to look for answers. But what I'm looking for are boxed cell tables with alternating white/gray backgrounds with a black header row in white text. Plus points if it is bordered and can cover the entire page. I've tried to do this in tabularray, but the color scheming is something I'm new to and if anyone could explain how to efficiently do this in LaTeX in general then you have my thanks. Some examples of stuff I've tried are the following:

\documentclass{article}
\usepackage{booktabs}
\usepackage[landscape,a4paper]{geometry}
\usepackage{colortbl}
\usepackage{xcolor}
\usepackage{xfrac}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
\begin{document}
\begin{table*}\centering
\ra{1.3}

\begin{tabular}{c|c|c|c}\toprule Nucleus & \textit{I} & Natural Abundance / % & Larmor Frequency @ 7T \ \midrule \rowcolor{black!20} \textsuperscript{1}H & \sfrac{1}{2} & 99.98 & 298.0 \ \textsuperscript{2}H & 1 & 0.02 & 45.7 \ \rowcolor{black!20} \textsuperscript{12}C & 0 & 98.90 & - \ \textsuperscript{13}C & \sfrac{1}{2} & 1.10 & 74.9 \ \rowcolor{black!20} \textsuperscript{14}N & 1 & 99.60 & 21.5 \ \textsuperscript{15}N & \sfrac{1}{2} & 0.40 & 30.2 \ \rowcolor{black!20} \textsuperscript{16}O & 0 & 99.96 & - \ \textsuperscript{17}O & \sfrac{1}{2} & 0.04 & 40.4 \ \bottomrule \end{tabular} \end{table*} \end{document}

\documentclass{article}
\usepackage{CormorantGaramond}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\tikzset{
    table/.style={
        matrix of nodes,
        row sep=-\pgflinewidth,
        column sep=-\pgflinewidth,
        nodes={
            rectangle,
            draw=black,
            align=center
        },
        minimum height=1.5em,
        text depth=0.5ex,
        text height=2ex,
        nodes in empty cells,
%%
        every even row/.style={
            nodes={fill=gray!20}
        },
        column 1/.style={
            nodes={text width=2em,font=\bfseries}
        },
        row 1/.style={
            nodes={
                fill=black,
                text=white,
                font=\bfseries
            }
        }
    }
}

\begin{tikzpicture}

\matrix (first) [table,text width=6em]
{
& Monday   & Tuesday & Wednesday & Thursday & Friday\\
1   & A & B & C & D & E \\
2   & F & G & H & J & K \\
3   & A & B & C & D & E \\
4   & F & G & H & J & K \\
};


\end{tikzpicture}
\end{document}

Taken from:

https://tinkertailorsoldiersponge.com/blog/2014/07/07/fancy-thesis-tables-in-latex

and

Example of fancy table using TikZ package

respectively. The one closer in output to what I want is the second.

  • 3
    Welcome to TeX.SE. The point of view of booktabs is that the less ornamentation you put on a table, the easier it is to see the data. So in that view, "good LaTeX table" is the opposite of gray rows, black backgrounded header, and border. But you don't really have a question here. What is it about the second example that you don't like? Do you have something that's more representative of your table, not from someone else's example? – Teepeemm Sep 14 '22 at 02:55
  • My apologies for the lack of contextual clarity. I can change the name to, "Good Stylistic LaTeX table" or something if that works better. At any rate, @frabjous's answer seems to work great for me. – Lumenor Sep 14 '22 at 05:18
  • Your title mentions tables "for papers", therefore it may be useful to note that when you intend to publish a paper in conference proceedings or in a journal, then often you have little to no say in how your tables are presented. These publications use templates that prescribe exactly what a table should look like, which is usually rather simple. For some conferences you might get away with changing the style but it is expected that you don't try to do anything fancy. – Marijn Sep 14 '22 at 13:50

1 Answers1

4

Here is how I'd do your first table but made to look more like your second table with tabularray. I've annotated the options specified in the option right after \begin{tblr} so hopefully it's clear what they do.

See the tabularray documentation for more info on each option.

\documentclass{article}
\usepackage{CormorantGaramond}
\usepackage[italic]{mathastext}% Garamond for numbers too
\usepackage{xfrac}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}% library for aligning at decimal place

\begin{document} \begin{table}\centering \begin{tblr}{ colspec={Q[c,m] Q[c,m] % first two columns centered in middle Q[m,c,si={table-format=2.2},wd=7em] % next two centered but aligned at decimals Q[m,c,si={table-format=3.1},wd=7em]}, % also a fixed width, so header wraps row{odd} = {bg=black!20},% make odd rows have gray background row{1} = {bg=black,fg=white,font=\bfseries},% top row black with white foreground and in bold vlines,hlines, % use both horizontal and vertical lines } {{{Nucleus}}} & {{{\textit{I}}}} & {{{Natural \ Abundance / %}}} & {{{Larmor \ Frequency @ 7T}}} \ \textsuperscript{1}H & \sfrac{1}{2} & 99.98 & 298.0 \ \textsuperscript{2}H & 1 & 0.02 & 45.7 \ \textsuperscript{12}C & 0 & 98.90 & {{{---}}} \ \textsuperscript{13}C & \sfrac{1}{2} & 1.10 & 74.9 \ \textsuperscript{14}N & 1 & 99.60 & 21.5 \ \textsuperscript{15}N & \sfrac{1}{2} & 0.40 & 30.2 \ \textsuperscript{16}O & 0 & 99.96 & {{{---}}} \ \textsuperscript{17}O & \sfrac{1}{2} & 0.04 & 40.4 \end{tblr} \end{table} \end{document}

tblr example

There are certainly ways to make it fill the page, but this particular table doesn't have the data to do that, and I'd want to see what the rest would look like before deciding on their arrangement.

Arguably it looks just as good if you remove the internal lines:

\documentclass{article}
\usepackage{CormorantGaramond}
\usepackage[italic]{mathastext}% Garamond for numbers too
\usepackage{xfrac}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}% library for aligning at decimal place

\begin{document} \begin{table}\centering \begin{tblr}{ colspec={|Q[c,m] Q[c,m] % first two columns centered in middle Q[m,c,si={table-format=2.2},wd=7em] % next two centered but aligned at decimals Q[m,c,si={table-format=3.1},wd=7em]|}, % also a fixed width to header wraps row{odd} = {bg=black!20},% make odd rows have gray background row{1} = {bg=black,fg=white,font=\bfseries},% top row black with white spacing and in bold % use both horizontal and vertical lines } {{{Nucleus}}} & {{{\textit{I}}}} & {{{Natural \ Abundance / %}}} & {{{Larmor \ Frequency @ 7T}}} \ \textsuperscript{1}H & \sfrac{1}{2} & 99.98 & 298.0 \ \textsuperscript{2}H & 1 & 0.02 & 45.7 \ \textsuperscript{12}C & 0 & 98.90 & {{{---}}} \ \textsuperscript{13}C & \sfrac{1}{2} & 1.10 & 74.9 \ \textsuperscript{14}N & 1 & 99.60 & 21.5 \ \textsuperscript{15}N & \sfrac{1}{2} & 0.40 & 30.2 \ \textsuperscript{16}O & 0 & 99.96 & {{{---}}} \ \textsuperscript{17}O & \sfrac{1}{2} & 0.04 & 40.4 \ \hline \end{tblr} \end{table} \end{document}

another style

Edit: As mentioned in the comments, new columns are specified in the tblr options with the colspec={...} option. This is analogous to the portion like {|c|c|c|c|} you would see in a typical tabular environment to specify the columns.

The difference is that tabularray allows for more options, so the generic column specification looks like Q[m,c,wd=2in] rather than just c, with a Q followed by a list of options in brackets. You can find the available options on page 20 of the documentation.

(There are also some other options such as S and X type columns, but these are the same as Q columns with certain options; the documentation has the details.)

If you would want the table to take up the entire width of the page, it's usually a good idea to have one or more expandable columns, which are similar to tabularx's X columns. However, tabularray implements this with expansion coefficients which set the relative width of the columns when expanded.

You can also manually specify the width of a column to a fixed size with wd=.

Columns with either wd= or co= allow for lines to wrap, and therefore paragraphs of text. Here is an example with three columns, all of which allow for paragraphs.

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\usepackage{lipsum}% generate dummy paragraphs

\begin{document}

\lipsum[5] \begin{table}[h]\centering \begin{tblr}{colspec={ Q[wd=0.8in,l,m]% first column is exactly 1in wide, left aligned in the vertical middle Q[co=1,l,m]% second column has an expandable width of coefficient 1 Q[co=2,l,m]% third column has an expandable width of coefficient 2 (Wider) },% end of colspec row{odd} = {bg=black!20},% make odd rows have gray background row{1} = {bg=black,fg=white,font=\bfseries},% top row black with white foreground and in bold hlines,vlines } Fixed & Expands & Expands Wider \ \lipsum[1][1] & \lipsum[2][1-2] & \lipsum[3][1-2] \ \end{tblr} \end{table} \end{document}

table with expandable columns

Those with coefficients will expand naturally to fill the width of the page; the larger the coefficient, the larger relative size they will have.

If you wanted each column the same width you would use co=1 in all their column specifications.

frabjous
  • 41,473
  • This is exactly what I was looking for! Thank you! – Lumenor Sep 14 '22 at 05:20
  • Can I ask if you can give another example for a weekly timetable? – Lumenor Sep 14 '22 at 05:25
  • Also, how can I add columns and use strings of text (like paragraphs) in this table? It goes outside of the document if the values are too big. – Lumenor Sep 14 '22 at 08:17
  • "Can I ask if you can give another example for a weekly timetable?" I'm not just doing random free work for you. At least give me the "guts" of the table to work with, and demonstrate that you're at least trying to learn for yourself from the documentation and the example. – frabjous Sep 14 '22 at 13:12
  • "Also, how can I add columns..." The colspec= option is where you list the column types, just like the |c|c|c|c| portion of the code you posted, except column specifications for tabularray can be more complex than just c and take forms like Q[m,c,wd=2in] where there are multiple options specified inside the brackets. See the documentation page 20 for all the options you can put there. – frabjous Sep 14 '22 at 13:18
  • "...and use strings of text (like paragraphs) in this table?" In order for text to wrap, the column options must include either a fixed width like wd=2in (as I do in columns 3 and 4 above), or a expansion coefficient co=1.5. Use of coefficients will set the width of the columns automatically to max out to the width of the page. If you wanted all columns the same width you could use, e.g., Q[l,m,co=1]. If you assign them different coefficients the ones with the larger number will be wide (e.g., Q[l,m,co=2] will be twice as wide as a Q[l,m,co=1] column. – frabjous Sep 14 '22 at 13:22
  • I can add a bit to my answer to elaborate on the column stuff. – frabjous Sep 14 '22 at 13:24
  • "I'm not just doing random free work for you."

    Thank you for calling me out on this. I have attempted the following code to some pretty good results:

    \centering
    \begin{adjustbox}{width=\columnwidth,center=\columnwidth}
    \begin{tblr}{
    width=\paperwidth,
    colspec={X[c,m] X[c,m] % 
    X[m,c] % 
    X[m,c]}, % 
    row{odd} = {bg=black!20},%
    row{1} = {bg=black,fg=white,font=\bfseries
    vlines,hlines,
     cell{2}{1}={r=3}{c},
    }
    {{{Period}}} & {{{Plus}}} & {{{Minus}}} & {{{Interesting}}} \\
     Pre-Colonial Times & \lipsum[1] &  &  \\
     & \lipsum[1] &  & \\```
    
    – Lumenor Sep 14 '22 at 21:32
  • OK, but why the adjustbox? Why set the tblr to \paperwidth only to shrink it back down to columnwidth? Why not skip the adjustbox, set the tblr to \columnwidth, and use a smaller font size if you want to squeeze more in? – frabjous Sep 14 '22 at 22:46
  • It seemed to me to be be the only way to get the text to not extend out of the border. – Lumenor Sep 15 '22 at 08:48
  • With width=\columnwidth and all the columns being of the X variety, it should resize itself to be exactly the right width, unless you have content that can't be wrapped in the space allotted or something. The font will be bigger than what you're getting with the adjustbox though, so you could use rows={font=\small}, before the other row options to make it smaller again. (Or \footnotesize, etc., depending on how small you wanted to go.) It shouldn't jut into the right margin. – frabjous Sep 15 '22 at 15:40
  • If you mean the bottom margin because you have a lot of rows, you could consider using a longtblr environment rather than \tblr, which can span multiple pages, so you don't need to shrink things as much. – frabjous Sep 15 '22 at 15:42
  • How do I make a list (order or unordered) in tabularray? – Lumenor Sep 16 '22 at 10:24
  • I don't think I know what you mean by "list". Generally tabularray is used for tables and arrays, not itemize/enumerate-like environments, if that's what you mean. Or do you mean a table with numbered rows or something? Anyway, if you have another question, make a new post with that new question, but try to explain what you want as fully as possible. – frabjous Sep 16 '22 at 14:19
  • My apologies for responding so late. I managed to resolve the issue on my own if I remember correctly, but thanks for your help @frabjous – Lumenor Jan 18 '23 at 05:41