下一個示例是我使用 pandas 為報告生成的眾多表格之一,我需要將標題列加粗。由于表格需要其他處理,我之前無法完成,這意味著我確實需要在 LateX 代碼中進行更改。我從 python 代碼中獲取 LateX 中的完整表作為字串。
\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}
有沒有一種簡單的方法可以使列標題變粗?即,我想獲得以下輸出:
\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}
我認為這可以用正則運算式完成,但我無法理解。我需要替換每次出現的{content}whencontent不為空。任何人都可以幫忙嗎?
uj5u.com熱心網友回復:
我建議使用該tabularray軟體包。這允許在表格的開頭做出有關格式化的所有決定,因此您只需將以前tabular的環境與新設定交換,而不必觸摸單元格本身。你甚至可以讓 latex 為你做這件事:
\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}
uj5u.com熱心網友回復:
下面的代碼應該為您提供所需的輸出。基本上搜索標題部分,然后用正則運算式替換非空括號。這部分也可以用一些 for 回圈來完成,但這看起來更干凈。
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'
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/443548.html
下一篇:/app/.heroku/python/bin/python:/app/.heroku/python/bin/python:無法執行二進制檔案
