1

I'm trying to do (what I think) a very simple table. Like the one below in txt

   ================================================
     Header1     Header2      Header3     Header4
    ---------   ---------    ---------   ---------
     asd asd      asdas        asad        asdas
    asasdass    aasd asda     sad sad     das dsa
   ================================================

These constraints are:

  1. Table must be centered on page;
  2. Table content must be centered;
  3. Top and bottom rules are wider than center rule
  4. There is a small spacing between each center rule in each column
  5. Rows must be colored using the two color pattern to be easily read.
  6. Be able to reduce the font size of the whole table.
  7. Custom vertical space between rows (slightly wider for some air ;-)

Using different packages as tabularx, tabular, booktabs or plain tables, I'm able to achieve some of the items, but never all of them. For example, I'm able to do coloring using regular table, but this do not work in tabularx. I have so many different table codes here, but none is able to achieve all items. A example below (that I'm not able to resize font, using \small environment here do not work.

\begin{table}[h!]
\def\arraystretch{1.2}
\begin{center}
\rowcolors{2}{black!5}{black!2}
\begin{tabular}{c c c c c}
   \toprule
   \scshape{Header1} && \scshape{Header2} && \scshape{Header3}  \\
   \cmidrule{1-1}\cmidrule{3-3}\cmidrule{5-5}
   Asd  Asd && asda && Something  \\
   AAA  AAA && Else && asdasd asd  \\
\bottomrule
\end{tabular}
\end{center}
\end{table}
2
  • 1
    any color that works with tabular will work with tabularx, and \small would work for your table example. Nov 28, 2023 at 0:42
  • 2
    Off-topic: \scshape is a switch and doesn't take a braces-delimited argument. Use \textsc{...} instead.
    – Mico
    Nov 28, 2023 at 1:54

1 Answer 1

1

The following screenshot and associated code show that it's not difficult to implement all of your objectives, while using a tabularx environment and switching to a \small relative font size.

enter image description here

\documentclass{book}
\usepackage{booktabs}
\usepackage{tabularx,ragged2e}
\newcolumntype{C}{>{\Centering}X} % for "after" table
\usepackage[table]{xcolor}

\begin{document}

\textcolor{red}{before} % the OP's own code
\begin{table}[h!]
\def\arraystretch{1.2}
\begin{center}
\rowcolors{2}{black!5}{black!2}
\begin{tabular}{c c c c c}
   \toprule
   \scshape{Header1} && \scshape{Header2} && \scshape{Header3}  \\
   \cmidrule{1-1}\cmidrule{3-3}\cmidrule{5-5}
   Asd  Asd && asda && Something  \\
   AAA  AAA && Else && asdasd asd  \\
\bottomrule
\end{tabular}
\end{center}
\end{table}

\textcolor{red}{after}
\begin{table}[h]
\small % obj. #6
\renewcommand\arraystretch{1.1} % obj. #7
\setlength\tabcolsep{10pt} % obj. #4 (default: 6pt}
\centering % obj. #1
\rowcolors{2}{black!7}{white} % obj. #5
\begin{tabularx}{0.6\textwidth}{ CCC } % obj. #2
\toprule % obj. #3
\rowcolor{white} % obj. #5
\textsc{Header1} & \textsc{Header2} & \textsc{Header3}  \\
\cmidrule(lr){1-1}\cmidrule(lr){2-2}\cmidrule(lr){3-3} % obj. #4
Asd  Asd & asda & Something   \\
AAA  AAA & Else & asdasd asd  \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .