注:本文为 “LaTeX | 度量单位 / 间距调整” 相关讨论合辑。
英文引文,机翻未校。
中文引文,略作重排。
未整理去重。
如有内容异常,请看原文。
Which measurement units should one use in LaTeX?
LaTeX 中应使用哪些度量单位?
There are various measurement units that one can use (such as pt, mm, in, em, ex etc.) for specifying lengths and heights. For font-based units (em and ex) the actual spacing will vary slightly depending on the font. For the other types of units the spacing is fixed at the given measurement.
LaTeX 中可使用多种度量单位(如 pt、mm、in、em、ex 等)指定长度和高度。对于基于字体的单位(em 和 ex),实际间距会因字体不同而略有差异;其他类型的单位则具有固定的度量值。
Is it better to prefer some of these units over others in certain situations? For example, is the em unit better than a fixed measurement when specifying lengths for indenting text eg. when setting xleftmargin, and xrightmargin in the listings package. When would you use the ex unit for specifying heights? I imagine fixed units should always be used when setting page margins.
在特定场景下,是否应优先选择某些单位而非其他单位?例如,在指定文本缩进长度时(如在 listings 宏包中设置 xleftmargin 和 xrightmargin),em 单位是否优于固定度量单位?何时应使用 ex 单位指定高度?我认为设置页边距时应始终使用固定单位。
So, I guess what I am asking is, what are the guidelines for deciding which measurement units to use?
因此,我的核心问题是:选择度量单位时应遵循哪些准则?
edited Jun 28, 2011 at 12:43
N.N.
asked Oct 17, 2010 at 22:26
Steve
Answers
Answer 1 (Will Robertson)
There are no hard-and-fast rules, but here’s a short list of guidelines:
不存在绝对严格的规则,但以下是一组简要准则:
-
1emcan be considered a horizontal length and1exa vertical one, so use them accordingly but mix and match as you need (they are horizontal and vertical arbitrarily, but usually you hear people talk about1emis the width of an “m” — usually false — and1exis the height of an “x” — usually true). I usually consider1emto be about the same size as the font size in points.
1em可视为水平长度单位,1ex可视为垂直长度单位,因此应据此使用,但也可根据需要灵活组合(二者的水平/垂直属性并非绝对,但通常认为1em等于字母 “m” 的宽度——这一说法通常不成立——而1ex等于字母 “x” 的高度——这一说法通常成立)。我通常认为1em的大小与以点(pt)为单位的字体大小大致相当。 -
emandexare relative lengths, so they’re better for designing around text; like you say, an indent of2emwill work whether the fontsize is9ptor12pt.
em和ex是相对长度单位,因此更适合围绕文本进行设计;如你所述,无论字体大小是9pt还是12pt,2em的缩进都能适用。 -
Things that are of fixed size (such as the page size) should be defined with fixed units, of course.
当然,对于固定大小的对象(如页面尺寸),应使用固定单位进行定义。 -
When things should be relative, it will often make more sense to define them in terms of the page design. For example,
width=0.5\linewidthmight make more sense thanwidth=5cmfor a figure.
当需要相对长度时,根据页面设计进行定义通常更为合理。例如,对于图形,width=0.5\linewidth可能比width=5cm更合适。 -
Watch out for the
ptunit! In TeX,1ptis1/72.27in, whereas the more common “PostScript point” used by most other software is1/72inwhich in TeX is1bp. If you’re dealing with other programs and need your lengths exact, usebpor use standardcmorinmeasurements.
注意pt单位!在 TeX 中,1pt等于1/72.27in,而大多数其他软件使用的更常见“PostScript 点”为1/72in,在 TeX 中对应1bp。若需与其他程序交互并要求长度精确,应使用bp单位,或采用标准的cm或in单位。 -
Remember that TeX uses fixed point arithmetic, so there are precision problems when you hit around five significant figures. E.g.,
需注意 TeX 采用定点算术运算,因此当数值达到约五位有效数字时可能出现精度问题。例如:\newlength\x \x=1in \showthe\xgives
输出结果为:> 72.26999pt.
edited Dec 22, 2021 at 15:13
answered Oct 17, 2010 at 23:47
Will Robertson
Comments on Answer 1
-
@Will: Bringhurst says: (2.1.1) “1 em is a distance equal to the type size”, so you can be more certain than “usually consider”. BTW, I’m thinking of changing my id to “BringhurstSycophant”.
@Will:Bringhurst 在其著作中指出:(2.1.1)“1 em 等于字体大小对应的距离”,因此你可以比“通常认为”更确定这一点。顺便说一句,我正考虑将我的用户名改为“BringhurstSycophant”(Bringhurst 的崇拜者)。
—— Brent.Longborough
Commented Oct 18, 2010 at 8:29 -
Is there a difference between
\linewidthand\textwidth?
\linewidth和\textwidth之间有区别吗?
—— maxschlepzig
Commented Oct 18, 2010 at 9:28 -
@maxschlepzig: Yes.
\textwidthis the width of the whole text area on the page.\linewidthis the width of the current line, so in a column it is equal to\columnwidth. See this answer: tex.stackexchange.com/questions/275/…
@maxschlepzig:有区别。\textwidth是页面上整个文本区域的宽度;\linewidth是当前行的宽度,因此在分栏布局中,其值等于\columnwidth。
—— Brent.Longborough
Commented Oct 18, 2010 at 10:30 -
Relative units are usually used, because of their invariance to some changes. But in print, when the possible page sizes form a predefined set, and the possible figure sizes form another a predefined set, than their ratios form a bigger set and we loose that invariance. Another point - sometimes figures are in margins, sometimes hang in margins and sometimes are entirely in the text. And in each case different relative lengths are suitable, not the page width. Again invariance is lost. That’s why absolute sizes are used - they are a few and are more invariant.
相对单位通常被使用,因为它们对某些变化具有不变性。但在印刷场景中,当页面尺寸和图形尺寸均为预定义集合时,其比例会形成更大的集合,导致不变性丧失。此外,图形可能位于页边距内、悬挂于页边距或完全处于文本区域,每种情况都适合使用不同的相对长度(而非页面宽度),这也会导致不变性丧失。因此,固定单位被广泛使用——它们数量有限且具有更强的不变性。
—— Karl Karlsson
Commented Jun 22, 2011 at 10:18 -
About the scale. Engineering drafts and geographic maps have mandatory integer scale. And these draftsmen were the people that did all the graphics in the books of the past (which in most cases are incomparably more simple and beautiful than what PSTricks or PGF/TikZ can do). Many people wonder how beautiful these books are. But now days, having computers, instead of making things more precise, they are made very arbitrary. And here comes another thing for the page layout - doing arbitrary margins and arbitrary figure sizes isn’t beautiful too.
关于比例:工程图纸和地理地图要求必须使用整数比例。过去书籍中的所有图形均由这些绘图员完成(在大多数情况下,其简洁性和美观度是 PSTricks 或 PGF/TikZ 无法比拟的)。许多人惊叹于这些书籍的美感,但如今有了计算机,图形设计非但没有更精确,反而变得非常随意。页面布局也是如此——随意设置页边距和图形尺寸并不会带来美感。
—— Karl Karlsson
Commented Jun 22, 2011 at 10:29
Answer 2 (Johan_E)
Reading these answers, and comments to them, piqued my curiosity… Especially this sentence that Will Robertson wrote in his answer:
阅读上述回答及相关评论后,我产生了浓厚的兴趣……尤其是 Will Robertson 在其回答中提到(见下文):
[…] I usually consider
1emto be about the same size as the font size in points.
[…] 我通常认为1em的大小与以点(pt)为单位的字体大小大致相当。
It inspired me to investigate the actual behavior of TeX (or rather LaTeX). Since my findings could be of interest to those finding this question, I post them here.
这启发我对 TeX(更准确地说是 LaTeX)的实际行为进行研究。由于我的发现可能对其他关注该问题的人有帮助,特此分享如下。
Computer Modern
Computer Modern 字体
I started of by checking what length 1em and 1ex are for different selection of Roman/Serif/Typewriter, Medium/Bold, and Upright/Italic/Slanted/SmallCaps. Here are the results:
我首先测试了不同字体样式(罗马体/衬线体/打字机字体、常规/粗体、直立/斜体/倾斜/小型大写)下 1em 和 1ex 的实际长度,结果如下:

As we can see, 1em varies quiet a lot; from 10.00pt to 11.82pt. The length of 1ex is more consistent, with only three different values for all the different styles.
如图所示,1em 的变化范围较大(从 10.00pt 到 11.82pt),而 1ex 的长度更为稳定,所有字体样式中仅出现三种不同值。
In the table some actual measurements of the font is included. These are the width of an “M” (measured as \wd of an \hbox{M} created when the font is active) and the height and depth of an “x” (measured similarly as \ht and \dp of an \hbox{x}).
表格中还包含了字体的实际测量数据:字母“M”的宽度(通过 \hbox{M} 的 \wd 长度测量,测量时字体处于激活状态)以及字母“x”的高度和深度(通过 \hbox{x} 的 \ht 和 \dp 长度测量)。
An interesting point of this table is that 1em is neither 10pt (the selected font size) or the M-width. On the other hand, 1ex corresponds exactly to the height of an “x”, except for typewriter small caps.
值得注意的是,1em 既不等于所选字体大小(10pt),也不等于字母“M”的宽度;而 1ex 除打字机字体的小型大写样式外,均与字母“x”的高度完全一致。
Latin Modern
Latin Modern 字体
[Xavier asked about the results for Latin Modern using pdfLaTeX, so I added this section.]
[Xavier 询问了 pdfLaTeX 下 Latin Modern 字体的测试结果,因此补充本节内容。]
To check the values for Latin Modern using pdfLaTeX I added this to the preamble:
为测试 pdfLaTeX 下 Latin Modern 字体的相关数值,我在导言区添加了以下代码:
\usepackage{lmodern}
\usepackage[T1]{fontenc}
Some more styles are available, compared to Computer Modern, so I added them to the list. Also, the bold italic typewriter style was not available for Latin Modern, so I removed it.
与 Computer Modern 相比,Latin Modern 提供了更多字体样式,因此我将其添加到测试列表中;同时,Latin Modern 不支持粗体斜体打字机字体,故将其从列表中移除。

