2

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.

1 Answer 1

0

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.
3
  • If you require more things in the table, feel free to ask, I will try to add them.
    – Niranjan
    Aug 10, 2022 at 17:59
  • Yeah, the template is fixed; so the second code worked. By any chance, is it possible to to remove the midrule from all the tables by including an input at \RenewDocumentEnvironment? Thank you so much! Aug 11, 2022 at 9:02
  • 1
    I am glad that this worked for you :) Regarding the next question, if you want to remove all the \midrules at one go; you can simply issue the following command in the preamble after loading booktabs: \def\midrule{}. Basically you just forget what \midrule means ;-)
    – Niranjan
    Aug 11, 2022 at 9:22

You must log in to answer this question.

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