Skip to content

Labels and cross-references

LaTeX Lesson 10 2:05 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
Lesson two showed headings renumbering themselves, and left a gap. 第 2 课展示了标题会自动重新编号,但留下了一个缺口。
Here it is. 就是这个。
The sentence says "as shown in Section 2". 那句话写着"如见第 2 节"。
Add a section above Methods and everything below it moved: Methods is now three. 在 Methods 上面加一节,它下面的一切都移动了:Methods 现在是 3。
But the sentence still says two, because you typed that two by hand. 但那句话还是写着 2,因为那个 2 是你手敲的。
It is now wrong, and nothing warned you. 它现在错了,而没有任何东西警告过你。
The fix is to stop writing numbers. 解决办法是别再写数字。
Tag the heading with label and a name — sec colon methods, a nickname you choose, and nothing about it appears on the page. 用 \label 加一个名字给那个标题打上标记—— sec:methods,一个你自己起的外号,而它本身不会出现在页面上。
Then in the sentence, write ref with that same name. 然后在句子里,用 \ref 写上同一个名字。
LaTeX puts in whatever number it currently has. LaTeX 会填进它当前的那个编号。
And the tilde before it is a space that never breaks, so the word Section and its number stay on the same line. 而前面那个波浪号是一个永不换行的空格, 好让 "Section" 和它的编号留在同一行上。
Now do the same insertion again. 现在再做一次同样的插入。
Methods becomes three, and this time the sentence changed too. Methods 变成 3,而这一次那句话也跟着变了。
Nothing chased it — the sentence never held a number in the first place, only a name, and the number was filled in at compile time. 没有人去追它——那句话从一开始就没有装过一个数字, 它装的只是一个名字,而数字是在编译时填进去的。
That is the whole trick, and it is worth using from your very first draft. 整个窍门就在这里,而且从你的第一稿起就值得用它。
One consequence worth knowing. 有一个后果值得知道。
On the first compile LaTeX learns what number each label ended up with, and a brand-new reference prints two question marks. 第一遍编译时,LaTeX 才弄清楚每个标签最后拿到的是哪个编号, 而一个全新的引用会打印出两个问号。
On the second it fills them in. 第二遍它才把它们填上。
That is because a ref can point forward, to something further down the file, so the number cannot be known on a single read. 这是因为一个引用可以往前指,指向文件更后面的东西, 所以只读一遍是不可能知道那个编号的。
Our Run does both passes for you. 我们的 Run 会替你跑完两遍。
Four things to take with you. 带走四点。
One: label gives a section or equation a nickname. 第一:\label 给一节或一个公式起一个外号。
Two: ref prints its current number, wherever you are in the document. 第二:\ref 打印它当前的编号,不管你在文档的什么位置。
Three: a tilde before ref keeps the word and the number together. 第三:\ref 前面的波浪号让那个词和编号待在一起。
Four: it takes two compiles, and Run does both. 第四:它需要编译两遍,而 Run 会两遍都跑。
Now label your section and point at it. 现在给你的那一节打上标签,然后去引用它。

Log in or create account

IGCSE, A-Level & AP