Home > Back-end >  How to make bold each word in a latex table?
How to make bold each word in a latex table?

Time:03-15

The next example is one of many tables I produce with pandas for a report, for which I need to make the header columns bold. Due to other processing needed for the tables, I can't make it before, meaning that I really need to make the changes in the LateX code. I get the full table in LateX as a string from python code.

\begin{table}[H]
\centering
\begin{tabular}{llrrrrr}
\toprule
{} & {} & {\#d} & {\#i} & {\#l} & {\#s} & {\#o} \\
{Dataset} & {Model} & {} & {} & {} & {} & {} \\
\midrule
\multirow[c]{5}{*}{\textit{D (simple)}} & b & 1,026 & 692 & \itshape 31 & 284 & 1,007 \\
 & f & 1,366 & 398 & 49 & 238 & 685 \\
 & f d & 1,372 & 415 & 46 & 215 & 676 \\
 & f d pt & 1,372 & 415 & 46 & 215 & 676 \\
 & f d tg & \bfseries 1,732 & \itshape 8 & 82 & 262 & \itshape 352 \\
\multirow[c]{5}{*}{\textit{D (complex)}} & b & 527 & 266 & 228 & 589 & 1,083 \\
 & f & 759 & 153 & 270 & 470 & 893 \\
 & f d & 749 & 177 & 233 & 456 & 866 \\
 & f d pt & 749 & 177 & 233 & 456 & 866 \\
 & f d tg & \bfseries 969 & \itshape 33 & \itshape 38 & \itshape 380 & \itshape 451 \\
\bottomrule
\end{tabular}
\end{table}

Is there a simple way to make the column headers bold? Ie, I'd like to obtain the following output:

\begin{table}[H]
\centering
\begin{tabular}{llrrrrr}
\toprule
{} & {} & {\textbf{\#d}} & {\textbf{\#i}} & {\textbf{\#l}} & {\textbf{\#s}} & {\textbf{\#o}} \\
{\textbf{Dataset}} & {\textbf{Model}} & {} & {} & {} & {} & {} \\
\midrule
\multirow[c]{5}{*}{\textit{D (simple)}} & b & 1,026 & 692 & \itshape 31 & 284 & 1,007 \\
 & f & 1,366 & 398 & 49 & 238 & 685 \\
 & f d & 1,372 & 415 & 46 & 215 & 676 \\
 & f d pt & 1,372 & 415 & 46 & 215 & 676 \\
 & f d tg & \bfseries 1,732 & \itshape 8 & 82 & 262 & \itshape 352 \\
\multirow[c]{5}{*}{\textit{D (complex)}} & b & 527 & 266 & 228 & 589 & 1,083 \\
 & f & 759 & 153 & 270 & 470 & 893 \\
 & f d & 749 & 177 & 233 & 456 & 866 \\
 & f d pt & 749 & 177 & 233 & 456 & 866 \\
 & f d tg & \bfseries 969 & \itshape 33 & \itshape 38 & \itshape 380 & \itshape 451 \\
\bottomrule
\end{tabular}
\end{table}

I assume that this can be done with regex but I can't understand it. I'd need to replace every occurrence of {content} when content is not empty. Can anyone help?

CodePudding user response:

I would suggest to use the tabularray package. This allows to make all the decisions about formatting at the start of the table, so you'll just have to exchange your previous tabular environment with the new settings and don't have to touch the cells themselves. You can even ask latex to do this for you:

\documentclass{article}

\usepackage{float}
\usepackage{tabularray}[=v2021]
\usepackage{multirow}
\UseTblrLibrary{booktabs}

\renewenvironment{tabular}[1]{
  \begin{tblr}{
    row{1-2}={font=\bfseries},
    colspec={#1}
  }
}{\end{tblr}}


\begin{document}

\begin{table}[H]
\centering
\begin{tabular}{llrrrrr}
\toprule
{} & {} & {\#d} & {\#i} & {\#l} & {\#s} & {\#o} \\
{Dataset} & {Model} & {} & {} & {} & {} & {} \\
\midrule
\multirow[c]{5}{*}{\textit{D (simple)}} & b & 1,026 & 692 & \itshape 31 & 284 & 1,007 \\
 & f & 1,366 & 398 & 49 & 238 & 685 \\
 & f d & 1,372 & 415 & 46 & 215 & 676 \\
 & f d pt & 1,372 & 415 & 46 & 215 & 676 \\
 & f d tg & \bfseries 1,732 & \itshape 8 & 82 & 262 & \itshape 352 \\
\multirow[c]{5}{*}{\textit{D (complex)}} & b & 527 & 266 & 228 & 589 & 1,083 \\
 & f & 759 & 153 & 270 & 470 & 893 \\
 & f d & 749 & 177 & 233 & 456 & 866 \\
 & f d pt & 749 & 177 & 233 & 456 & 866 \\
 & f d tg & \bfseries 969 & \itshape 33 & \itshape 38 & \itshape 380 & \itshape 451 \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

CodePudding user response:

The code below should give you the desired output. Basically searching for the header section, and then replacing the non-empty brackets with regex. This part can also be done with some for loops but this seems cleaner.

1 {    all stuff with 1  chars except } ->\g<1>  1 }
[\{]            ([^}] ?)                        [\}]
import re

new_table = ''
working_on_header = False
for line in old_table.split('\n'):

    if line == '\\toprule':
        working_on_header = True
    elif line == '\\midrule':
        working_on_header = False
    elif working_on_header:
        line = re.sub(r"[\{]([^}] ?)[\}]", r"{\\textbf{\g<1>}}", line)
    new_table  = line   '\n'
  • Related