1

I am trying to make a LaTeX table that includes special headers. My code for the table is shown below:

\begin{table}[ht]
\centering
\scalebox{0.70}{
\begin{tabular}{lddcdd}
\toprule
 &\multicolumn{2}{c}{M1} & 
 & \multicolumn{2}{c}{M2} \\
\cmidrule{2-3} \cmidrule{5-6}
 & \multicolumn{1}{c}{1-2} 
 & \multicolumn{1}{c}{7-8} & 
 & \multicolumn{1}{c}{1-2} 
 & \multicolumn{1}{c}{7-8} \\ 
\midrule
A &   \textbf{0.0021} & \textbf{0.0033} & & -2.37 & 0.00 \\ 
B & \textbf{0.0052} & \textbf{0.0042} & & -1.23 & 0.67 \\ 
C & \textbf{0.0082} & \textbf{0.0057} & & -0.21 & 1.08 \\ 
D & 0.0107 & 0.0097 & &  0.74 & 1.41 \\ 
E & \textbf{0.0080} & 0.0088 & &  5.05 & 5.05 \\ 
F & 0.0140 & 0.0148 & & -2.37 & 0.00 \\ 
G & 0.0128 & 0.0176 & & -1.23 & 0.67 \\ 
H & 0.0157 & 0.0164 & & -0.62 & 1.10 \\ 
I & 0.0145 & 0.0155 & & -0.21 & 1.08 \\ 
K & 0.0236 & 0.0225 & &  5.05 & 5.05 \\ 
\bottomrule
\end{tabular}}
\smallskip
\caption{My Caption}
\label{tab:table1}
\end{table}

it creates a table like this:

enter image description here

but I want the bolded numbers to align nicely with un-bolded numbers...How can I make the bolded numbers to align the other numbers in the right way? Thank you,

chico0913
  • 138
  • 1
    please add complete compilable code as a MWE -- the d type of column in the table is not described – js bibra Oct 23 '20 at 23:37
  • you could try this -- https://tex.stackexchange.com/questions/318372/align-numbers-by-decimal-point-using-siunitx-and-bfseries/318388#318388 – js bibra Oct 23 '20 at 23:41

2 Answers2

3

With use of S columns (defined in siunitx package) and robust command for marking cells with number in boldface:

\documentclass{article}
\usepackage[skip=1ex]{caption}  % <---
\usepackage{booktabs}

\usepackage{siunitx} % <--- \usepackage{etoolbox} % <--- \newrobustcmd\B{% \DeclareFontSeriesDefault[rm]{bf}{b}\bfseries} % <=== \begin{document} \begin{table}[ht] \sisetup{detect-weight, mode=text, % <=== table-format=1.4} \centering \begin{tabular}{l SS S[table-format=-1.2]S[table-format=1.2]} \toprule & \multicolumn{2}{c}{M1} & \multicolumn{2}{c}{M2} \ \cmidrule(r){2-3} \cmidrule(l){4-5} & {1-2} & {7-8} & {1-2} & {7-8} \ \midrule A &\B 0.0021 &\B 0.0033 & -2.37 & 0.00 \ B &\B 0.0052 &\B 0.0042 & -1.23 & 0.67 \ C &\B 0.0082 &\B 0.0057 & -0.21 & 1.08 \ D & 0.0107 & 0.0097 & 0.74 & 1.41 \ E &\B 0.0080 & 0.0088 & 5.05 & 5.05 \ F & 0.0140 & 0.0148 & -2.37 & 0.00 \ G & 0.0128 & 0.0176 & -1.23 & 0.67 \ H & 0.0157 & 0.0164 & -0.62 & 1.10 \ I & 0.0145 & 0.0155 & -0.21 & 1.08 \ K & 0.0236 & 0.0225 & 5.05 & 5.05 \ \bottomrule \end{tabular} \caption{My Caption} \label{tab:table1} \end{table} \end{document}

enter image description here

Note:

  • don't use \scalebox, it scale also fonts in table (you lost control over their size)

  • it is sufficient is determine 5 columns and mark group with \cmidrule(r){2-3} and \cmidrule(l){4-5}

Edit:

In the definition

\newrobustcmd\B{\DeclareFontSeriesDefault[rm]{bf}{b}\bfseries}

the {b} denotes "non-extended bold". That is reason why text marked by \B occupies the same width as regular-weight text. This property -- as is pointed out in @Mico comment -- is exactly what's needed for display numerals in a tabular settings. That it can be exploited by siunitx, it must be in the text-mode.

Zarko
  • 296,517
  • thank you for your reply. After editing my code according to your recommendation, I get the string [rm]bfb right before all of my bolded numbers. I am not sure why this is occuring. Could you please tell me how to avoid this? – chico0913 Oct 24 '20 at 13:50
  • @chico0913, this is not possible. I also test your following up question, and it works. So, you check out, what you really have in the your real document. Provided MWE works! Showed result for sure is not produced by showed MWE. See my comment below your question https://tex.stackexchange.com/questions/568146/ – Zarko Oct 24 '20 at 14:03
0

Solution found here -- https://tex.stackexchange.com/a/318416/197451

I have not used the d column instead used S column to specify numbers in the second column

enter image description here

\documentclass[preview,border=1pt]{standalone}
\usepackage{booktabs}
\usepackage{siunitx}

\usepackage{etoolbox} % <-- new \usepackage{xparse} % <-- new \NewExpandableDocumentCommand\mc{O{1}m} % <-- {\multicolumn{#1}{c}{#2}}

\begin{document} \begin{table} \sisetup{ input-open-uncertainty= , % <-- new input-close-uncertainty= , % <-- new table-space-text-pre=(, % <-- new table-space-text-post=), % <-- new table-align-text-pre=false,% <-- new detect-weight, mode=text, % <-- new table-format=1.4} \renewcommand{\bfseries}{\fontseries{b}\selectfont} % <-- new \newrobustcmd{\B}{\bfseries} % <-- changed \caption{My Caption} \centering \begin{tabular}{ l S} % <--- changed \toprule[2pt] \mc[2]{Panel A} \ \midrule &\mc{M1}\ \midrule B & 0.0140\ \addlinespace C &\B 0.0140 \ \bottomrule \end{tabular} \end{table} \end{document}

js bibra
  • 21,280
  • The decimal markers aren't aligned in the table, and the bold-extended font weight is in use. Please replace \renewcommand{\bfseries}{\fontseries{b}\selectfont} \newrobustcmd{\B}{\bfseries} with \renewcommand{\bfdefault}{b} \let\B\bfseries to get a better (typographic) outcome. – Mico Oct 24 '20 at 05:45
  • output is further skewed – js bibra Oct 24 '20 at 07:19
  • Sorry, I failed to mention \robustify\B after \renewcommand{\bfdefault}{b} \let\B\bfseries – Mico Oct 24 '20 at 07:43