Defining a table template for a document

formattingmacrostables

I am trying to create a document which automatically updates certain values according to an output from a python code. From this python code, it produces a latex table, which looks pretty basic. However, I have to integrate this table with the document template, so that it matches the rest of the pre-written tables in the document.

The document template for tables are provided by for eg:


\begin{table}[H] \centering
    \caption{Nominal Skirt Annulus}
    \label{tab:nomskirtannulus}
    \begin{tabular}  {p{2cm}p{3cm}}
        \rowcolor{blue}{\textcolor{white}{Skirt Thickness} } & {\textcolor{white}{Nominal Annulus} } \\ 
        
    10 & 11 \\
        
        
        \hline          
    \end{tabular}   
\end{table} 

In this template, the number of columns and rows are variable.

The python generated latex table looks like:

\begin{table}
\centering
\caption{bleh}
\label{tab:bleh}
\begin{tabular}{ppp}
\toprule
             Tolerance & Bottom &  Lowest Can Weld \\
\midrule
       Annulus nominal &        0.098000 &                 0.098000 \\
     MP flange ovality &        0.001000 &                 0.001000 \\
     
\bottomrule
\end{tabular}
\end{table}

This table should be formatted according to the template (with row colour, text colour etc) automatically. Is there a way to do this in Latex?

Thanks.

Best Answer

A safe version which I will strongly recommend is the following:

\documentclass{article}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\NewDocumentEnvironment{ mytbl }{ O{} m +b }{%
  \begin{tblr}{%
      colspec={#2},%
      row{1}={%
        bg={blue},%
        fg={white},%
        font={\bfseries}% 
      },%
    }%
    #3%
  \end{tblr}%
}{}

\begin{document}
\begin{mytbl}{clr}
  \toprule
  abcd&efgh&ijkl\\
  \midrule
  abcd&efgh&ijkl\\
  abcd&efgh&ijkl\\
  \bottomrule
\end{mytbl}
\end{document}

This will require you to rename all the generated tabular environments to mytbl. Maybe there will be some way to ask Python to generate mytbl instead of tabular directly. This will keep all the other tabulars untouched and safe. If you use any package which uses tabular for some purpose, that tabular will also stay safe.

If you are absolutely sure that you (and packages you use) are only going to require tabulars of this specific type, then the following might be useful.

\documentclass{article}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\RenewDocumentEnvironment{tabular}{ O{} m +b }{%
  \begin{tblr}{%
      colspec={#2},%
      row{1}={%
        bg={blue},%
        fg={white},%
        font={\bfseries}%
      },%
    }%
    #3%
  \end{tblr}%
}{}

\begin{document}
\begin{tabular}{clr}
  \toprule
  abcd&efgh&ijkl\\
  \midrule
  abcd&efgh&ijkl\\
  abcd&efgh&ijkl\\
  \bottomrule
\end{tabular}
\end{document}

Both will generate:

1


Edits:

  1. Add \UseTblrLibrary{booktabs} command for supporting booktabs commands with tblr.
  2. Add booktabs-rules to the table-code and change the output-screenshot.
Related Question