使用書籤設定表格格式

使用書籤設定表格格式

我在桌子上遇到了一些麻煩。表的程式碼如下所示:

\documentclass{report}
\usepackage{booktabs}
\usepackage{array}
\newcommand*\rotbf[1]{\rotatebox{90}{\textbf{#1}}}
\newcommand{\specialcell}[2][c]{\begin{tabular}[#1]{@{}l@{}}#2\end{tabular}}

\begin{document}
\begin{table}[hbr]
\centering
\label{tbl:heatwheel_res}
\begin{tabular}{llll}
\toprule
\textbf{Parameter} & \specialcell{Exhaust\\air} & \specialcell{Exhaust and \\ outdoor air} & \specialcell{Heat wheel\\(80~\%)} \\
\midrule
Heat recovery [\%] & 89,6 \% & 89,6 \% & 77,4 \% \\
Real heat recovery [\%] & 50,5 \% & 52,1 \% & - \\
Net energy need for VH and SH & 27,7 & 27,0 & 15,8 \\
\specialcell{Delivered energy for\\DHW, VH and SH} & 31,1 & 27,6 & 45,6 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

我使用它是\specialcell為了打破太長的元素行。但是,我希望第一行中的文字為粗體。當我新增\textbf{}inside時\specialcell,該文件將無法編譯。錯誤訊息是:「Missing } inserted」。

你有什麼建議嗎?我可以建立一個\specialcell2自動加粗的新特殊儲存格 ( ) 嗎?

另一個問題:如何強製表格變寬一點,以增加第一列和第二列之間的空間?我認為第 1 列和第 2 列在第 4 行有點太接近了。

答案1

由於整個外部單元格包含\specialcell應為粗體,因此\bfseries可以在外部單元格之前指定\specialcell

... & \bfseries\specialcell{...} & ...

這也可以放入巨集中\specialcellbold,請參考以下範例:

\documentclass{report}
\usepackage{booktabs}
\usepackage{array}
\newcommand*\rotbf[1]{\rotatebox{90}{\textbf{#1}}}
\newcommand{\specialcell}[2][c]{\begin{tabular}[#1]{@{}l@{}}#2\end{tabular}}
\newcommand{\specialcellbold}[2][c]{%
  \bfseries
  \begin{tabular}[#1]{@{}l@{}}#2\end{tabular}%
}

\begin{document}
\begin{table}[hbr]
\centering
\label{tbl:heatwheel_res}
\begin{tabular}{llll}
\toprule
\textbf{Parameter} &
\specialcellbold{Exhaust\\air} &
\specialcellbold{Exhaust and\\ outdoor air} &
\specialcellbold{Heat wheel\\(80~\%)} \\
\midrule
Heat recovery [\%] & 89,6 \% & 89,6 \% & 77,4 \% \\
Real heat recovery [\%] & 50,5 \% & 52,1 \% & - \\
Net energy need for VH and SH & 27,7 & 27,0 & 15,8 \\
\specialcell{Delivered energy for\\DHW, VH and SH} & 31,1 & 27,6 & 45,6 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

結果

該表的格式略有不同:

\documentclass{report}
\usepackage{booktabs}
% \usepackage{array}% It can also be loaded explicitly, implicitly it is
%                     loaded by siunitx
\usepackage{siunitx}
% \sisetup{output-decimal-marker={,}}% OP now wants to have the default dot
\sisetup{detect-weight, mode=text}
\newcommand*\rotbf[1]{\rotatebox{90}{\textbf{#1}}}
\newcommand{\specialcell}[2][b]{\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
\newcommand{\specialcellbold}[2][b]{%
  \bfseries
  \sisetup{text-rm=\bfseries}%
  \begin{tabular}[#1]{@{}c@{}}#2\end{tabular}%
}
\newcommand*{\leftspecialcell}[2][b]{%
  \begin{tabular}[#1]{@{}l@{}}#2\end{tabular}%
}

\begin{document}
\begin{table}[hbr]
\centering
\label{tbl:heatwheel_res}
\begin{tabular}{l*{3}{S[table-format=2.1]}}
\toprule
\textbf{Parameter} &
{\specialcellbold{Exhaust\\air}} &
{\specialcellbold{Exhaust and\\ outdoor air}} &
{\specialcellbold{Heat wheel\\(\SI{80}{\percent})}} \\
\midrule
Heat recovery [\si{\percent}] & 89,6 & 89,6 & 77,4 \\
Real heat recovery [\si{\percent}] & 50,5 & 52,1 & {---} \\
Net energy need for VH and SH & 27,7 & 27,0 & 15,8 \\
\leftspecialcell{Delivered energy for\\
  \quad DHW, VH and SH} & 31,1 & 27,6 & 45,6 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

替代結果

評論:

  • 底部對齊的標題行。
  • 第 2 至 4 列居中。
  • 包的使用siunitx用於在小數點處對齊以及設定百分號。
  • 左側儲存格中第一行之後的行是縮排的。並且對齊方式遵循底線而不是垂直居中。
  • 對於缺少的條目,使用破折號代替連字號。
  • 刪除了多餘的百分號。
  • 將輸出小數標記變更為預設點(請參閱評論羅爾夫)。
  • 有點粗糙的是\siunit{text-rm=\bfseries}。選項在 -columndetect-weight內部不起作用S

更新

我,Svend Tveskæg,發現程式碼不太容易閱讀,我把它清理了一下(我希望沒關係):

\documentclass{report}

\usepackage{booktabs}
\usepackage{siunitx}

\sisetup{
%  output-decimal-marker = {,},
  detect-weight,
  mode = text
}
\newcommand*{\specialcell}[2][b]{%
  \begin{tabular}[#1]{@{}c@{}}#2\end{tabular}%
}
\newcommand*{\specialcellbold}[2][b]{%
  \bfseries
  \sisetup{text-rm = \bfseries}%
  \begin{tabular}[#1]{@{}c@{}}#2\end{tabular}%
}
\newcommand*{\leftspecialcell}[2][b]{%
  \begin{tabular}[#1]{@{}l@{}}#2\end{tabular}%
}

\begin{document}

\begin{table}[htbp]
 \centering
 \label{tbl:heatwheel-res}
  \begin{tabular}{l *{3}{S[table-format = 2.1]}}
   \toprule
    \textbf{Parameter}                                    &
    {\specialcellbold{Exhaust    \\ air}}                 &
    {\specialcellbold{Exhaust and\\ outdoor air}}         &
    {\specialcellbold{Heat wheel \\ (\SI{80}{\percent})}}       \\
   \midrule
    Heat recovery [\si{\percent}]         & 89,6 & 89,6 & 77,4  \\
    Real heat recovery [\si{\percent}]    & 50,5 & 52,1 & {---} \\
    Net energy need for VH and SH         & 27,7 & 27,0 & 15,8  \\
    \leftspecialcell{Delivered energy for                       \\
    \quad DHW, VH and SH}                 & 31,1 & 27,6 & 45,6  \\
   \bottomrule
  \end{tabular}
\end{table}

\end{document}

答案2

只是為了比較。如何用純 TeX 製作此表。但它也適用於 LaTeX(沒有任何載入的套件)。

\def\toprule{\noalign{\medskip\hrule height .8pt\medskip}}
\def\midrule{\noalign{\medskip\hrule\medskip}}
\let\bottomrule=\toprule
\def\specialcell#1{$\vcenter{\halign{\hfil##\hfil\strut\cr#1\cr}}$}
\def\x{\phantom{\%}}\def\h{\hfill\null}

\halign{&\ \hfil#\unskip\ \hfil\cr
\toprule
\bf Parameter\h & \bf\specialcell{Exhaust\cr air} &
                  \bf\specialcell{Exhaust and\cr outdoor air} &
                  \bf\specialcell{Heat wheel\cr(80~\%)} \cr
\midrule
Heat recovery [\%]\h &89,6 \% & 89,6 \% & 77,4 \% \cr
Real heat recovery [\%]\h & 50,5 \% & 52,1 \% & --- \cr
Net energy need for VH and SH & 27,7 \x & 27,0 \x & 15,8 \x \cr
\specialcell{Delivered energy for\cr DHW, VH and SH\hfill}\h& 31,1 \x & 27,6 \x & 45,6 \x\cr
\bottomrule
}

\bye

表格

答案3

不要重新發明輪子:makecell包:它的\thead\makecell命令允許單元格中的換行符和通用格式。它還定義 \rothead\rotcell命令您是否載入rotating包。演示:

\documentclass{report}
\usepackage{booktabs}
\usepackage{array}
\usepackage{rotating}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\renewcommand\cellalign{lc}
\setcellgapes{2pt}
\makegapedcells

\begin{document}

\begin{table}[hbr]
\centering
\label{tbl:heatwheel_res}
\begin{tabular}{lccc}
\toprule
\makecell{\bfseries Parameter} & \thead{Exhaust\\air} & \thead{Exhaust and \\ outdoor air} & \thead{Heat wheel\\(80~\%)} \\
\midrule
\makecell{Heat recovery\\{}[\%]} & 89,6 & 89,6 & 77,4 \\
\makecell{Real heat recovery\\{}[\%]} & 50,5 & 52,1 & -- \\
\makecell{Net energy need for\\VH and SH} & 27,7 & 27,0 & 15,8 \\
\makecell{Delivered energy for\\DHW, VH and SH} & \rotcell{31,1 }& 27,6 & 45,6 \\
\bottomrule
\end{tabular}
\end{table}

\end{document} 

在此輸入影像描述

相關內容