The values of this table corresponds to the values for Computer Modern to within a hundredth of a point for the columns 1em, M-height, 1ex and x-depth (all zero again).
该表格中 1em、M 高度、1ex 和 x 深度列的数值与 Computer Modern 字体的对应数值相差不超过 0.01pt(x 深度均为 0)。
This time it’s a bit different for the x-height values. For the most part they correspond to the value of 1ex. However they do not correspond for these styles: medium small caps roman, all medium sans, and all typewriter styles apart from medium italic.
但 x 高度列的数值有所不同:大多数样式的 x 高度与 1ex 一致,但罗马体常规小型大写、所有无衬线常规样式以及除斜体常规外的所有打字机样式除外。
XeLaTeX and Latin Modern
XeLaTeX 与 Latin Modern 字体
Generating the above tables with XeLaTeX instead of pdfLaTeX, without changing the code, results in exactly the same values.
在不修改代码的情况下,使用 XeLaTeX 而非 pdfLaTeX 生成上述表格,得到的结果完全一致。
I continued the investigation with XeLaTeX and the Latin Modern fonts. I used fontspec to load the fonts “manually” with \fontspec{fontname} where fontname is given in the first column of the table. [See note in Conclusions regarding fontspec.] This uses the system wide (non TeX specific) catalogue, via the fontconfig library, to locate and load the fonts. In this particular case it loads Type1 fonts (.pfb-files) that just happen to live in my main texmf tree.
我进一步使用 XeLaTeX 测试了 Latin Modern 字体,通过 fontspec 宏包的 \fontspec{fontname} 命令手动加载字体(其中 fontname 为表格第一列中的字体名称)。[关于 fontspec 的说明见“结论”部分。] 该方法通过 fontconfig 库调用系统级(非 TeX 专用)字体目录来定位和加载字体,本测试中加载的是位于主 texmf 目录下的 Type1 字体(.pfb 文件)。

Once again, the values for 1ex matches the measured x-height exactly (even when x-depth is non-zero). This time, however, 1em is exactly the specified font size (10pt). One more thing to note is that the measured values correspond to the values for Computer Modern (rounded to hundredths of points) except for when the x-depth is non-zero.
再次验证了 1ex 与测量的 x 高度完全一致(即使 x 深度非零);而本次测试中 1em 恰好等于指定的字体大小(10pt)。此外,除 x 深度非零的情况外,其他测量值与 Computer Modern 字体的对应数值(四舍五入到小数点后两位)一致。
More fonts
更多字体
For good measures, I also compiled a table for some other fonts. These are all TrueType and OpenType fonts.
为确保测试的全面性,我还整理了其他一些字体的相关数据,这些字体均为 TrueType 或 OpenType 格式。

