这个问题与此类似:表格中的小数以粗体显示时与列不对齐
我希望带有 dcolumn 的表格中的某些数字为斜体并与其他数字对齐。我尝试了与粗体数字建议的类似方法:
\newcolumntype{I}[3]{>{\textit\DC@{#1}{#2}{#3}}c<{\DC@end}}
然而,它不起作用。
例子:
\documentclass[12pt]{article}
\usepackage{dcolumn}
\newcolumntype{.}{D{.}{.}{-1}}
\makeatletter
\newcolumntype{B}[3]{>{\boldmath\DC@{#1}{#2}{#3}}c<{\DC@end}}
\makeatother
\begin{document}
\begin{tabular}{..}
\hline
1.5 & 0.19 \\
\multicolumn{1}{B{.}{.}{-1}}{2.75} & 4.2 \\
3.4 & \textit{8.0} \\
\hline
\end{tabular}
\end{document}
答案1
最简单的方法可能是指定两次字体
\documentclass[12pt]{article}
\usepackage{dcolumn}
\newcolumntype{.}{D{.}{.}{-1}}
\makeatletter
\newcolumntype{B}[3]{>{\boldmath\DC@{#1}{#2}{#3}}c<{\DC@end}}
\makeatother
\begin{document}
\begin{tabular}{..}
\hline
1.5 & 0.19 \\
\multicolumn{1}{B{.}{.}{-1}}{2.75} & 4.2 \\
3.4 & \mathit{8}.\mathit{0} \\
\hline
\end{tabular}
\end{document}
答案2
您可能会定义一个新的数学版本来获取斜体数字,但我将首先提出一个基于siunitx
(部分来自https://tex.stackexchange.com/a/334323/4427)
\documentclass[12pt]{article}
\usepackage{siunitx,booktabs,etoolbox}
\begin{document}
\begin{table}
\centering
%% local redefinitions
\renewrobustcmd{\bfseries}{\fontseries{b}\selectfont}
\renewrobustcmd{\boldmath}{}
\begin{tabular}{
S[table-format=1.2,detect-weight,mode=text]
S[table-format=1.2,detect-weight,mode=text]
}
\toprule
1.5 & 0.19 \\
\bfseries 2.75 & 4.2 \\
3.4 & \itshape 8.0 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
或者,使用dcolumn
:
\documentclass[12pt]{article}
\usepackage{dcolumn}
\DeclareMathVersion{italic}
\SetSymbolFont{operators}{italic}{OT1}{\familydefault}{m}{it}
\newcolumntype{.}{D{.}{.}{-1}}
\makeatletter
\newcolumntype{B}[3]{>{\boldmath\DC@{#1}{#2}{#3}}c<{\DC@end}}
\newcolumntype{I}[3]{>{\mathversion{italic}\DC@{#1}{#2}{#3}}c<{\DC@end}}
\makeatother
\begin{document}
\begin{tabular}{..}
\hline
1.5 & 0.19 \\
\multicolumn{1}{B{.}{.}{-1}}{2.75} & 4.2 \\
3.4 & \multicolumn{1}{I{.}{.}{-1}}{8.0} \\
\hline
\end{tabular}
\end{document}
答案3
与 David 的想法相同,但为了方便起见,将其包装到宏中:
\documentclass[12pt]{article}
\usepackage{dcolumn}
\newcolumntype{.}{D{.}{.}{-1}}
\makeatletter
\newcolumntype{B}[3]{>{\boldmath\DC@{#1}{#2}{#3}}c<{\DC@end}}
\makeatother
% call \mathit separately for integer and decimal parts
\newcommand{\itnum}[2]{\mathit{#1}.{\mathit{#2}}}
\begin{document}
\begin{tabular}{..}
\hline
1.5 & 0.19 \\
\multicolumn{1}{B{.}{.}{-1}}{2.75} & 4.2 \\
3.4 & \itnum{8}{0} \\
\hline
\end{tabular}