對齊 dcolumn 中的斜體數字

對齊 dcolumn 中的斜體數字

這個問題與此類似:加粗時表中的小數與 dcolumn 不對齊

我希望帶有 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} 

相關內容