Conclusions
结论
[Edit: after adding the section on Latin Modern in pdfLaTeX, I have reconsidered some of the conclusions.]
[编辑:添加“pdfLaTeX 下的 Latin Modern 字体”部分后,我重新审视了部分结论。]
The font metric mechanism seems to differ between using TFM fonts and Xe(La)TeX’s new font support.
TFM 字体与 Xe(La)TeX 的新型字体支持在字体度量机制上存在差异。
(non-Xe)LaTeX
(非 Xe)LaTeX
(this also applies to XeLaTeX using TFM fonts.)
(同样适用于使用 TFM 字体的 XeLaTeX)
- The value of
1emis not equal to the selected size of the font, nor is it the width of an actual “M”.
1em的值既不等于所选字体大小,也不等于字母“M”的实际宽度。 - The value of
1exis not tied to the size of an actual “x”.
1ex的值与字母“x”的实际大小无必然关联。
However, from the presented data I can conclude that 1ex is most often the height of an actual “x” (\ht of \hbox{x}); while the value of 1em is (almost?) never the width of an “M” (\wd of \hbox{M}).
但根据测试数据可得出:1ex 通常等于字母“x”的实际高度(\hbox{x} 的 \ht 长度),而 1em 几乎从不等于字母“M”的实际宽度(\hbox{M} 的 \wd 长度)。
XeLaTeX/fontspec
XeLaTeX/fontspec
Note: I realize that this probably has nothing to do with
fontspec, per se. It is more the question of “old” vs. “new” font handling mechanisms in the engine. However I use the phrase XeLaTeX/fontspec to differentiate the new font handling from the TFM font handling still present in the XeTeX engine.
注:我意识到这可能与fontspec本身无关,更多是引擎中“旧”与“新”字体处理机制的差异。但为区分 XeTeX 引擎中仍存在的 TFM 字体处理机制与新型字体处理机制,我使用“XeLaTeX/fontspec”这一表述。
From the collected data I conclude that, for non-TFM fonts:
根据收集的数据,对于非 TFM 字体,可得出以下结论:
- The value of
1emis exactly the selected font size.
1em的值恰好等于所选字体大小。 - The value of
1exis exactly the height of an “x”. (\htof\hbox{x})
1ex的值恰好等于字母“x”的高度(\hbox{x}的\ht长度)。
Different Sizes
不同字体大小
Regarding different font sizes, I have done some runs for 12pt text, and found nothing surprising. Just remembering that the Computer Modern fonts have optical sizes so 1em and 1ex will (probably) depend on the font size in a non-simple way.
关于不同字体大小的测试:我对 12pt 文本进行了测试,未发现异常结果。需注意的是,Computer Modern 字体具有光学尺寸特性,因此 1em 和 1ex 与字体大小的关系可能并非简单线性。
edited Apr 13, 2017 at 12:35
Johan Johan_E
Comments on Answer 2
-
Very interesting for Computer Modern. I would love to know how LaTeX defines
1em; typographically, it is (nowadays) defined as equal to the font size, so clearly there is something weird happening with Computer Modern (or with your calculations). Maybe you just discovered a last TeX bug :
Computer Modern 字体的测试结果非常有趣。我很想知道 LaTeX 是如何定义1em的;从排版学角度来看,1em如今被定义为等于字体大小,因此 Computer Modern 字体(或你的计算)中显然存在一些异常。或许你发现了 TeX 的最后一个漏洞:
—— Xavier
Commented Feb 13, 2013 at 4:24 -
Just out of curiosity, what are the results for Latin Modern with pdflatex?
出于好奇,pdfLaTeX 下 Latin Modern 字体的测试结果如何?
—— Xavier
Commented Feb 13, 2013 at 4:25 -
@Xavier: The Latin Modern fonts (with pdfLaTeX) showed some surprising results…
1exnot equal to\htof\hbox{x}! Check the updated answer.
@Xavier:Latin Modern 字体(pdfLaTeX 下)的测试结果令人意外……1ex与\hbox{x}的\ht长度不相等!
—— Johan_E
Commented Feb 13, 2013 at 6:33 -
Remark: in typewriter fonts, it makes sense that all values of (
1em,1ex) are equal, no matter which series or style you use. In other fonts, similar approach makes sense, too. That’s why they do not correspond.
注:在打字机字体中,无论使用何种字重或样式,1em和1ex的值保持一致是合理的;其他字体中类似的设计思路也同样适用,这也是它们与字母实际尺寸不对应的原因。
—— yo’
Commented Feb 13, 2013 at 7:41
Answer 3 (Lev Bishop)
To fill in a couple of gaps that Will didn’t address: English-speaking typographers will specify the measure (\textwidth) in pc and the leading (\baselineskip) in pt; continental European typographers will use cc and dd for the same purposes (not that you’ll see much difference).
补充 Will 未提及的两点:英语国家的排版师通常使用 pc 单位指定文本宽度(\textwidth),使用 pt 单位指定行距(\baselineskip);欧洲大陆的排版师则使用 cc 和 dd 单位实现相同目的(二者差异不大)。
There’s no particular reason for preferring these units other than tradition.
选择这些单位并无特殊原因,仅为传统习惯。
answered Oct 18, 2010 at 1:13
Lev Bishop
Comments on Answer 3
-
Now you’re giving me TeXBook flashbacks.
这让我想起了《TeX 手册》中的内容。
—— Matthew Leingang
Commented Oct 18, 2010 at 4:17 -
Interesting, I didn’t know about
cc(cicero) anddd(didôt point) units.
很有趣,我之前不知道cc(西塞罗单位)和dd(迪多点单位)。
—— Steve
Commented Oct 19, 2010 at 12:13
What are the various units (ex, em, in, pt, bp, dd, pc) expressed in mm?
各类单位(ex、em、in、pt、bp、dd、pc)如何以 mm 表示?
How can one determine the length of the different units (measured 1ex, 1em, 1in, 1pt, 1bp, 1dd, 1pc) in mm?
如何确定不同单位(1ex、1em、1in、1pt、1bp、1dd、1pc)对应的 mm 长度?
edited May 9, 2013 at 23:02
Qrrbrbirlbel
asked Jan 4, 2011 at 16:30
Regis Santos
12 Answers
Answer 1 (Philippe Goutet)
Here is a variant on Herbert’s answer, using \dimexpr instead (inspired from the thread \ifnum for real numbers of comp.text.tex), which allows to do conversions in a purely expandable way. The syntax is \convertto{mm}{1pt} to convert 1pt in mm:
以下是 Herbert 回答的一个变体,使用 \dimexpr 实现(灵感来自 comp.text.tex 论坛的帖子《\ifnum for real numbers》),支持纯可扩展的单位转换。语法格式为 \convertto{mm}{1pt},表示将 1pt 转换为 mm 单位:
\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother
The results are not quite the same as with the printlen package, probably due to the fact that \dimexpr performs arithmetic slightly differently from TeX. Here’s a table showing all the converted lengths (I omitted sp to avoid arithmetic overflows):
转换结果与 printlen 宏包(http://ctan.org/pkg/printlen)略有差异,可能是因为 \dimexpr 的算术运算方式与 TeX 原生运算存在细微差别。以下表格列出了所有单位的转换结果(为避免算术溢出,省略了 sp 单位):

\documentclass[a4paper]{article}
\usepackage{array}
\usepackage[hmargin=2cm]{geometry}
\makeatletter
% http://groups.google.com/group/comp.text.tex/msg/7e812e5d6e67fcc5
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother
\begin{document}
\begin{center}\begin{tabular}
{>{\def\colunit{pt}}l<{\convertto{\rowunit}{1\colunit}}
>{\def\colunit{mm}}l<{\convertto{\rowunit}{1\colunit}}
>{\def\colunit{cm}}l<{\convertto{\rowunit}{1\colunit}}
>{\def\colunit{ex}}l<{\convertto{\rowunit}{1\colunit}}
>{\def\colunit{em}}l<{\convertto{\rowunit}{1\colunit}}
>{\def\colunit{bp}}l<{\convertto{\rowunit}{1\colunit}}
>{\def\colunit{dd}}l<{\convertto{\rowunit}{1\colunit}}
>{\def\colunit{pc}}l<{\convertto{\rowunit}{1\colunit}}
>{\def\colunit{in}}l<{\convertto{\rowunit}{1\colunit}}
>{\bfseries}l}
\multicolumn{1}{l}{\bfseries 1pt} & \multicolumn{1}{l}{\bfseries 1mm} & \multicolumn{1}{l}{\bfseries 1cm} & \multicolumn{1}{l}{\bfseries 1ex} & \multicolumn{1}{l}{\bfseries 1em} & \multicolumn{1}{l}{\bfseries 1bp} & \multicolumn{1}{l}{\bfseries 1dd} & \multicolumn{1}{l}{\bfseries 1pc} & \multicolumn{1}{l}{\bfseries 1in} & \\
\gdef\rowunit{pt} & & & & & & & & & \rowunit\\
\gdef\rowunit{mm} & & & & & & & & & \rowunit\\
\gdef\rowunit{cm} & & & & & & & & & \rowunit\\
\gdef\rowunit{ex} & & & & & & & & & \rowunit\\
\gdef\rowunit{em} & & & & & & & & & \rowunit\\
\gdef\rowunit{bp} & & & & & & & & & \rowunit\\
\gdef\rowunit{dd} & & & & & & & & & \rowunit\\
\gdef\rowunit{pc} & & & & & & & & & \rowunit\\
\gdef\rowunit{in} & & & & & & & & & \rowunit\\
\end{tabular}\end{center}
\end{document}
edited Mar 26, 2017 at 15:22
barbara beeton
Comments on Answer 1
-
Any particular reason why you do not do:
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1\ #1}or\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1\relax\ #1}to include the unit in the expanded output?
是否有特定原因不将单位包含在扩展输出中?例如:\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1\ #1}或\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1\relax\ #1}。
—— Jonathan Komar
Commented Oct 20, 2016 at 10:58 -
Of course, the results for
exandemdepend on the current font …
当然,ex和em的转换结果取决于当前使用的字体……
—— Hagen von Eitzen
Commented Nov 18, 2019 at 22:16 -
It’s interesting to note that TeX adopts a quasi-decimal metric system.
值得注意的是,TeX 采用了准十进制度量系统。
—— gboffi
Commented Jul 3, 2020 at 15:40 -
@gboffi It’s a consequence of the internal representation of dimensions in TeX. Everything is, at its lowest level an integer (portable floating point arithmetic wasn’t standardized until 1985, well after TeX82’s release, so Knuth had to implement his algorithms ex nihilo) and dimensions are represented in integer quantities of
sp = 1/65536pt. This, plus the conversions to decimal end up with mild rounding errors that can show up in anything other than the conversions betweensp,ptandpc(and possiblyex/emdepending on their values)
@gboffi 这是 TeX 内部维度表示方式的必然结果。在最低层级,所有数据均以整数形式存储(可移植浮点算术直到 1985 年才标准化,远晚于 TeX82 的发布,因此 Knuth 不得不从零开始实现相关算法),而维度则以sp(1sp = 1/65536pt)为单位的整数表示。这一设计,加上十进制转换过程,会导致轻微的舍入误差,除sp、pt和pc之间的转换(以及可能的ex/em转换,取决于其具体值)外,其他单位转换中均可能出现该误差。
—— Don Hosek
Commented Jul 30, 2021 at 13:34
Answer 2 (user4686)
incipit (2017)
引言(2017 年补充)
The precise rules how TeX inputs a dimension expressed in a certain unit are commented in these two posts:
TeX 处理特定单位维度输入的精确规则详见以下两篇帖子:
The underlying thing is that internally all dimensions are integer multiples of sp unit, hence there must be some conversion done. A few salient points:
基本原理是:TeX 内部所有维度均以 sp 单位的整数倍表示,因此必须进行单位转换。以下是几个关键点:
-
the set of those internal dimensions obtainable from a specification in
cmhas non empty symmetric difference with the set of those dimensions obtainable from a dimension withinas unit. This is important if one intents to use\ifdimtests. This is a core TeX aspect and will not be changed by any higher level interface for testing dimension equality.
以cm为单位指定的维度所对应的内部维度集合,与以in为单位指定的维度所对应的内部维度集合的对称差集非空。若需使用\ifdim进行维度比较,这一点至关重要。这是 TeX 的核心特性,任何高层级的维度相等性测试接口均无法改变这一特性。 -
when expressing a dimension in a given unit
<unit>, the granularity is1/65536<unit>and using more than five digits after the decimal mark can bring only a one-shot change. But it is hard to guess the borderline: nothing can change beyond 17 decimal digits, but the result obtained from 17 decimal digits is not necessarily the same as the one obtained from rounding these 17 decimal digits to only 5 decimal digits. As this might surprise people I am providing here some examples you can try out:
当以单位<unit>表示维度时,精度为1/65536<unit>,小数点后超过五位的数字仅能带来一次性变化。但临界值难以预测:小数点后超过 17 位的数字不会产生任何变化,但 17 位数字对应的结果与将其舍入到五位小数后的结果不一定相同。为避免误解,以下提供几个可验证的示例: -
0.22222pt(from2/9) gives14563spbut0.22222222222222222ptgives14564sp. One must use0.22223ptto get also14654sp.
0.22222pt(由2/9得到)对应14563sp,而0.22222222222222222pt对应14564sp。必须使用0.22223pt才能得到14654sp。 -
1.53333pt(from23/15) gives100488spbut1.53333333333333333ptgives100489sp. One must use1.53334ptto get also100489sp.
1.53333pt(由23/15得到)对应100488sp,而1.53333333333333333pt对应100489sp。必须使用1.53334pt才能得到100489sp。
Yes, using 0.66667pt is the correct choice, but this is a bit an accidental fact as the two examples above show: sometimes one should round in the opposite direction to get what the full specification with 17digits would have given. (unfortunately it appears I have lost a file where I had tested these things but I remember that my testing revealed that counter-intuitive rounding was needed in roughly 10% of the tested cases, built-up using small fractions like the above ones).
诚然,0.66667pt 是正确的选择,但如上述两个示例所示,这在一定程度上是偶然的:有时需要反向舍入才能得到 17 位数字完整表示对应的结果。(遗憾的是,我丢失了包含这些测试的文件,但记得测试结果显示,在约 10% 的案例中(基于上述类似的小分数构建),需要采用反直觉的舍入方式。)
The bigger the dimension unit, the less precise the inner representation. Hence using in is a less precise input method than using cm which is less precise than using pt. For example using em typically means you ave a 10sp granularity, and there is no way to go below that, even with using 17 decimal digits in the expression of the dimension using with em unit. Using pt ensures the minimal 1sp granularity.
单位越大,内部表示的精度越低。因此,使用 in 单位的输入精度低于 cm,而 cm 又低于 pt。例如,使用 em 单位通常意味着精度为 10sp,即使在维度表达式中使用 17 位小数,也无法突破这一精度限制;而使用 pt 单位可确保最小 1sp 的精度。
Regarding the Didot point, there is a more-detailed examination in a section of the xint manual.
关于迪多点(dd)的更多细节,可参考 xint 宏包手册(http://www.ctan.org/pkg/xint)的相关章节。
(end of 2017 added contents)
(2017 年补充内容结束)
Here is another type of table. It gives the exact irreducible conversion factors between units (em and ex are the special font dependent cases; for them I use \dimexpr but this seems not to be a too good idea, it would perhaps be better to get them from the the suitable \fontdimen parameters).
以下是另一种类型的表格,给出了单位之间精确的既约转换因子(em 和 ex 为依赖字体的特殊情况;此处使用 \dimexpr 获取其值,但这可能并非最佳方案,或许应通过适当的 \fontdimen 参数获取)。
After the exact table I also give a table with values rounded to five decimal places.
精确表格之后,还提供了舍入到五位小数的转换表格。
Update: Following a suggestion done in a comment, the tables first. There seems to be something fishy with the em: it seems to be exactly 10pt+1sp and not the more intuitively reasonable 10pt. I would have guessed it should be exactly 10pt in that case with the default CM fonts. But I just tested with \the\fontdimen6\font and it also gave to my surprise 10.00002pt, (compiled with pdftex).
更新:根据评论中的建议,先展示表格。em 单位的数值存在异常:它似乎恰好等于 10pt+1sp,而非直觉上合理的 10pt。我本以为默认 CM 字体下 1em 应恰好等于 10pt,但通过 \the\fontdimen6\font 测试(使用 pdftex 编译),意外得到 10.00002pt。
Update: before turning off the internet for a while, I checked the em for bold, slanted and teletype: respectively 11.5pt-4sp, 10pt+1sp and 10.5pt-7sp. Perhaps some font expert could explain what is the mechanism? perhaps an underlying conversion from big points at some stage in the font creation process?
更新:断开网络前,我测试了粗体、倾斜体和打字机字体的 em 数值:分别为 11.5pt-4sp、10pt+1sp 和 10.5pt-7sp。或许字体专家能解释这一机制?可能是字体创建过程中某个阶段从大点(big points)进行转换导致的?
Update: I have played a bit with continued fractions, and a good approximation to 1dd is 107/100pt which is clear also from the decimal expansion in the second table, and a very good fraction approximation to 1dd in millimeters is 44/117 (the previous centered convergent is 3/8=0.375 which is already quite good compared to the exact 0.3760650274... value of 1dd in mm).
更新:通过连分数分析发现,1dd 的一个良好近似为 107/100pt(第二张表格的十进制展开也印证了这一点),而 1dd 对应的 mm 长度的一个极佳分数近似为 44/117(前一个中心收敛项为 3/8=0.375,与 1dd 精确的 0.3760650274...mm 已非常接近)。


\documentclass{article}
\usepackage{xintfrac}
% http://www.ctan.org/tex-archive/macros/generic/xint
% This code was compiled with the version 1.06a of the xint
% bundle dated 2013/05/09, which should appear soon on CTAN.
% The current CTAN version 1.06 should be also OK for this.
\usepackage{array}
\usepackage[hmargin=.5cm]{geometry}
% Conversions to the basic dimension, chosen to be the centimeter
% Base dimension: 1cm
\def\onecm {1}
\def\onemm {1/10}
\def\onein {2.54}
%% \def\onept {\xintMul {1/72.27}{\onein}}
%% simpler:
\def\onept {2.54/72.27}
%% \def\onebp {\xintMul {1/72}{\onein}}
%% simpler:
\def\onebp {2.54/72}
\def\onepc {\xintMul {12}{\onept}}
\def\oneex {\xintMul {\the\numexpr\dimexpr 1ex\relax\relax}{\onesp}}
\def\oneem {\xintMul {\the\numexpr\dimexpr 1em\relax\relax}{\onesp}}
\def\onedd {\xintMul {1238/1157}{\onept}}
% 1157 dd = 1238 pt I take this conversion factor from the TeXBook
% Wikipedia has other conversion factors, but of course here we
% have to do it the TeX way.
\def\onecc {\xintMul {12}{\onedd}}
\def\onesp {\xintMul {1/65536}{\onept}}
% Routines with delimited arguments, the good old TeX way
% (completely expandable)
\makeatletter
% exact conversion to an irreducible fraction:
% example: \convertexactly 126.2772pt\to {bp}
% and `pt' may be a macro expanding to it.
% idem for {bp} which may be a macro expanding to bp
\def\convertexactly #1\to #2%
{\xintIrr{\convertexactly@ #1\to {#2}}}%
% Variant with rounding at a number of decimal places
% given by first argument.
\def\convertwithrounding #1#2\to #3%
{\xintRound {#1}{\convertexactly@ #2\to {#3}}}%
% routines doing the job:
\def\convertexactly@ #1\to
{%
\romannumeral0%
\expandafter\expandafter\expandafter
\convertexactly@a
\xintReverseOrder {#1}\Z
}%
\def\convertexactly@a #1%
{%
\ifcat\noexpand #1\relax
\expandafter \convertexactly@b
\else
\expandafter \convertexactly@c
\fi #1%
}%
\def\convertexactly@b #1#2\Z #3%
{%
\xintdiv {\xintMul {\xintReverseOrder{#2}}{\csname one#1\endcsname}}
{\csname one#3\endcsname}%
}%
\def\convertexactly@c #1#2#3\Z #4%
{%
\xintdiv {\xintMul {\xintReverseOrder{#3}}{\csname one#2#1\endcsname}}
{\csname one#4\endcsname}%
}%
\makeatother
\def\bigstrut {\vbox to 24pt{}\vbox to 12pt{}}%
\begin{document}
Testing:
72.27pt is exactly \convertexactly 72.27pt\to {bp}bp
1/2.54in is exactly \convertexactly 1/2.54in\to {mm}mm
10pt is exactly (for this font) \convertexactly 10pt\to {ex}ex,
or approximately \convertwithrounding{20}10pt\to {ex}ex
10pt is exactly (for this font) \convertexactly 10pt\to {em}em,
or approximately \convertwithrounding{20}10pt\to {em}em
1em is exactly (for this font) \convertexactly
1em\to {pt}pt, or approximately
\convertwithrounding{20}1em\to {pt}pt.
And indeed
\verb+\the\dimexpr 1em\relax+ gives \the\dimexpr 1em\relax{} and
\verb+\the\fontdimen6\font+ gives \the\fontdimen6\font
1ex is exactly (for this font) \convertexactly 1ex\to {pt}pt, or approximately
\convertwithrounding{20}1ex\to {pt}pt.
And indeed
\verb+\the\dimexpr 1ex\relax+ gives \the\dimexpr 1ex\relax.
\def\tableentry{$\displaystyle\xintFrac{\convertexactly 1\colunit\to\rowunit}$\bigstrut}
\begin{center}\begin{tabular}
{>{\def\colunit{pt}}l<{\tableentry}
>{\def\colunit{mm}}l<{\tableentry}
>{\def\colunit{cm}}l<{\tableentry}
>{\def\colunit{ex}}l<{\tableentry}
>{\def\colunit{em}}l<{\tableentry}
>{\def\colunit{bp}}l<{\tableentry}
>{\def\colunit{dd}}l<{\tableentry}
>{\def\colunit{pc}}l<{\tableentry}
>{\def\colunit{in}}l<{\tableentry}
>{\bfseries}l}
\multicolumn{1}{l}{\bfseries 1pt} & \multicolumn{1}{l}{\bfseries 1mm} &
\multicolumn{1}{l}{\bfseries 1cm} & \multicolumn{1}{l}{\bfseries 1ex} &
\multicolumn{1}{l}{\bfseries 1em} & \multicolumn{1}{l}{\bfseries 1bp} &
\multicolumn{1}{l}{\bfseries 1dd} & \multicolumn{1}{l}{\bfseries 1pc} &
\multicolumn{1}{l}{\bfseries 1in} & \\
\gdef\rowunit{pt} & & & & & & & & & \rowunit\\
\gdef\rowunit{mm} & & & & & & & & & \rowunit\\
\gdef\rowunit{cm} & & & & & & & & & \rowunit\\
\gdef\rowunit{ex} & & & & & & & & & \rowunit\\
\gdef\rowunit{em} & & & & & & & & & \rowunit\\
\gdef\rowunit{bp} & & & & & & & & & \rowunit\\
\gdef\rowunit{dd} & & & & & & & & & \rowunit\\
\gdef\rowunit{pc} & & & & & & & & & \rowunit\\
\gdef\rowunit{in} & & & & & & & & & \rowunit\\
\end{tabular}\end{center}
\clearpage
\def\tableentry{\convertwithrounding {5}1\colunit\to\rowunit}
\begin{center}\begin{tabular}
{>{\def\colunit{pt}}l<{\tableentry}
>{\def\colunit{mm}}l<{\tableentry}
>{\def\colunit{cm}}l<{\tableentry}
>{\def\colunit{ex}}l<{\tableentry}
>{\def\colunit{em}}l<{\tableentry}
>{\def\colunit{bp}}l<{\tableentry}
>{\def\colunit{dd}}l<{\tableentry}
>{\def\colunit{pc}}l<{\tableentry}
>{\def\colunit{in}}l<{\tableentry}
>{\bfseries}l}
\multicolumn{1}{l}{\bfseries 1pt} & \multicolumn{1}{l}{\bfseries 1mm} &
\multicolumn{1}{l}{\bfseries 1cm} & \multicolumn{1}{l}{\bfseries 1ex} &
\multicolumn{1}{l}{\bfseries 1em} & \multicolumn{1}{l}{\bfseries 1bp} &
\multicolumn{1}{l}{\bfseries 1dd} & \multicolumn{1}{l}{\bfseries 1pc} &
\multicolumn{1}{l}{\bfseries 1in} & \\
\gdef\rowunit{pt} & & & & & & & & & \rowunit\\
\gdef\rowunit{mm} & & & & & & & & & \rowunit\\
\gdef\rowunit{cm} & & & & & & & & & \rowunit\\
\gdef\rowunit{ex} & & & & & & & & & \rowunit\\
\gdef\rowunit{em} & & & & & & & & & \rowunit\\
\gdef\rowunit{bp} & & & & & & & & & \rowunit\\
\gdef\rowunit{dd} & & & & & & & & & \rowunit\\
\gdef\rowunit{pc} & & & & & & & & & \rowunit\\
\gdef\rowunit{in} & & & & & & & & & \rowunit\\
\end{tabular}\end{center}
\end{document}

Actually the two approximations, given with 20 places after the decimal mark, for 1em and 1ex, are exact, the denominators are powers of 2, the complete decimal expansion only has zeros after those shown. I still do not quite understand why 1em turns out to be 655361sp and not 655360sp=10pt in the case of the CM font. This 10pt+1sp is strange.
实际上,1em 和 1ex 保留 20 位小数的“近似值”是精确的,因为其分母为 2 的幂次,完整十进制展开中显示的数字后均为零。但我仍未完全理解,为何 CM 字体中 1em 等于 655361sp,而非 655360sp=10pt——这种 10pt+1sp 的情况较为特殊。
edited Jun 10, 2020 at 12:32
answered May 9, 2013 at 20:42
user4686
Comments on Answer 2
-
+1, neat! Two suggestions: Use the
parskippackage to get some space between the “sentences”. Put the picture first in your answer so that it’s closer to your remarks; unlike in most other answers, the output seems more important than the code here.
+1,非常简洁!两点建议:使用parskip宏包在“句子”之间添加间距;将图片置于回答开头,使其更靠近相关说明——与大多数其他回答不同,此处输出结果似乎比代码更重要。
—— doncherry
Commented May 9, 2013 at 21:02 -
Sorry. What is the exact range in
pt?
抱歉,pt单位的精确范围是多少?
—— kiss my armpit
Commented Jul 6, 2014 at 9:41 -
@kissmyarmpit sorry for delay. The maximal input for a dimension expressed in pt is
16383.99999237060546874pt. Anything bigger creates a dimension too large error. This corresponds to1073741823sp(2^30-1). In fact the minimal spec which gives this maximal allowed value is16383.99997711181640625pt. Anything smaller gives at most2^30-2 sp.
@kissmyarmpit 抱歉回复延迟。以pt为单位的维度输入最大值为16383.99999237060546874pt,超过该值将触发“维度过大”错误。该最大值对应1073741823sp(即 2 30 − 1 2^{30}-1 230−1)。实际上,能得到该最大值的最小输入为16383.99997711181640625pt,更小的输入最多只能得到2^30-2 sp。
—— user4686
Commented Mar 27, 2017 at 8:14 -
@kissmyarmpit using five decimal digits, both
16383.99998ptand16383.99999ptgive the maximal dimension, but not16383.99997pt. And\the\maxdimenoutputs16383.99998pt. But as I said16383.99999ptis still acceptable (try\the\dimexpr 16383.99999pt\relax.) but not16383.999993pt.
@kissmyarmpit 若保留五位小数,16383.99998pt和16383.99999pt均能得到最大维度,但16383.99997pt不能。\the\maxdimen的输出结果为16383.99998pt,但如前所述,16383.99999pt仍为有效输入(可尝试\the\dimexpr 16383.99999pt\relax),而16383.999993pt则无效。
—— user4686
Commented Mar 27, 2017 at 8:22
Answer 3 (user2478)

And TeX’s nice arithmetic can be seen …
TeX 的出色算术运算能力在此可见一斑…
\documentclass{article}
\usepackage{printlen}
\parindent=0pt
\newlength\Length \Length=1cm
\begin{document}
\tabular{p{2cm}p{2cm}}
\mbox{--- 1cm ---}
\uselengthunit{cm}\printlength{\Length}\
\uselengthunit{mm}\printlength{\Length}\
\uselengthunit{in}\printlength{\Length}\
\uselengthunit{pt}\printlength{\Length}\
\uselengthunit{bp}\printlength{\Length}\
\uselengthunit{sp}\printlength{\Length}\
\uselengthunit{pc}\printlength{\Length}\
&
\mbox{--- 1em ---}
\Length=1em
\uselengthunit{cm}\printlength{\Length}\
\uselengthunit{mm}\printlength{\Length}\
\uselengthunit{in}\printlength{\Length}\
\uselengthunit{pt}\printlength{\Length}\
\uselengthunit{bp}\printlength{\Length}\
\uselengthunit{sp}\printlength{\Length}\
\uselengthunit{pc}\printlength{\Length}
\endtabular
\end{document}
edited Mar 27, 2017 at 17:03
Werner♦
Comments on Answer 3
-
Heh! I like how the centimeter didn’t come out right…
哈!厘米的转换结果居然不准确,真有意思……
—— SamB
Commented Jan 4, 2011 at 19:18 -
1exis supposed to be the height of ‘x’, and1emis supposed to the width of ‘m’ – so they should vary with the font and font size. Do they?
1ex本应等于字母 ‘x’ 的高度,1em本应等于字母 ‘m’ 的宽度——因此它们应随字体和字体大小变化。实际情况是否如此?
—— frabjous
Commented Jan 5, 2011 at 2:27 -
@frabjous:
1emis the width of M and sure they vary
@frabjous:1em等于字母 M 的宽度,且它们确实会随字体和字体大小变化。
—— user2478
Commented Jan 5, 2011 at 6:57 -
Thanks, Herbert. That’s an important thing to note for the question asked.
谢谢 Herbert,这对所提问题来说是一个重要的注意点。
—— frabjous
Commented Jan 5, 2011 at 19:31 -
@frabjous: This isn’t quite right for fonts with narrow M’s, see https://en.wikipedia.org/wiki/Em_(typography)
@frabjous:对于 M 字母较窄的字体,这一说法并不完全准确。
—— Caramdir
Commented Jan 5, 2011 at 23:33
Answer 4 (Torbjørn T.)
- An inch is
25.4 mm.
1 英寸(in)等于25.4 mm(参考:http://en.wikipedia.org/wiki/Inch)。 - For TeX,
1 ptis1/72.27 in, which is0.351459804 mm.
在 TeX 中,1 pt等于1/72.27 in,即0.351459804 mm。 - For most other software,
1 ptis1/72 in, which is0.352777778 mm. Also called Postscript Point, in TeX this is called a big point (bp).
在大多数其他软件中,1 pt等于1/72 in,即0.352777778 mm,也称为 PostScript 点,在 TeX 中称为大点(bp)。 exandemare not a fixed length, as they depend on the fontsize. (See this question.)
ex和em并非固定长度,其值取决于字体大小(详见:此问题)。
Edit: Herberts answer shows you how to find the length of 1 ex and 1em in your document.
编辑:Herbert 的回答展示了如何在文档中获取 1 ex 和 1em 的具体长度。
edited Apr 13, 2017 at 12:35
Comments on Answer 4
-
You have a small typo here. 1 TeX point is
1/72.27inch (not1/72.72inch). BTW I think it is not widely known that Google’s calculator supports both points. Exemplary searches: 1 TeX point - 1/72.27 inch, 1 PS point - 1/72 inch, both obviously returning 0.
此处存在一个小笔误:1 TeX 点等于1/72.27英寸(而非1/72.72英寸)。顺便说一句,谷歌计算器支持这两种点单位的转换,这一点似乎并不为人熟知。示例搜索:1 TeX point - 1/72.27 inch、1 PS point - 1/72 inch,结果均显然为 0。
—— przemoc
Commented Jun 27, 2011 at 20:37 -
@przemoc: Indeed, you’re right, thanks. I don’t think I’ll edit the post though, that’ll bring the question to the top of the list again, and the correct value is in the accepted answer anyway.
@przemoc:确实如此,感谢指正。但我不打算修改该回答,因为这会将问题重新置顶,而正确数值已在已采纳的回答中给出。
—— Torbjørn T.
Commented Jun 27, 2011 at 20:54 -
You shouldn’t care about such small detail as moving your answer to the top, because keeping the mistake is definitely bad. Moreover, Ctrl+F on
1/72.27returns only my comment, which is pretty bad, because it is formal definition of point in TeX and should be clearly mentioned somewhere, i.e. in the main text of answer, not in a comment.
你不应在意回答置顶这样的小细节,因为保留错误显然更不可取。此外,使用 Ctrl+F 搜索1/72.27仅能找到我的评论,这非常不妥——因为这是 TeX 中点单位的正式定义,应在回答正文中明确提及,而非隐藏在评论中。
—— przemoc
Commented Jun 27, 2011 at 21:06 -
I’ve just recherched point sizes and history, because I have found conflicting information about the Teχ point. It is in fact
1/72.27of the modern (25.4 mm) inch, or800/803of the PostScript point (1/72inch).
我刚刚研究了点单位的大小和历史,因为发现关于 TeX 点的信息存在冲突。实际上,TeX 点等于现代英寸(25.4 mm)的1/72.27,或 PostScript 点(1/72英寸)的800/803。
—— mirabilos
Commented Aug 11, 2015 at 15:38
Answer 5 (egreg)
The same as Philippe Goutet’s, but using the fp module of LaTeX3; however the syntax of the \convertto command is different:
与 Philippe Goutet 的回答类似,但使用 LaTeX3 的 fp 模块;不过 \convertto 命令的语法有所不同:
\convertto{1in}{pt}
returns 72.26999, just the number. With \convertto*{1in}{pt} we’d get 72.26999pt, with the unit.
上述代码返回 72.26999(仅数值);使用 \convertto*{1in}{pt} 则返回 72.26999pt(包含单位)。
\documentclass[a4paper]{article}
\usepackage{array}
\usepackage[hmargin=2cm]{geometry}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\convertto}{smm}
{
\egreg_convertto:nn {#2}{#3}
\IfBooleanT{#1}{#2}
}
\cs_new:Npn \egreg_convertto:nn #1 #2
{
\fp_eval:n { round( \dim_to_decimal:n { #1 } / \dim_to_decimal:n {1#2} , 5 ) }
}
\ExplSyntaxOff
\begin{document}
\begin{center}\begin{tabular}
{>{\def\colunit{pt}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{mm}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{cm}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{ex}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{em}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{bp}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{dd}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{pc}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{in}}l<{\convertto{1\colunit}{\rowunit}}
>{\bfseries}l}
\multicolumn{1}{l}{\bfseries 1pt} & \multicolumn{1}{l}{\bfseries 1mm} &
\multicolumn{1}{l}{\bfseries 1cm} & \multicolumn{1}{l}{\bfseries 1ex} &
\multicolumn{1}{l}{\bfseries 1em} & \multicolumn{1}{l}{\bfseries 1bp} &
\multicolumn{1}{l}{\bfseries 1dd} & \multicolumn{1}{l}{\bfseries 1pc} &
\multicolumn{1}{l}{\bfseries 1in} & \\
\gdef\rowunit{pt} & & & & & & & & & \rowunit\\
\gdef\rowunit{mm} & & & & & & & & & \rowunit\\
\gdef\rowunit{cm} & & & & & & & & & \rowunit\\
\gdef\rowunit{ex} & & & & & & & & & \rowunit\\
\gdef\rowunit{em} & & & & & & & & & \rowunit\\
\gdef\rowunit{bp} & & & & & & & & & \rowunit\\
\gdef\rowunit{dd} & & & & & & & & & \rowunit\\
\gdef\rowunit{pc} & & & & & & & & & \rowunit\\
\gdef\rowunit{in} & & & & & & & & & \rowunit\\
\end{tabular}\end{center}
\end{document}
Both \convertto and \convertto* can be used in an expandable context.
\convertto 和 \convertto* 均可在可扩展上下文中使用。
The same table but with siunitx (with the already built one for comparison). One needs to prepare the body of the table beforehand.
以下是使用 siunitx 宏包生成的相同表格(与上述表格对比),需提前准备表格主体内容:
\documentclass[a4paper]{article}
\usepackage{siunitx,array}
\usepackage[hmargin=1cm]{geometry}
\usepackage{xparse}
\ExplSyntaxOn
\seq_new:N \g_egreg_convertto_units_seq
\seq_new:N \l_egreg_convertto_temp_seq
\tl_new:N \l_egreg_convertto_body_tl
\seq_gset_from_clist:Nn \g_egreg_convertto_units_seq
{
pt, mm, cm, ex, em, bp, dd, pc, in
}
\cs_new_protected:Nn \__egreg_convertto_maketable:
{
\seq_map_inline:Nn \g_egreg_convertto_units_seq
{
\seq_clear:N \l_egreg_convertto_temp_seq
\seq_map_inline:Nn \g_egreg_convertto_units_seq
{
\seq_put_right:Nx \l_egreg_convertto_temp_seq { \egreg_convertto:nn { 1####1 } { ##1 } }
}
\tl_put_right:Nx \l_egreg_convertto_body_tl
{
\seq_use:Nn \l_egreg_convertto_temp_seq { & } & \exp_not:n { \textbf{##1} \\ }
}
}
\seq_set_map:NNn \l_egreg_convertto_temp_seq \g_egreg_convertto_units_seq { {\exp_not:N \textbf{1\,##1}} }
}
\NewDocumentCommand{\maketable}{}
{
\__egreg_convertto_maketable:
\begin{tabular}{*{\seq_count:N \g_egreg_convertto_units_seq}{S[table-format=2.5]}l}
\seq_use:Nn \l_egreg_convertto_temp_seq { & } \\
\l_egreg_convertto_body_tl
\end{tabular}
}
\NewExpandableDocumentCommand{\convertto}{smm}
{
\egreg_convertto:nn {#2}{#3}
\IfBooleanT{#1}{#2}
}
\cs_new:Npn \egreg_convertto:nn #1 #2
{
\fp_eval:n { round( \dim_to_decimal:n { #1 } / \dim_to_decimal:n {1#2} , 5 ) }
}
\ExplSyntaxOff
\begin{document}
\begin{center}
\sisetup{group-digits=false}
\maketable
\end{center}
\begin{center}
\begin{tabular}
{>{\def\colunit{pt}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{mm}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{cm}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{ex}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{em}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{bp}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{dd}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{pc}}l<{\convertto{1\colunit}{\rowunit}}
>{\def\colunit{in}}l<{\convertto{1\colunit}{\rowunit}}
>{\bfseries}l}
\multicolumn{1}{l}{\bfseries 1pt} & \multicolumn{1}{l}{\bfseries 1mm} &
\multicolumn{1}{l}{\bfseries 1cm} & \multicolumn{1}{l}{\bfseries 1ex} &
\multicolumn{1}{l}{\bfseries 1em} & \multicolumn{1}{l}{\bfseries 1bp} &
\multicolumn{1}{l}{\bfseries 1dd} & \multicolumn{1}{l}{\bfseries 1pc} &
\multicolumn{1}{l}{\bfseries 1in} & \\
\gdef\rowunit{pt} & & & & & & & & & \rowunit\\
\gdef\rowunit{mm} & & & & & & & & & \rowunit\\
\gdef\rowunit{cm} & & & & & & & & & \rowunit\\
\gdef\rowunit{ex} & & & & & & & & & \rowunit\\
\gdef\rowunit{em} & & & & & & & & & \rowunit\\
\gdef\rowunit{bp} & & & & & & & & & \rowunit\\
\gdef\rowunit{dd} & & & & & & & & & \rowunit\\
\gdef\rowunit{pc} & & & & & & & & & \rowunit\\
\gdef\rowunit{in} & & & & & & & & & \rowunit\\
\end{tabular}\end{center}
\end{document}
edited Nov 23, 2017 at 16:40
Comments on Answer 5
-
It seems that TeX’s memory you used is more important. For Philippe :777 strings out of 493481 10679 string characters out of 3140965 67406 words of memory out of 3000000 For you 7172 strings out of 493481 128969 string characters out of 3140965 174995 words of memory out of 3000000
似乎你使用的 TeX 内存更多。Philippe 的代码使用情况:493481 个字符串中占用 777 个,3140965 个字符串字符中占用 10679 个,3000000 个内存字中占用 67406 个;而你的代码使用情况:493481 个字符串中占用 7172 个,3140965 个字符串字符中占用 128969 个,3000000 个内存字中占用 174995 个。
—— Alain Matthes
Commented May 9, 2013 at 8:16 -
@AlainMatthes Loading
expl3on top of LaTeX2e has a big impact. If I loadxparseover Philippe’s macros I get 7166 strings out of 493481, 128879 string characters out of 3140966, 175547 words of memory out of 3000000. LaTeX3 will surely consume more memory than LaTeX2e, at the advantage of a cleaner approach.
@AlainMatthes 在 LaTeX2e 基础上加载expl3会产生较大影响。若在 Philippe 的宏包代码中加载xparse,内存使用情况为:493481 个字符串中占用 7166 个,3140966 个字符串字符中占用 128879 个,3000000 个内存字中占用 175547 个。LaTeX3 的内存占用确实高于 LaTeX2e,但优势在于代码结构更清晰。
—— egreg
Commented May 9, 2013 at 8:52 -
@AlainMatthes There’s a reason that the LaTeX3 approach didn’t fly at all when it was first developed (early 1990s). As egreg says, there is a cost to having a more structured approach, but the idea is that you enhance code readability and logic.
@AlainMatthes LaTeX3 最初开发时(20 世纪 90 年代初)未能普及是有原因的。如 egreg 所述,结构化更强的方案必然伴随一定代价,但其核心目标是提升代码的可读性和逻辑性。
—— Joseph Wright ♦
Commented May 10, 2013 at 6:50 -
Not sure what is happening. I copied the code for your answer into a MacTeX2016 (updated a few days ago) TeXShop file. The first row and first column are fine, but all other entries are incorrect. For example, Column 2 Row 2 is 8.09555 and Column 2 Row 9 is 205.62699. Did I neglect something?
不确定发生了什么。我将你的回答中的代码复制到 MacTeX2016(几天前更新过)的 TeXShop 文件中,第一行和第一列的结果正确,但其他所有条目均不正确。例如,第 2 列第 2 行的结果为 8.09555,第 2 列第 9 行的结果为 205.62699。我是否遗漏了什么?
—— sgmoye
Commented Mar 27, 2017 at 20:03 -
@sgmoye Yes, some internals in expl3 have changed. Updated.
@sgmoye 是的,expl3 的部分内部机制已发生变化,代码已更新。
—— egreg
Commented Mar 27, 2017 at 20:21
Answer 6 (yannisl)
My posting just crossed the other ones. Best to view it, in action.
我的回答与其他回答不谋而合,建议实际运行代码查看效果。
\documentclass[11pt]{article} % use larger type; default would be 10pt
\usepackage{xcolor}
\begin{document}
\newdimen\temp
\def\alength#1#2{
\temp#1
\nointerlineskip \baselineskip=2pt
\vbox{\hbox{\hskip-29pt\texttt{\textcolor{#2}{#1=\the\temp}}}
\hbox{\vtop{\color{#2}\hrule width 130pt\vspace{#1}\hrule width 130pt}}%
\vspace{0.9cm}}
}
\alength{1cm}{blue}
\alength{1pc}{red}
\alength{1mm}{orange}
\alength{1cc}{red}
\alength{1dd}{red}
\alength{100000sp}{purple}
\alength{1in}{black}
\alength{1bp}{brown}
\alength{1em}{black}
\alength{1ex}{black}
\end{document}
answered Jan 4, 2011 at 17:16
yannisl
Comments on Answer 6
-
It is better if you put all in one base line to make a comparison.
若将所有单位的长度置于同一基准线上进行对比,效果会更好。
—— Display Name
Commented Jan 4, 2011 at 17:26 -
@xport The base is the relationship to points! I tried to give something to visualize the length (with the exception of the sp, which is smaller than the visible wavelength and hence I scaled that by100K. Unit conversion tables are normally done like this.
@xport 基准是与点单位(pt)的关系!我尝试通过可视化方式展示长度(除sp单位外,因其小于可见光波长,故放大 10 万倍展示)。单位转换表格通常采用这种呈现方式。
—— yannisl
Commented Jan 4, 2011 at 18:32
What are the possible dimensions / sizes / units LaTeX understands?
LaTeX 支持的维度/尺寸/单位有哪些?
I know there are different ways of expressing sizes or dimensions in LaTeX such as points (pt), inches (in) and ex.
我知道 LaTeX 中有多种表示尺寸或维度的方式,例如点(pt)、英寸(in)和 ex 等。
As some commands, such as \hspace understand all of them, I would like to have a reference or complete list of possible dimensions or sizes including a description of what they mean.
由于某些命令(如 \hspace)支持所有这些单位,我希望获得一份完整的维度/尺寸单位参考列表,包括各单位的含义说明。
edited Nov 1, 2014 at 11:52
lockstep
asked Jan 17, 2012 at 14:27
Henrik
Comments on the Question
- The definitive reference is the TeXbook by Donald Knuth; the source of which is freely available.
权威参考资料是 Donald Knuth 所著的《TeX 手册》(The TeXbook),其源代码可免费获取。
—— Martin Schröder
Commented Jan 17, 2012 at 16:59
Answers
Answer 1 (Stefan Kottwitz♦)
From the plain TeX reference:
根据 Plain TeX 参考手册,LaTeX 支持的单位包括:
pt: Point
pt:点(印刷行业常用单位)pc: pica (12pt)
pc:皮卡(1 皮卡 = 12 点)in: inch (72.27pt)
in:英寸(1 英寸 = 72.27 点)bp: Big point (72bp= 1in)
bp:大点(1 英寸 = 72 大点,即 PostScript 点)cm: Centimeter
cm:厘米mm: Millimeter
mm:毫米dd: Didot point (1157dd= 1238pt)
dd:迪多点(1157 迪多点 = 1238 点)cc: cicero (12dd)
cc:西塞罗(1 西塞罗 = 12 迪多点)sp: Scaled point (65536sp= 1pt), the smallest TeX unit
sp:缩放点(1 点 = 65536 缩放点),TeX 中最小的单位ex: Nominal x-height
ex:名义 x 高度(通常等于字母 ‘x’ 的高度)em: Nominal m-width
em:名义 m 宽度(通常等于字母 ‘m’ 的宽度)
Available in math mode:
数学模式专用单位:
mu: math unit, 1em= 18mu, whereemis taken from the math symbols family, various lengths are derived from it (thinspace,thickspace, etc.)
mu:数学单位(1em= 18mu,其中em取自数学符号字体族),多种长度(如thinspace、thickspace等)均基于该单位推导。
Additionally available in pdfTeX and LuaTeX:
pdfTeX 和 LuaTeX 额外支持的单位:
px: “pixel”, the dimension given to the\pdfpxdimenprimitive; default value is 1bp, corresponding to a pixel density of 72 dpi
px:像素(由\pdfpxdimen原语定义),默认值为 1bp,对应 72 dpi 的像素密度。
The meanings of the various points are described here:
各类点单位的详细含义可参考:
edited Apr 13, 2017 at 12:35
Comments on Answer
-
It is worth to note that “sp” is the smallest TeX unit and that it cannot be subdivided further. Thus any length in TeX is an integer multiple of “sp”.
值得注意的是,sp是 TeX 中最小的单位,无法进一步细分。因此,TeX 中的任何长度均为sp的整数倍。
—— AlexG
Commented Jan 17, 2012 at 14:39 -
em: It is M-width
em:等于字母 M 的宽度。
—— user2478
Commented Jan 17, 2012 at 14:39 -
pdftexandluatexhave also thepxunit, whose value can be changed on a per document basis (default 1px = 1bp).
pdftex和luatex还支持px单位,其值可按文档单独设置(默认 1px = 1bp)。
—— egreg
Commented Jan 17, 2012 at 14:41 -
there’s also the
mu– math unit (1 em = 18 mu, where em is taken from the math symbols family). this can be used only in math mode.
还有mu——数学单位(1 em = 18 mu,其中 em 取自数学符号字体族),仅可在数学模式中使用。
—— barbara beeton
Commented Jan 17, 2012 at 14:49
Answer 2 (Tobi)
I made visual overview for all units available in TeX. Including a comparison and the definitions/conversions.
我制作了 TeX 所有可用单位的可视化概览,包含单位对比、定义及转换关系。

The complete code and PDFs (EN, DE; b/w, color) are available at GitHub:
完整代码及 PDF 文档(英文、德文;黑白、彩色版本)可在 GitHub 上获取:
https://github.com/tweh/tex-units
edited Jul 3, 2016 at 20:45
answered May 23, 2016 at 15:28
Tobi
What value TeX uses as its minimal unit
TeX 使用的最小单位具体是什么?
TeX uses scaled point as its minimal unit. At least it’s said so in the TeXbook:
TeX 以缩放点(scaled point)作为最小单位。至少《TeXbook》中是这样表述的:
TeX represents all dimensions internally as an integer multiple of the tiny units called sp.
TeX 会将所有维度在内部表示为名为 sp 的微小单位的整数倍。
65536 sp = 2¹⁶ sp = 1 pt.
And later on,
后续章节中还提到:
TeX actually does its calculations with integer multiples of 2⁻¹⁶…
TeX 实际上是通过 2⁻¹⁶ 的整数倍进行所有计算的……
BUT, compile this and you will see, that 0.00000762939453125 starts to be distinguishable from zero, which means that it’s TeX’s minimal unit. (0.00000762939453125 is exactly 2⁻¹⁷)
但,编译以下代码后你会发现,0.00000762939453125 开始能与零区分开,这似乎意味着它才是 TeX 的最小单位(注:0.00000762939453125 恰好等于 2⁻¹⁷)。
\line{\hskip 0pt plus 16383.99999fil and now?\hskip 0pt plus 0.000007629394531249fill}
\line{\hskip 0pt plus 16383.99999fil and now?\hskip 0pt plus 0.000007629394531250fill}
\bye
So, what is the real minimal unit: 2⁻¹⁶ or 2⁻¹⁷?
那么,TeX 真正的 最小单位究竟是 2⁻¹⁶ 还是 2⁻¹⁷?
2 Answers
Answer 1 (egreg)
When you say
当你输入以下代码时:
\dimen0=0.00000762939453125pt
a \showthe\dimen0 instruction will answer
\showthe\dimen0 命令的输出结果为:
> 0.00002pt.
because this is what 1sp looks like when shown in points.
这是因为 1sp 以 pt 为单位显示时,其数值就是 0.00002pt。
What happens? TeX does binary arithmetic, deep down inside the program. The number you input is recognized as greater than zero (one should look into the program code) and so the dimension is set to the smallest positive one.
背后的原理是什么?TeX 在程序底层采用二进制算术运算。你输入的数值被识别为大于零(可参考程序源代码),因此该维度被设置为最小的正维度单位。
Similarly, after
类似地,执行以下代码后:
\skip0=0pt plus 0.000007629394531250fill \showthe\skip0
you get
输出结果为:
> 0.0pt plus 0.00002fill.
so never is a positive dimension less than 1sp used. The value you input can appear less than 1sp, but the value that TeX uses is 1sp.
因此,TeX 绝不会 使用小于 1sp 的正维度。你输入的数值可能看似小于 1sp,但 TeX 实际处理时会将其转换为 1sp。
The conversion from decimal number to scaled integer is described in module 102 of tex.web; the integral part is just multiplied by 65536, so it’s only interesting for numbers in the form 0.d₀d₁…dₖ₋₁.
十进制数到缩放整数(scaled integer)的转换过程在 tex.web 的第 102 模块中有详细说明:整数部分直接乘以 65536,因此该转换仅对 0.d₀d₁…dₖ₋₁ 形式的小数有研究意义。
The final scaled integer is stored in a which is initialized as 0. Then we start from the far right and at each step a is assigned the result of
最终的缩放整数存储在变量 a 中,初始值设为 0。之后从最右侧的数字开始,每一步通过以下公式更新 a 的值:
(a + dᵢ × 2¹⁷) div 10
其中 div 表示截断除法(truncating division)。
Finally, a is assigned the value (a + 1) div 2 (with truncation).
最后,通过公式 (a + 1) div 2(同样采用截断除法)得到最终的 a 值。
If we carry out this algorithm, we get the following numbers as values of a (in parentheses the digit examined)
若对 0.00000762939453125 执行该算法,a 的中间值变化如下(括号内为当前处理的数字):
(5) 65536
(2) 32768
(1) 16384
(3) 40960
(5) 69632
(4) 59392
(9) 123904
(3) 51712
(9) 123136
(2) 38528
(6) 82496
(7) 100000
(0) 10000
(0) 1000
(0) 100
(0) 10
(0) 1
final step (1+1) div 2 = 1
The div operation works by truncation.
其中 div 表示截断除法(即舍弃小数部分取整)。
I leave as an exercise to show that 0.0000152587890625 = 2⁻¹⁶ produces 1 as well and that 0.000007629394531249 produces 0 instead.
作为练习,读者可自行验证:0.0000152587890625(即 2⁻¹⁶)经该算法处理后结果同样为 1,而 0.000007629394531249 处理后结果为 0。
As you see, the algorithm uses 2¹⁷ to ensure 16 bit precision of the fractional part.
由此可见,该算法引入 2¹⁷ 是为了保证小数部分的 16 位精度。
Comment on Answer 1 (user4686)
your description is incomplete: the final value of a is divided by 2 and rounded by the assignment a <- (a+1)div 2.
你的描述不够完整:变量 a 的最终值是通过 a <- (a+1)div 2 实现除以 2 并 四舍五入 的效果,而非单纯截断。
Answer 2 (user4686)
When you input a dimension as a certain (positive) decimal number of points, it will be rounded to the nearest integral multiple of 1/65536 pt = 1sp, where numbers from N.5 inclusive to (N+1).5 exclusive are rounded to N+1.
当你输入一个以 pt 为单位的正十进制维度值时,TeX 会将其舍入为最接近的 1/65536 pt = 1sp 的整数倍。舍入规则为:数值在 [N.5, (N+1).5) 区间内时,舍入为 N+1。
This can be illustrated by the following experiments. They need the e-TeX extensions (compile with pdftex or etex), but I checked that the conclusions are obeyed in Knuth tex.
以下实验可验证该结论(需使用 e-TeX 扩展,通过 pdftex 或 etex 编译),且经核实,Knuth 原版 tex 同样遵循该规则。
I also checked mathematically that the specific algorithm described in egreg’s answer produces exactly what I am saying here. See below.
我还通过数学推导验证:egreg 回答中描述的具体算法,其结果与上述舍入规则完全一致(推导过程见下文)。
% compile with etex or pdftex
\input xintexpr.sty
\tt
\def\Test #1{% #1 = integer
\def\a {#1/65536}%
\def\b {(#1+1)/65536}%
\def\delta {1/65536}%
This is #1/65536 in decimal: \xinttheiexpr [30]\a \relax.\endgraf
This is (#1+1)/65536 in decimal: \xinttheiexpr [30]\b \relax.\endgraf
\def\Iterate {%
\edef\c {\xinttheiexpr [30](\a+\b)/2\relax }%
\ifnum\number\dimexpr \c pt\relax >#1
\let\b\c
\else
\let\a\c
\fi
\edef\delta {\xinttheexpr \delta/2\relax }%
}%
\loop
\Iterate
\xintifboolexpr {\delta < 10^(-25)}{\iffalse}{\iftrue}%
\repeat
\edef\A {\xinttheiexpr [30]\a*65536\relax}%
\edef\B {\xinttheiexpr [30]\b*65536\relax}%
$\a \approx {\A\over 65536}$\endgraf
$\a$ pt is represented internally as
$\number\dimexpr \a pt\relax$ (sp).\endgraf
$\b \approx {\B \over 65536}$\endgraf
$\b$ pt is represented internally as
$\number\dimexpr \b pt\relax$ (sp).\endgraf
\vskip.5cm
}
\Test {0}
\Test {1}
\Test {2}
\Test {3}
\Test {17}
\Test {123456789}
\nopagenumbers
\bye

(update corrects typo in indices)
(更新:修正了索引中的拼写错误)
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\usepackage{newtxtext,newtxmath}
\let\leq\leqslant
\let\geq\geqslant
\begin{document}\pagestyle{empty}
Let us start from a decimal number $x$ in $[0,1)$ with $k$ digits after
decimal mark.
\[ x = 0.d_k\cdots d_1\]
Enumerating the digits this way facilitates my description next. We set
$a_0=0$ and define repetitively up to $j=k$ the following non-negative integers:
\[ a_{j+1} = \lfloor \frac{a_j + 2^{17}\cdot d_{j+1}}{10}\rfloor \]
Once we reach $a_k$ we do a final definition:
\[ a = \lfloor \frac{a_k+1}2 \rfloor\]
By definition
\[ a_1 \leq \frac{d_1\cdot 2^{17}}{10} < a_1 + 1\]
\[ 10a_1 \leq d_1 \cdot 2^{17} < 10a_1 + 10\]
Because everything in sight is integer valued, we can improve this to:
\[ 10a_1 \leq d_1 \cdot 2^{17} \leq 10a_1 + 9\]
Similarly
\[a_2 \leq \frac{a_1 + d_2\cdot 2^{17}}{10} < a_2 + 1\]
\[ 10a_2 \leq a_1 + d_2 \cdot 2^{17} < 10a_2 + 10\]
Again everything in sight is integer valued:
\[ 10 a_2 \leq a_1 + d_2 \cdot 2^{17} \leq 10a_2 +9 = 10a_2 + 10 - 1\]
We transform this into
\[ 10^2 a_2 \leq 10^1 a_1 + 10^1 d_2\cdot 2^{17}\leq 10^2 a_2 + 100 - 10\]
Then, similarly
\[10^3 a_3 \leq 10^2 a_2 + 10^2 d_3\cdot 2^{17}\leq 10^3 a_3 + 10^3 - 10^2\]
\[10^4 a_4 \leq 10^3 a_3 + 10^3 d_4\cdot 2^{17}\leq 10^4 a_4 + 10^4 - 10^3\]
up to
\[10^k a_k \leq 10^{k-1}a_{k-1} + 10^{k-1}d_{k}\cdot 2^{17}\leq 10^k a_k +
10^k - 10^{k-1}\]
If we add up everything and simplify the common terms we end up with
\[ 10^k a_k\leq (10^{k-1}d_{k}+\cdots + d_1)\cdot 2^{17}\leq 10^k a_k + 10^k
-1\]
Thus
\[ \frac{a_k}2 \leq 2^{16}\cdot x \leq \frac{a_k + 1 - 10^{-k}}2 <
\frac{a_k+1}2\]
If $a_k = 2j$ is even, the rule of \textsc{D.~Knuth} is now to set $a = j$ and
this $j$ satifies
\[ j \leq 2^{16}\cdot x < j + 0.5\]
If $a_k = 2j+1$ is odd, the rule of \textsc{D.~Knuth} is to set $a = j+1$ and
we have
\[ j + 0.5 \leq 2^{16}\cdot x < j +1\]
Hence in all cases we have the formula:
\[ a = \lfloor 2^{16}\cdot x+0.5\rfloor\]
in other words $a$ is the \textbf{rounded} value of $2^{16}\cdot x$.
As originally claimed by your humble servant.
Sincerely,\par
Mercredi 04 mars 2015 à 15:27:34\par
typo corrected Mercredi 04 mars 2015 à 16:01:51
\vskip.2cm
\hrule
\end{document}

Let me also add some extra argument to explain why TeX’s scanning discards all but the first seventeen digits after decimal mark:
以下补充说明为何 TeX 仅保留小数部分的前 17 位数字(其余部分将被舍弃):
(typo corrected, it was §452 not §402)
(拼写错误修正:原表述为 §402,实际应为 §452)

To dispel potential confusion recall that the above is only for decimal numbers of the shape 0.abcedf.... Digits before the dot are treated separately, it is only for digits after the dot that only 17 at max need to be kept in the process.
需澄清的是,上述规则仅适用于 0.abcedf... 形式的小数。小数点前的整数部分将单独处理,仅小数部分在转换过程中最多保留 17 位数字。
Comments on Answer 2
-
at jfbu: it does not compile via pdftex - pastebin.com/Y9UBW1RY
致 jfbu:该代码无法通过 pdftex 编译——参考链接:pastebin.com/Y9UBW1RY– Igor Liferenko
Commented Mar 4, 2015 at 9:43 -
@IgorLiferenko try updating xint. The
\xinttheiexpr [30]thing was introduced in release1.1 (2014/10/28).
@IgorLiferenko 建议更新 xint 宏包。\xinttheiexpr [30]语法是在1.1版本(2014年10月28日发布)中引入的。– user4686
Commented Mar 4, 2015 at 9:49 -
alternatively replace everywhere in the code
\xinttheiexpr [30] stuff\relaxby\xintRound {30}{\xinttheexpr stuff\relax}. Be careful to use\xinttheexprand not\xinttheiexprin this variant.
另一种解决方案:将代码中所有\xinttheiexpr [30] stuff\relax替换为\xintRound {30}{\xinttheexpr stuff\relax}。注意在该变体中使用\xinttheexpr而非\xinttheiexpr。– user4686
Commented Mar 4, 2015 at 9:54 -
In this context, and in contrast, TeX truncates, rather than rounding, when an operation on dimensions does not give an integral number of
sp. Example:\dimen0 123sp,\divide \dimen0 by 2,\number\dimen0returns61and more striking\dimen0 129sp,\divide \dimen0 by 10,\number\dimen0returns12.
需对比说明的是:当维度运算结果并非sp的整数倍时,TeX 会采用 截断 而非舍入的方式处理。例如:\dimen0 123sp执行\divide \dimen0 by 2后,\number\dimen0输出61;更典型的例子是\dimen0 129sp执行\divide \dimen0 by 10后,输出结果为12。– user4686
Commented Mar 4, 2015 at 10:15 -
@IgorLiferenko No, to tug.org/TUGboat/tb35-1/tb109knut.pdf
@IgorLiferenko 并非如此,可参考:tug.org/TUGboat/tb35-1/tb109knut.pdf– egreg
Commented Mar 5, 2015 at 9:45
Original Comments
补充评论
-
The real minimal unit is
1sp, period.
真正的最小单位就是1sp,仅此而已。– Werner ♦
Commented Mar 4, 2015 at 8:19 -
Really matter? I wonder if anyone would really note the difference of about 0.005 µm (the size of the smallest viruses) in a document :
这真的重要吗?我很好奇,有人会在意文档中约 0.005 微米(最小病毒的尺寸)的差异吗 :– Fran
Commented Mar 4, 2015 at 21:45 -
@Fran “A scaled point is huge, more than two farshimmelt potrzebies; it’s more than 53 Angstrom units, 'way bigger than a hydrogen atom.” : )
@Fran “一个缩放点(sp)其实很大——比两个‘farshimmelt potrzebies’还大,超过 53 埃,比氢原子大得多。”:)
(注:“farshimmelt potrzebies”为虚构单位,出自 Knuth 幽默表述;1 埃 = 0.1 纳米)– Igor Liferenko
Commented Mar 10, 2015 at 9:12 -
I know the TUGboat article : I understand Donald’s concern about that
\maxdimendo not reach Sagan’s number in meters (for covering a building with a banner, for example) but I do not share his concern for the arrival of the nanotechnology era. Even thinking in VR glasses, I can’t visualize the future with nanobooks or miliportzebieposters.
我读过那篇 TUGboat 上的文章 : 我理解 Donald(Knuth)的顾虑——他担心\maxdimen(TeX 支持的最大维度)换算成米后未达到“萨根数”(Sagan’s number,天文学中的大数),比如用横幅覆盖整栋建筑时可能不够用,但我并不认同他对纳米技术时代的担忧。即便考虑虚拟现实眼镜,我也无法想象未来会出现“纳米书籍”或“毫波特泽比海报”(注:“portzebie”为虚构长度单位)。– Fran
Commented Mar 10, 2015 at 12:29
LaTeX 间距调整命令使用
1. 数学公式的间距
用于微调公式内符号、表达式的间距,单位统一为 mu(数学单位,1 mu = 1/18 em,与当前字体大小成比例)。
| 命令 | 描述 | 等效间距 | 说明 |
|---|---|---|---|
\, | 正薄间距(常用微调) | +3 mu | |
\! | 负薄间距(压缩默认间距) | –3 mu | |
\: | 中等间距(比 \, 更宽) | +4 mu | |
\; | 厚间距(公式内明显分隔) | +5 mu | |
\quad | 较大间距(约 1 em ≈ 18 mu) | +18 mu | |
\qquad | 最大固定间距(约 2 em ≈ 36 mu) | +36 mu | |
\mkern <number>mu | 自定义间距(支持正负值) | <number> mu(如 \mkern 8mu 为 +8 mu) | 仅在数学模式下可用 |
示例代码
\documentclass{article}
\usepackage{amsmath} % 加载数学公式必备包
\begin{document}
% 1. 默认间距(公式内符号默认间距较紧凑)
\[ f(x) = x + \sin x + \log x \]
% 2. 用 \, 增加薄间距(优化符号间呼吸感)
\[ f(x) = x \, + \, \sin x \, + \, \log x \]
% 3. 用 \! 压缩间距(解决符号重叠感,如分式、积分内)
\[ \int_{0}^{1} \! \frac{1}{1+x^{2}}\,dx \] % 积分符与被积函数间距压缩
% 4. 用 \mkern 自定义间距(按需设置特定间距)
\[ a \mkern 12mu + \mkern -2mu b \mkern 15mu = \mkern 8mu c \]
\end{document}
2. 文本的间距
用于调整单词、字符或段落内的水平间距,单位支持 pt(点)、cm(厘米)、em(当前字体宽度)等常规长度单位。
| 命令 | 描述 | 等效间距(示例) | 说明 |
|---|---|---|---|
\, | 正薄间距(文本内细微加宽) | +3 pt | |
\! | 负薄间距(文本内细微压缩) | –3 pt | |
\: | 中等间距(比 \, 更宽) | +4 pt | |
\; | 厚间距(文本内明显分隔) | +5 pt | |
\quad | 较大间距(约 1 em) | +18 pt | |
\qquad | 最大固定间距(约 2 em) | +36 pt | |
\hspace{<length>} | 自定义水平间距(支持绝对/相对单位) | 如 \hspace{5pt}、\hspace{0.5cm} | |
\hskip <length> | 自定义水平间距(语法更简洁) | 如 \hskip 3em(3 个字符宽度) | 与 \hspace 功能相同 |
\space | 单个空格(等效于键盘空格,可显式调用) | 1 个标准空格宽度 | 仅在文本模式下有效 |
示例代码
\documentclass{article}
\begin{document}
% 1. 默认间距(单词间标准空格)
This is a sample text for spacing test.
% 2. 用 \, 增加薄间距(优化特定字符组合的视觉效果)
This\,is\,a\,sample\,text\,for\,spacing\,test.
% 3. 用 \hspace 自定义固定间距(强制设置字符/单词间距)
LaTeX\hspace{8pt}is\hspace{0.3cm}a\hspace{2em}typesetting\,system.
% 4. 用 \hskip 简化自定义间距(与 \hspace 效果一致)
We\hskip 10pt need\hskip 15pt more\hskip 20pt space.
\end{document}
说明
LaTeX 间距调整命令按场景划分清晰,区别在于适用模式和单位类型:
- 单位差异:
- 数学模式的默认单位
mu与字体大小绑定(如 12pt 字体的 1 mu 大于 10pt 字体) - 文本模式的
pt、cm为固定单位 - 自定义间距时需根据需求选择合适单位
- 命令限制:
\mkern是数学模式专属命令,无法在文本模式中使用- 文本模式的
\space在数学模式中无效,需用\,等替代
- 使用原则:
- 优先使用默认间距,仅在排版拥挤或松散时微调
- 公式中优先用
\,/\!等mu单位命令 - 文本中优先用
\hspace/\hskip等长度单位命令 - 保证间距与场景适配
via:
- best practices - Which measurement units should one use in LaTeX? - TeX - LaTeX Stack Exchange
https://tex.stackexchange.com/questions/4239/which-measurement-units-should-one-use-in-latex - conversion - What are the various units (ex, em, in, pt, bp, dd, pc) expressed in mm? - TeX - LaTeX Stack Exchange
https://tex.stackexchange.com/questions/8260/what-are-the-various-units-ex-em-in-pt-bp-dd-pc-expressed-in-mm - lengths - What are the possible dimensions / sizes / units LaTeX understands? - TeX - LaTeX Stack Exchange
https://tex.stackexchange.com/questions/41370/what-are-the-possible-dimensions-sizes-units-latex-understands - What value TeX uses as its minimal unit - TeX - LaTeX Stack Exchange
https://tex.stackexchange.com/questions/231269/what-value-tex-uses-as-its-minimal-unit/ - Lengths in LaTeX - Overleaf, Online LaTeX Editor
https://www.overleaf.com/learn/latex/Lengths_in_LaTeX - Help On LaTeX Lengths
- https://abel.math.harvard.edu/computing/latex/tetex/latex/latex2e-html/ltx-86.html
- Units of Length - LaTeX - W3cubDocs
https://docs.w3cub.com/latex/units-of-length.html - Units of length (LaTeX2e unofficial reference manual (January 2025))
https://latexref.xyz/Units-of-length.html - ……


33万+

被折叠的 条评论
为什么被折叠?



