烟台市建设局网站,像素时代网站建设手机站设计,团购网站短信平台,洞头区网站建设收费注#xff1a;本文为 “LaTeX | 文档单位排版” 相关讨论合辑。 英文引文#xff0c;机翻未校。 如有内容异常#xff0c;请看原文。 What package should I use to typeset units?
应该使用哪个宏包来排版单位#xff1f;
I know there are a few packages related to …注本文为 “LaTeX | 文档单位排版” 相关讨论合辑。英文引文机翻未校。如有内容异常请看原文。What package should I use to typeset units?应该使用哪个宏包来排版单位I know there are a few packages related to typesetting units:我知道有几个与单位排版相关的宏包unitsdefunits, which can be seen as a “base package” used byunitsdefunits可视为unitsdef所依赖的“基础宏包”SIunitssiunitx, which is the successor toSIunitssiunitx是SIunits的后续版本maybe others? likecjwunitsandhepunits或许还有其他的例如cjwunits和hepunitsWhat is the difference between them? Do they also work inside Math mode? And, more importantly, which one should I use?它们之间有什么区别在数学模式Math mode下是否同样适用更重要的是应该选择哪一个Suggestion for people trying to answer this question: show some examples of how to typeset both simple and complex units. For instance, try typesetting these items:给试图回答这个问题的人的建议展示一些简单和复杂单位的排版示例。例如尝试排版以下内容100 Celsius3 x 10^5 km/sgravitational constant is measured in N m2/kg210 kHz 1 s^{-4}sin(x) meters\documentclass{article} \usepackage{siunitx} % 加载单位排版宏包 \usepackage{amsmath} % 用于数学环境 \begin{document} \begin{itemize} \item 100 Celsius\qty{100}{\degreeCelsius} % 渲染效果$100^\circ\text{C}$ \item $3 \times 10^5$ km/s\qty{3e5}{\km\per\s} % 渲染效果$3 \times 10^5 \, \text{km/s}$ \item gravitational constant is measured in $\text{N}\,\text{m}^2/\text{kg}^2$ 引力常量的单位为 \unit{\newton\metre\squared\per\kilogram\squared} % 渲染效果$\text{N}\,\text{m}^2/\text{kg}^2$ \item $10 \, \text{kHz} 1 \, \text{s}^{-4}$ \( \qty{10}{\kilo\hertz} \qty{1}{\per\second\tothe{4}} \) % 渲染效果$10 \, \text{kHz} 1 \, \text{s}^{-4}$ \item $\sin(x)$ meters\qty[parse-numbers false]{\sin(x)}{\metre} % 渲染效果$\sin(x) \, \text{m}$ \end{itemize} \end{document}10 0 ∘ C 100^\circ\text{C}100∘C3 × 1 0 5 km/s 3 \times 10^5 \, \text{km/s}3×105km/sN m 2 / kg 2 \text{N}\,\text{m}^2/\text{kg}^2Nm2/kg210 kHz 1 s − 4 10 \, \text{kHz} 1 \, \text{s}^{-4}10kHz1s−4sin ( x ) m \sin(x) \, \text{m}sin(x)mI think these examples cover most needs when writing scientific texts: simple measurements (value simple unit), values written in scientific notation, simple fractional units, complex fractional and squared units, units without any value accompanying them, units with arbitrary exponents, and arbitrary math code instead of a simple value.我认为这些示例涵盖了撰写科学文献时的大部分需求简单测量值数值简单单位、科学计数法表示的数值、简单分数形式单位、复杂分数及平方形式单位、无伴随数值的单位、含任意指数的单位以及用任意数学代码替代简单数值的情况。edited Aug 24, 2010 at 11:45Joseph Wright♦asked Aug 24, 2010 at 11:34Denilson Sá MaiaAnswersAs the author ofsiunitxI have some idea about the relative strengths of the various alternatives. There are various things that need to be done for units:作为siunitx宏包的作者我对各类替代方案的相对优势有所了解。单位排版需要实现以下多项功能Semantic input of units, so\kilo\metrerather thankm: this allows special effects such as reformatting fractions ‘on the go’ (some people prefer to use literal input, and this ideally needs to be supported too).单位的语义化输入例如使用\kilo\metre而非km这支持即时重新格式化分数等特殊效果部分用户倾向于使用字面输入理想情况下也应支持该方式。Formatting of numbers, for example dividing up digits into groups.数值格式化例如将数字分组显示。Correctly presenting awkward symbols such as the micro symbol.正确呈现特殊符号如微符号μ。Setting the font: many guidelines call for units to be in upright roman font with no bold or italic variation.字体设置许多规范要求单位使用直立罗马字体upright roman font不使用粗体或斜体变体。Keeping numbers and units together (no space breaking)保持数值与单位的关联性不允许换行分隔Many packages have been written to address one or other aspect of this question. For example,SIunitsis good at providing semantic input, whereasSIstylefocusses on correct typesetting but leaves the input to the user. On the other hand,unitsdefsticks to doing only units and does not worry about other aspects.已有多个宏包针对上述部分功能需求而开发。例如SIunits擅长提供语义化输入而SIstyle专注于正确排版但将输入方式交由用户自行处理。此外unitsdef仅专注于单位相关功能不涉及其他方面。The aim of thesiunitxpackage is to cover all of the requirements above, and to provide a way that package behaviour can be altered. Thus it uses a series of key-value options that will change how the output is formatted. The options can be applied to the entire document or to an individual piece of input. It also is designed to work in either math or text mode equally well. I have also been very careful to provide up to date advice about the internationally-agreed units in the documentation ofsiunitx.siunitx宏包的目标是覆盖上述所有需求并提供可调整宏包行为的方式。为此它采用一系列键值对选项来改变输出格式这些选项可应用于整个文档或单个输入内容。该宏包同时设计为在数学模式和文本模式下均能良好工作且其文档中包含了关于国际通用单位的最新建议。As a demo, using the latest release ofsiunitxthe following all show off the package:以下是使用最新版本siunitx的演示示例展示了该宏包的功能\documentclass{article} \usepackage{siunitx} \begin{document} \begin{itemize} \item \qty{100}{\degreeCelsius} \item \qty{3e5}{\km\per\s} or \qty[per-mode symbol]{3e5}{\km\per\s} \item \unit{\newton\metre\squared\per\kilogram\squared} or \unit[per-mode symbol]{\newton\metre\squared\per\kilogram\squared} \item $\qty{10}{\kilo\hertz} \qty{1}{\per\second\tothe{4}}$ \item \qty[parse-numbers false]{\sin(x)}{\metre} \end{itemize} \end{document}10 0 ∘ C 100^\circ\text{C}100∘C3 × 1 0 5 km s − 1 or 3 × 1 0 5 km/s 3 \times 10^5 \, \text{km}\,\text{s}^{-1} \text{ or } 3 \times 10^5 \, \text{km/s}3×105kms−1or3×105km/sN m 2 kg − 2 or N m 2 / kg 2 \text{N}\,\text{m}^2\,\text{kg}^{-2} \text{ or } \text{N}\,\text{m}^2/\text{kg}^2Nm2kg−2orNm2/kg210 kHz 1 s − 4 10 \, \text{kHz} 1 \, \text{s}^{-4}10kHz1s−4sin ( x ) m \sin(x) \, \text{m}sin(x)mThere are a couple of notes to bear in mind if usingsiunitx. First, it requires e-TeX, which can be an issue with some publishers (even over 10 years after it was finalised!). Secondly, the formatting does take up some time. There are settings to turn things off but it’s always the case that working by hand will ultimately be faster. However, the upside of clearer and more flexible input is in my opinion worth it.使用siunitx时需注意两点首先它依赖 e-TeX这可能对部分出版商造成限制即使在 e-TeX 定稿十多年后依然如此其次格式化过程会占用一定时间尽管可通过设置关闭部分功能但手动排版终究更快。不过在我看来更清晰、更灵活的输入方式所带来的优势是值得的。One other point is thatsiunitxis under development, with bug fixes and new features (there is a list for v3.1). In contrast, bothSIunitsandSIstyleare depreciated: bug fixes only. Theunitsandunitsdefpackages have not been updated for many years.另一点需要说明的是siunitx仍在持续开发中包含错误修复和新功能v3.1 版本有相关功能列表。相比之下SIunits和SIstyle已被废弃仅提供错误修复而units和unitsdef宏包已多年未更新。edited Jun 24, 2021 at 10:39answered Aug 24, 2010 at 13:36Joseph Wright♦Great answer! I gotta look at the code sample later, with more time. You forgot to citecjwunitsandhepunits.很棒的回答我之后会抽时间仔细看看代码示例。你忘记提到cjwunits和hepunits了哦。– Denilson Sá MaiaCommented Aug 24, 2010 at 20:26hepunitsis an extension ofSIunits. I’m not familiar withcjwunits, but a quick read through the code suggests is a simple approach to providing something likeunitsdef-style units.hepunits是SIunits的扩展包。我对cjwunits不太熟悉但快速浏览其代码后发现它采用简单的方式提供了类似unitsdef风格的单位功能。– Joseph Wright ♦Commented Aug 24, 2010 at 20:43Doessiunitxhave any conflict insidealign*environment? I get! Package siunitx Error: Invalid token \protect in numerical input.siunitx在align*环境中是否存在冲突我遇到了错误! Package siunitx Error: Invalid token \protect in numerical input.– Kedar MhaswadeCommented Nov 5, 2020 at 0:40Is this advice still current (2021)? In particular, the code example seems different from what is supported by currentsiunitx. I’ve always usedunitsdefand I was going to update tosiunitxsince it’s actively maintained, but the two packages are incompatible, so this prevents a smooth transition, forcing me to change every unitsdef usage in a document at once, which could be a bit disruptive.这些建议在 2021 年仍然适用吗特别是代码示例似乎与当前siunitx支持的语法不同。我一直使用unitsdef由于siunitx仍在积极维护我打算升级到该宏包但这两个宏包不兼容导致无法平滑过渡必须一次性修改文档中所有unitsdef的用法这可能会带来一定的麻烦。– LorenzoDonati4Ukraine-OnStrikeCommented Dec 21, 2021 at 15:12LorenzoDonati–Codidact.com The code example was updated recently to match the v3 change. As you say,siunitxis incompatible withunitsdef: that’s a consequence of using the same document command names.LorenzoDonati–Codidact.com 代码示例最近已更新以适配 v3 版本的变化。如你所说siunitx与unitsdef不兼容这是由于两者使用了相同的文档命令名称所致。– Joseph Wright ♦Commented Dec 21, 2021 at 18:57Why use a package to typeset units?为什么需要使用宏包来排版单位I’ve seen a lot of posts about different packages for typesetting units and their relative strengths and weaknesses. Most posts seem to agree that thesiunitxpackage is the most up to date package, but I haven’t really figured out the advantages of using a package at all. What is the advantage of writing something like:我看过很多关于不同单位排版宏包及其优缺点的帖子。大多数帖子似乎都认为siunitx是最新的宏包但我始终没有弄明白使用宏包的根本优势。例如使用以下写法的优势是什么\SI{10}{\kilo\gram\meter\per\second\squared}when you could just write:而不是直接写成kg m s$^-2$Or for numbers, what’s the difference between或者对于数值以下两种写法有什么区别\num{10}and和10edited Apr 9, 2013 at 23:33doncherryasked Apr 9, 2013 at 23:24VyasYou can write\SI{10}{kg m s^{-2}}, which guarantees that the unit will be typeset in the correct way: the right space (or multiplication symbol if you so decide) between the various parts, no line break between them, the right glyph shape (upright even if the context is italics) and so on.你可以写成\SI{10}{kg m s^{-2}}这能确保单位以正确的方式排版各部分之间的间距恰当或根据设置使用乘号、不允许换行、字形正确即使在斜体上下文下仍保持直立等。– egregCommented Apr 9, 2013 at 23:30egreg You should write\SI{10}{\kg\m\per\square\s}instead of\SI{10}{kg m s^{-2}}to get the correct spacing and a consistent output.egreg 为了获得正确的间距和一致的输出你应该写成\SI{10}{\kg\m\per\square\s}而不是\SI{10}{kg m s^{-2}}。– Svend TveskægCommented Apr 9, 2013 at 23:42SvendTveskæg I forgot the dots:\SI{10}{kg.m.s^{-2}}or, for the fraction form,\SI{10}{kg.m/s^{2}}.SvendTveskæg 我忘记加圆点了\SI{10}{kg.m.s^{-2}}或者分数形式\SI{10}{kg.m/s^{2}}。– egregCommented Apr 9, 2013 at 23:49I would still say that the other way is “more” correct; check, e.g., the following:\documentclass{article} \usepackage{siunitx} \DeclareSIUnit\second{Horse} \begin{document} Should you write \SI{10}{\kg\m\per\square\s} or \SI{10}{kg.m.s^{-2}}? \end{document}. The formatting options is only avaliable with the approach I suggest.我仍然认为另一种方式“更”正确例如查看以下代码\documentclass{article} \usepackage{siunitx} \DeclareSIUnit\second{Horse} \begin{document} Should you write \SI{10}{\kg\m\per\square\s} or \SI{10}{kg.m.s^{-2}}? \end{document}。只有我建议的方式支持格式化选项。– Svend TveskægCommented Apr 9, 2013 at 23:54Correct spacing for larger numbers is also a plus.KaTeX parse error: Undefined control sequence: \num at position 1: \̲n̲u̲m̲{10000}will output 10 000. Writing10 000 10\,00010000, seems very hacky to me.大数的正确间距也是一个优势。\num{10000}会输出 10 000而手动写成10\,000在我看来非常繁琐。– N3buchadnezzarCommented Apr 18, 2013 at 7:40AnswersI came to this question thinking “yeah! Why?” but upon reading the currently extant comments and answer I have already changed my tune and am now tending the other way. What I realized, thinking about it and about my small interactions with this package, is that it provides much the same servicewithinLaTeX as LaTeX (or even just TeX itself) provides for writing as a whole. To understand that point, consider the alternatives in communicating some math in writing via a computer:我看到这个问题时的第一反应是“对啊为什么要这么麻烦”但在阅读了现有的评论和回答后我的观点发生了转变现在更倾向于使用宏包。通过思考以及我对该宏包的少量使用经验我意识到它在 LaTeX 中提供的功能与 LaTeX甚至只是 TeX 本身在整体写作中提供的服务非常相似。为了理解这一点我们可以对比通过计算机书面交流数学内容的几种方式Write it phonetically, as it were. Words are words, of course, but Greek letters are spelled out, or perhaps drawn in some ad-hoc ASCII art, and more complicated formulas are hacked together in an unspecified manner that may or may not be clear from context.可以说是“语音化”书写。文字本身就是文字但希腊字母需要拼写出来或者用临时的 ASCII 艺术绘制更复杂的公式则以不确定的方式拼凑而成其含义可能需要结合上下文才能理解。Write it in TeX, in which Greek letters are spelled out in macros, more complicated formulas are hacked together in some possibly awful manner…and then the result is “compiled” into a document making it all look like you had in mind, so no harm done if your source was a little more schematic than you’d have liked.使用 TeX 书写其中希腊字母通过宏命令拼写更复杂的公式可能以某种不太理想的方式拼凑而成……但最终结果会“编译”成文档呈现出你想要的样子因此即使源文件比你期望的更具示意性也不会有问题。Thesiunitxpackage does exactly this, but instead of math formulas, it does SI-style units. Sure, one could write the same symbols that normally appear in such units askgors$^{-1}$, but that can go wrong visually very easily depending on if the unit appears in math mode or not (just imagine how that affects the latter!), or if several units become crammed together, or (egreg’s quite valid observation) broken across a line. Then you may decide that instead ofs$^{-1}$you really want/sfor the unit of hertz, necessitating a global change of notation.siunitx宏包正是这样做的但它处理的是国际单位制SI风格的单位而非数学公式。当然人们可以直接写出单位中常见的符号如kg或s$^{-1}$但这种方式很容易出现视觉上的问题——这取决于单位是否在数学模式下使用试想这会对后者产生怎样的影响或者多个单位是否拥挤在一起又或者正如 egreg 所指出的合理观察单位被换行分隔。之后你可能会决定将赫兹的单位从s$^{-1}$改为/s这就需要全局修改符号表示方式。These are all the kinds of problems that are handled in TeX by defining macros containing your preferred styles for\kilogramand\hertzand so on. Thesiunitxjust organizes the whole project of defining these macros and their relationships to each other, and gives a lot of aesthetic options on top of it.这些问题在 TeX 中都可以通过定义宏来解决例如为\kilogram和\hertz等单位定义你偏好的样式。siunitx宏包只是将定义这些宏及其相互关系的整个过程系统化并在此基础上提供了大量的美观排版选项。Basically, it boils down to “Why would anyone want to program their document in the first place?”, to which the answer is “It’s easier to write well-structured documents that way.”归根结底这相当于问“为什么有人愿意用编程的方式来撰写文档”而答案是“这样更容易写出结构良好的文档”。answered Apr 9, 2013 at 23:40Ryan ReichAllow me to give an answer in the form of a practical anecdote:请允许我通过一个实际的轶事来回答这个问题When I wrote my PhD dissertation in chemistry, each chapter had either been published in or was soon to be submitted to a peer-reviewed journal. Each chapter was written at a different time over the course of several years, and of course each journal had its own standards as to whether there should be a space between the number and the unit, whether inverse units should be given as fractions or exponents, etc. Besides that, when I would just type out the units, sometime I would add a space and sometime I wouldn’t, and so ended up doing a lot of proofreading to catch this until I hit onsiunit, and then onsiunitx.当我撰写化学博士论文时每个章节要么已经发表在同行评审期刊上要么即将提交。这些章节是在几年间的不同时间撰写的而每个期刊都有自己的格式标准——例如数值与单位之间是否需要空格、倒数单位应采用分数形式还是指数形式等。此外当我直接输入单位时有时会加空格有时不会这导致我需要花费大量时间校对直到我发现了siunit宏包后来又使用了siunitx。By turning the units into macros, I had one less formatting issue that I had to worry about, and when I took the body of the chapter, I just had to write a new header using the journal’s style package and the correctsiunitxparameters (or, if the journal didn’t support it, hard-code the macros in the preamble), and all the units and numbers were automagically formatted correctly. When I then needed to put them into my thesis, I just had to\input{}the body of the text after a\chapter{}tag, and all of my chapters were formatted to my university thesis standards with no fuss, no muss. Meanwhile, my friends who were working with Word were spending weeks and sometimesmonthsdoing nothing but reformatting and proofreading to get everything consistent (most of that spent on equation, figure, and reference numbering that I never gave a second thought to).通过将单位转换为宏命令我少了一个需要担心的格式问题。当我使用章节正文时只需使用期刊的样式宏包和正确的siunitx参数编写新的页眉如果期刊不支持该宏包则在导言区硬编码宏命令所有的单位和数值就会自动格式化为正确的样式。之后当我需要将这些内容整合到论文中时只需在\chapter{}命令后使用\input{}导入正文所有章节就会自动按照大学的论文标准格式化无需额外操作。与此同时我那些使用 Word 的朋友们却要花费数周甚至数月的时间专门进行格式调整和校对以确保所有内容的一致性其中大部分时间用于处理公式、图表和参考文献的编号而这些在 LaTeX 中我从未费心考虑过。In fact, the file that had my macro definitions ended up being almost as long as my chapters, since every time I had a mathematical symbol (superscripts, subscripts, bold, etc.), I created a macro with a descriptive name so that not only was it easier to proofread, but if for some reason I had to change a symbol or variable, I only had to change it once. Same idea with units. If everything is a macro, if you need to change something, you only have to change it once and that propagates itself throughout the document without having to worry.事实上我用于存储宏定义的文件最终几乎和章节正文一样长。因为每次遇到数学符号上标、下标、粗体等我都会创建一个具有描述性名称的宏命令这样不仅更容易校对而且如果由于某种原因需要修改符号或变量只需修改一次即可。单位的处理也是如此。如果所有内容都通过宏命令定义那么修改时只需改动一处更改就会自动传播到整个文档无需额外担心。answered Apr 10, 2013 at 0:42craigimdoncherry Thanks. I lurked around for quite a while before I found a question I actually knew the answer todoncherry 谢谢。我潜伏了很久才找到一个我确实知道答案的问题– craigimCommented Apr 10, 2013 at 0:53As with all of the other components ofTeX,LaTeX, and friends, the idea is to separate content from form与TeX、LaTeX及相关工具的所有其他组件一样其核心思想是实现内容与形式的分离。We wish to specify the changes globally in the preamble, and not worry about the details things in the document.我们希望在导言区全局指定格式设置而不必担心文档中的细节问题。Similar questions:类似的问题Why don’t we write\textbf{1. Introduction}instead of\section{Introduction}?为什么我们不写成\textbf{1. Introduction}而是使用\section{Introduction}Why do we use theenumerateenvironment when we could just write(a),(b),(c)etc ?为什么我们使用enumerate环境而不是直接写成(a)、(b)、(c)等Why do we choose to make our graphics inLaTeX(usingtikzandpstricksfor example) when we could make them externally and just include them?为什么我们选择在 LaTeX 中制作图形例如使用tikz和pstricks宏包而不是在外部制作后再导入Why do we usebiblatex(and other bibliography packages) to organize our references- couldn’t we just type them in the correct order ourselves?为什么我们使用biblatex及其他参考文献宏包来管理参考文献而不是手动按正确顺序输入edited Apr 9, 2013 at 23:45answered Apr 9, 2013 at 23:36cmhughesI see your point in general, and I agree with the basic principle of separating style and content inLaTeX. I just never really considered units as something required that much formatting, although clearly there’s a lot of small details I didn’t consider such as italicization and spacing我大致理解你的观点也同意 LaTeX 中内容与样式分离的基本原则。我只是从未真正认为单位需要如此多的格式设置尽管显然有很多细节我没有考虑到例如斜体和间距问题。– VyasCommented Apr 10, 2013 at 2:02Whether in math mode or not, I will无论是否在数学模式下我都会always use\numfromsiunitxfor any numerical literals,对所有数值常量始终使用siunitx宏包的\num命令always use.for the decimal marker in the decimal literals,在数值常量中始终使用.作为小数点分隔符but later set the global setting in the preamble to specify the decimal marker for the rendered output.但之后在导言区通过全局设置指定输出文档中的小数点分隔符。\documentclass{article} \usepackage{siunitx} \sisetup{ %output-decimal-marker{,}% just uncomment if you want to use comma as the decimal marker! } \begin{document} $\num{3.14}$ is not the value of $\pi$, \num{2.718281828} is not the value of $e$. \end{document}This mechanism makes your document portable!这种机制使你的文档具有可移植性answered Apr 10, 2013 at 0:46kiss my armpitHow do you feel about using\numfor integer literals?你对将\num用于整数常量有何看法– quserCommented Jun 4, 2020 at 4:17The advantage of using a package is that you get set the numbers and units in a consistent typographycal way. Sometimes you have to use a spatium (\,) between number and unit, sometimes not. Packagesiunitxknows about this and does it automatically right for you.使用宏包的优势在于你可以以一致的排版方式设置数值和单位。有时需要在数值和单位之间使用间距\,有时则不需要。siunitx宏包了解这些规则并会自动为你正确处理。edited Apr 13, 2013 at 12:12answered Apr 9, 2013 at 23:28Menschvia:fonts - What package should I use to typeset units? - TeXhttps://tex.stackexchange.com/questions/2248/what-package-should-i-use-to-typeset-unitsbest practices - Why use a package to typeset units? - TeXhttps://tex.stackexchange.com/questions/107818/why-use-a-package-to-typeset-unitsSymbols - TeXipediahttps://www.texipedia.com/symbols