LaTeX 表格中数字以小数点对齐的方法

表格 2019-09-03 16:02  浏览 :6546
在使用`LaTeX`排版表格的时候,有时候会要求表格以小数点对齐,对于数量比较少的情况下我们可以采用空格大法来实现小数点对齐,但是这种对齐方法难免会有疏漏,下面来介绍一种方法,使用`dcolumn`宏包 先来看原始的表格是什么样子的 ![](/data/ueditor/php/upload/image/20190903/1567497263420222.jpg) ```tex \\documentclass{article} \\begin{document} \\begin{table} \\begin{tabular}{|*{5}{l}|} \\hline 80.08 & 36.036 & 19.019 & 73.073 & 78.078 \\\\ 2.002 & 50.05 & 8.008 & 63.063 & 53.053 \\\\ 46.046 & 31.031 & 83.083 & 76.076 & 18.018 \\\\ 50.05 & 33.033 & 35.035 & 58.058 & 4.004 \\\\ \\42.042 & 55.055 & 82.082 & 89.089 & 79.079 \\\\ \\hline \\end{tabular} \\end{table} \\end{document} ``` `dcolumn`宏包定义了新的列对齐方式,可以使表格中小数点对齐, ```tex \\documentclass{article} \\usepackage{dcolumn} \\newcolumntype{d}[1]{D{.}{.}{#1}} %表示输入为小数点,显示为小数点,参数是根据几位小数来考虑列宽度 \\begin{document} \\begin{table} \\begin{tabular}{|*{5}{d{3}}|} \\hline 80.08 & 36.036 & 19.019 & 73.073 & 78.078 \\\\ 2.002 & 50.05 & 8.008 & 63.063 & 53.053 \\\\ 46.046 & 31.031 & 83.083 & 76.076 & 18.018 \\\\ 50.05 & 33.033 & 35.035 & 58.058 & 4.004 \\\\ 42.042 & 55.055 & 82.082 & 89.089 & 79.079 \\\\ \\hline \\end{tabular} \\end{table} \\end{document} ``` ![](/data/ueditor/php/upload/image/20190903/1567497283352908.jpg) 这样就可以使表格中的数字以小数点对齐了
发布评论
登录后方可评论!点击登录
全部评论 (2)
OsbertWang
1楼 · 2019-09-03 22:22

感觉这个话题,以前在整理 faq 手册时见到过,方法其实挺多的,少量的时候,我还是爱 @{.}

Chennanzhang
2楼 · 2019-09-03 17:57

空格大法并不合适,距离很难控制。整数和小数分列的采用 r@{.}l 方式可以,但是数据处理要费死劲了,而且代码的数据不直观;dcolumn 不错,\newcolumntype{d}[1]{D{.}{.}{#1}} 中 #1 并不表示保留几位小数,而是根据几位小数来考虑列宽度。其实还有一个siunitx 宏包提供的 S 列格式更方便,可以根据 siunitx 的数据格式来处理数值,并且可控制以小数点对齐或普通居中对齐。