我正在使用 tabularx 包来自定义我的表格。我尝试定义新列类型“C”和“Y”。由于我使用了 arraybackslash,我的表格的粗体“标题”对齐得相当丑陋。我该如何解决这个问题?请参见下图。文本“Prvek”和“Kov”未垂直对齐。第三列中的文本换行到第二行。另请参阅我的 MNWE。
我也尝试过类似的事情:
\newcolumntype{C}[1]{>{\centering\arraybackslash\vspace{0pt}\setlength{\parskip}{0pt plus 1pt}}m{#1}<{\vspace{0pt}}}
但它不起作用。
\documentclass[twocolumn,czech]{scrbook}
\usepackage{tabularx}
\usepackage{siunitx}
\usepackage[version=4]{mhchem}
\usepackage[table]{xcolor}
\definecolor{CornflowerBlue}{HTML}{ADD8E6} % CornflowerBlue
\usepackage{lipsum}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}
\section{test}
\lipsum[4]
\begin{table}[h!]
\centering
\rowcolors{2}{lightgray}{white}
\begin{tabularx}{\linewidth}{|C{1.1cm}|C{1.7cm}|Y|}
\hline
\rowcolor{CornflowerBlue}
\textbf{\color{black} Prvek} & \textbf{\color{black} Kov} & \textbf{\color{black} Měrná vodivost
(\unit{\siemens\per\meter})} \\ \hline
\ce{Ag} & Stříbro & \num{63.012e6} \\ \hline
\ce{Cu} & Měď & \num{58.108e6} \\ \hline
\ce{Au} & Zlato & \num{45.167e6} \\ \hline
\ce{Al} & Hliník & \num{34.795e6} \\ \hline
\ce{Be} & Beryllium & \num{27.778e6} \\ \hline
\ce{Fe} & Železo & \num{10.406e6} \\ \hline
\ce{Ni} & Nikl & \num{14.430e6} \\ \hline
\ce{Zn} & Zinek & \num{16.949e6} \\ \hline
\ce{Sn} & Cín & \num{8.696e6} \\ \hline
\end{tabularx}
\end{table}
\end{document}
答案1
您的答案可以简化如下:
\documentclass[twocolumn,czech]{scrbook}
\usepackage{lipsum}
\usepackage{tabularx}
\usepackage{siunitx}
\usepackage[version=4]{mhchem}
\usepackage[table]{xcolor}
\definecolor{CornflowerBlue}{HTML}{ADD8E6} % CornflowerBlue
\newcolumntype{C}{>{\centering\arraybackslash\hspace{0pt}}X}
\begin{document}
\section{test}
\lipsum[4]
\begin{table}[h!]
\renewcommand\tabularxcolumn[1]{m{#1}}
\rowcolors{2}{lightgray}{white}
\begin{tabularx}{\linewidth}{|c|c|C|}
\hline
\rowcolor{CornflowerBlue}
\textbf{Prvek}
& \textbf{Kov}
& \textbf{Měrná vodivost
(\unit{\siemens\per\meter})} \\ \hline
\ce{Ag} & Stříbro & \num{63.012e6} \\ \hline
\ce{Cu} & Měď & \num{58.108e6} \\ \hline
\ce{Au} & Zlato & \num{45.167e6} \\ \hline
\ce{Al} & Hliník & \num{34.795e6} \\ \hline
\ce{Be} & Beryllium & \num{27.778e6} \\ \hline
\ce{Fe} & Železo & \num{10.406e6} \\ \hline
\ce{Ni} & Nikl & \num{14.430e6} \\ \hline
\ce{Zn} & Zinek & \num{16.949e6} \\ \hline
\ce{Sn} & Cín & \num{8.696e6} \\ \hline
\end{tabularx}
\end{table}
\end{document}
或者使用tblr
fromtabularray
包代码更加简洁:
\documentclass[twocolumn,czech]{scrbook}
\usepackage{lipsum}
\usepackage[version=4]{mhchem}
\usepackage{xcolor}
\definecolor{CornflowerBlue}{HTML}{ADD8E6} % CornflowerBlue
\usepackage{tabularray}
\UseTblrLibrary{siunitx}
\begin{document}
\section{test}
\lipsum[4]
\begin{table}[h!]
\begin{tblr}{hline{1,2,Z},
colspec = {c c X[c,m, si={table-format=2.3e1}]},
row{1} = {guard, font=\bfseries, bg=CornflowerBlue},
row{odd[3]} = {bg=lightgray}
}
Prvek & Kov & Měrná vodivost
(\unit{\siemens\per\meter}) \\
\ce{Ag} & Stříbro & 63.012e6 \\
\ce{Cu} & Měď & 58.108e6 \\
\ce{Au} & Zlato & 45.167e6 \\
\ce{Al} & Hliník & 34.795e6 \\
\ce{Be} & Beryllium & 27.778e6 \\
\ce{Fe} & Železo & 10.406e6 \\
\ce{Ni} & Nikl & 14.430e6 \\
\ce{Zn} & Zinek & 16.949e6 \\
\ce{Sn} & Cín & 8.696e6 \\
\end{tblr}
\end{table}
\end{document}
答案2
解决方案可以通过命令实现\renewcommand{\arraystretch}{1.5}
我还用 tabular 环境封装了 tabularx。因此代码如下:
\begin{table}[ht!]
\renewcommand{\arraystretch}{1.5}
\begin{tabularx}{\linewidth}{|z{2cm}|X|m{2cm}|}
\hline
\textbf{\color{black} Prvek} & \textbf{\color{black} Kov} & \textbf{\color{black} Měrná vodivost (\unit{\siemens\per\meter})} \\ \hline
\ce{Ag} & Stříbro & \num{63.012e6} \\ \hline
\ce{Cu} & Měď & \num{58.108e6} \\ \hline
\ce{Au} & Zlato & \num{45.167e6} \\ \hline
\ce{Al} & Hliník & \num{34.795e6} \\ \hline
\ce{Be} & Beryllium & \num{27.778e6} \\ \hline
\ce{Fe} & Železo & \num{10.406e6} \\ \hline
\ce{Ni} & Nikl & \num{14.430e6} \\ \hline
\ce{Zn} & Zinek & \num{16.949e6} \\ \hline
\ce{Sn} & Cín & \num{8.696e6} \\ \hline
\end{tabularx}
\end{table}
同样,我尝试\extrarowheight
在“tabularx”环境中为各个行设置值,\setlength
同时使用该命令以及创建“tabularx”环境的命令。但在这种情况下(\setlength{\extrarowheight}{4ex}
)行内的文本没有垂直对齐。我还没有找到一种简单的方法来解决这个问题。但第一个解决方案可靠地工作。
答案3
{NiceTabular}
这是使用构建该表格的一种方法nicematrix
。您需要进行多次编译。
\documentclass[twocolumn,czech]{scrbook}
\usepackage{siunitx}
\usepackage{lipsum}
\usepackage{nicematrix}
\begin{document}
\section{test}
\lipsum[4]
\begin{table}[h!]
\centering
\begin{NiceTabular}
[hvlines,cell-space-limits=3pt,color-inside]
{w{c}{1.1cm}w{c}{1.7cm}X[c]}
\RowStyle[rowcolor=[HTML]{ADD8E6}]{\bfseries}
Prvek & Kov & \Block{}{Měrná vodivost\\ (\unit{\siemens\per\meter})} \\
\rowcolors{lightgray}{white}
Ag & Stříbro & \num{63.012e6} \\
Cu & Měď & \num{58.108e6} \\
Au & Zlato & \num{45.167e6} \\
Al & Hliník & \num{34.795e6} \\
Be & Beryllium & \num{27.778e6} \\
Fe & Železo & \num{10.406e6} \\
Ni & Nikl & \num{14.430e6} \\
Zn & Zinek & \num{16.949e6} \\
Sn & Cín & \num{8.696e6} \\
\end{NiceTabular}
\end{table}
\end{document}
答案4
不需要复杂的代码。这里有三种实现,一种符合您的愿望,两种方式更好:颜色和粗体并没有真正增加清晰度。对于第一个实现,只需嵌套一个简单的tabular
。
当然需要标题。
\documentclass[twocolumn,czech]{scrbook}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{array}
\usepackage{siunitx}
\usepackage[version=4]{mhchem}
\usepackage[table]{xcolor}
\usepackage{booktabs} % for the second realization
\usepackage{lipsum}
\definecolor{CornflowerBlue}{HTML}{ADD8E6} % CornflowerBlue
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}
\section{test}
\lipsum[1][1-2]
\begin{table}[htp]
\centering
\rowcolors{2}{lightgray}{white}
\begin{tabular}{
|w{c}{1.1cm}|
w{c}{1.7cm}|
w{c}{\dimeval{\columnwidth-2.8cm-6\tabcolsep-4\arrayrulewidth}}|
}
\hline
\rowcolor{CornflowerBlue}
\color{black}\bfseries Prvek &
\color{black}\bfseries Kov &
\color{black}\begin{tabular}{@{}c@{}}
\bfseries Měrná vodivost \\ (\unit{\siemens\per\meter})
\end{tabular} \\
\hline
\ce{Ag} & Stříbro & \num{63.012e6} \\ \hline
\ce{Cu} & Měď & \num{58.108e6} \\ \hline
\ce{Au} & Zlato & \num{45.167e6} \\ \hline
\ce{Al} & Hliník & \num{34.795e6} \\ \hline
\ce{Be} & Beryllium & \num{27.778e6} \\ \hline
\ce{Fe} & Železo & \num{10.406e6} \\ \hline
\ce{Ni} & Nikl & \num{14.430e6} \\ \hline
\ce{Zn} & Zinek & \num{16.949e6} \\ \hline
\ce{Sn} & Cín & \num{8.696e6} \\ \hline
\end{tabular}
\end{table}
\lipsum[1-2]
\begin{table}[htp]
\centering
\begin{tabular*}{\columnwidth}{@{\extracolsep{\fill}}ccS[table-format=2.3e1]@{}}
\toprule
Prvek & Kov & {\begin{tabular}[t]{@{}c@{}}
Měrná vodivost \\ (\unit{\siemens\per\meter})
\end{tabular}} \\
\midrule
\ce{Ag} & Stříbro & 63.012e6 \\
\ce{Cu} & Měď & 58.108e6 \\
\ce{Au} & Zlato & 45.167e6 \\
\ce{Al} & Hliník & 34.795e6 \\
\ce{Be} & Beryllium & 27.778e6 \\
\ce{Fe} & Železo & 10.406e6 \\
\ce{Ni} & Nikl & 14.430e6 \\
\ce{Zn} & Zinek & 16.949e6 \\
\ce{Sn} & Cín & 8.696e6 \\
\bottomrule
\end{tabular*}
\end{table}
\lipsum[1-2]
\begin{table}[htp]
\centering
\begin{tabular}{@{}ccS[table-format=2.3e1]@{}}
\toprule
Prvek & Kov & {\begin{tabular}[t]{@{}c@{}}
Měrná vodivost \\ (\unit{\siemens\per\meter})
\end{tabular}} \\
\midrule
\ce{Ag} & Stříbro & 63.012e6 \\
\ce{Cu} & Měď & 58.108e6 \\
\ce{Au} & Zlato & 45.167e6 \\
\ce{Al} & Hliník & 34.795e6 \\
\ce{Be} & Beryllium & 27.778e6 \\
\ce{Fe} & Železo & 10.406e6 \\
\ce{Ni} & Nikl & 14.430e6 \\
\ce{Zn} & Zinek & 16.949e6 \\
\ce{Sn} & Cín & 8.696e6 \\
\bottomrule
\end{tabular}
\end{table}
\lipsum
\end{document}
前两个认识:
第三种可能性(无拉伸):