最初の行にパーセント記号を付けて表の数字を揃える

最初の行にパーセント記号を付けて表の数字を揃える

最初の行のパーセント記号を考慮して数字を揃えたいと思います。

MWE:

\documentclass{article}
\usepackage{booktabs}
\begin{document}

\begin{table}
    \centering
        \begin{tabular}{@{}rrrr@{}}\toprule
            Col1 & Col2 & Col3 & Col4\\ \midrule
            10\% & 20\% & 30\% & 40\%\\
            5    &  6   &  7   &  8  \\
          115    &116   &117   &118  \\
            \bottomrule
        \end{tabular}
\end{table}

\end{document}

次のような出力が得られます。

ここに画像の説明を入力してください

ただし、最初の行のパーセント記号を考慮し、それに応じて後続の数字をインデントして、この画像に示すように数字を揃えたいと思います。

ここに画像の説明を入力してください

答え1

読者はすべてのエントリがパーセンテージを指しているかどうか確信が持てません。ヘッダーにその旨を記載してください。

\documentclass{article}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{tabular}{
  @{}
  l
  *{2}{S[table-format=3.0]}
  @{}
}
\toprule
Debt/Assets & {2003} & {2004} \\
            & {(\%)} & {(\%)}   \\
\midrule
\SI{<50}{\percent}           & {--} &  10 \\
\SIrange{50}{59.9}{\percent} &  20  &   5 \\
\SIrange{60}{69.9}{\percent} & {--} &  10 \\
\SIrange{70}{79.9}{\percent} & {--} &  10 \\
\SIrange{80}{89.9}{\percent} &  40  &  10 \\
\SI{\ge90}{\percent}         &  40  &  55 \\
\cmidrule(lr){2-2} \cmidrule(l){3-3}
Total                        & 100  & 100 \\
Mean                         &  85  &  80 \\
Median                       &  85  &  94 \\
\midrule
Number of projects           & {5}  & {20} \\
\bottomrule
\end{tabular}

\end{document}

最後の部分は\midrule、行が異なるデータに関するものであることを明確にするのに役立ちます。

ここに画像の説明を入力してください

写真の悪いスタイルの方がお好みなら…

\documentclass{article}
\usepackage{siunitx,booktabs}

\newcommand{\dashpercent}{%
  \makebox[1.5em][r]{--\makebox[0pt][l]{\,\%}}%
}
\newcommand{\dash}{\makebox[1.5em][r]{--}}%
\newcommand{\ppercent}{\makebox[0pt][l]{\,\%}}

\begin{document}

\begin{tabular}{
  @{}
  l
  *{2}{S[table-format=3.0]@{\hspace{1.2em}}}
  @{}
}
\toprule
Debt/Assets & {2003} & {2004} \\
\midrule
\SI{<50}{\percent}           & {\dashpercent} &  10\ppercent \\
\SIrange{50}{59.9}{\percent} &  20  &   5 \\
\SIrange{60}{69.9}{\percent} & {\dash} &  10 \\
\SIrange{70}{79.9}{\percent} & {\dash} &  10 \\
\SIrange{80}{89.9}{\percent} &  40  &  10 \\
\SI{\ge90}{\percent}         &  40  &  55 \\
\cmidrule(lr){2-2} \cmidrule(lr){3-3}
Total                        & 100\ppercent  & 100\ppercent \\
Mean                         &  85\ppercent  &  80\ppercent \\
Median                       &  85\ppercent  &  94\ppercent \\
\midrule
Number of projects           & {5}  & {20} \\
\bottomrule
\end{tabular}

\end{document}

ここに画像の説明を入力してください

答え2

シンボルを定義することもできます\phantom %:

\newcommand{\pct}{\phantom{\%}}

それから

\begin{tabular}{@{}rrrr@{}}\toprule
    Col1 & Col2 & Col3 & Col4\\ \midrule
    10\% & 20\% & 30\% & 40\%\\
    5\pct    &  6\pct   &  7\pct   &  8\pct  \\
    115\pct   &116\pct   &117\pct   &118\pct  \\
    \bottomrule
\end{tabular}

%符号が列の端を越えることなく、目的の出力を生成します。

ここに画像の説明を入力してください

答え3

\%を として一時的に再定義します\rlap。また、右側のテーブルを の幅だけ拡張します\%。定義の内容がtabular変わらないのが利点です。

\documentclass{article}
\usepackage{booktabs,calc}
\begin{document}

\begin{table}
    \centering
    \let\svpc\%
    \setbox0=\hbox{\%}
    \edef\tmp{\the\wd0}
    \renewcommand\%{\rlap{\svpc}}
        \begin{tabular}{@{}rrrr@{\hspace{\tmp}}}\toprule
            Col1 & Col2 & Col3 & Col4\\ \midrule
            10\% & 20\% & 30\% & 40\%\\
            5    &  6   &  7   &  8  \\
          115    &116   &117   &118  \\
            \bottomrule
        \end{tabular}
\end{table}

\end{document}

ここに画像の説明を入力してください

関連情報