在 org-mode 导出中,如何按编号引用一个章节?

18

我正在使用org-mode并尝试生成一个链接来引用一个章节的编号,而不是它的标题。

* Section One
:PROPERTIES:
:CUSTOM_ID: sec:one
:END:

* Section Two
#+label: sec:two

I can reference Section One with  [[#sec:one]] and [[#sec:one][Section One]],
but I can't get the actual section number (1) to resolve.

我想看见

As you can see in Section 1

通过编写以下内容

As you can see in Section [[sec:one]],

有什么想法吗?
4个回答

32

我为此使用专用目标

* Section One
  <<sec:one>>

* Section Two
  <<sec:two>>

I can reference Section One with  [[sec:one]] and [[sec:one][Section One]],
but I can get the actual section number (1) to resolve.

这个工作符合预期;请参考orgmode文档中有关内部链接的内容


嘿Tom,当我尝试这个时,在输出中专用目标似乎显示为<sec:one>等。 - Sam Ritchie
此外,在 LaTeX 导出时,行号不会解析,除非我执行类似于 (setq org-export-latex-hyperref-format "\ref{%s}") 的操作。 - Sam Ritchie
1
奇怪——你使用的org-mode版本是哪个?我使用的是8.0.2,emacs 24。明天我会检查我的设置变量,但org 8文档表明这应该可以正常工作,无需特殊设置。 - Tom Regner
啊,我在使用7.9.4版本。我会升级一下然后再回来检查的。 - Sam Ritchie
在 org <8.0 版本中,你可以使用 # 符号注释掉 <<target>>,这样目标就不可见了,但你仍然可以链接到它。 - johntait.org
这是一个奇怪的解决方案:查看生成的latex,每个注释部分现在都有两个\labels{} - Clément

11

Tom Regner的方法可行,但您不必使用专用目标,仍然可以使用自定义ID链接,但无需描述。像这样:

Tom Regner的方法行得通,但您可以使用自定义ID链接而不必使用指定的目标,并且不需要添加任何描述。就像这样:

* Section One
:PROPERTIES:
:CUSTOM_ID: sec:one
:END:

* Section Two
You can reference Section One with [[#sec:one]] but NOT
[[#sec:one][Section One]], i.e., the link without description
will get you the actual section number (1).

8
您可以通过名称引用部分:
* Section One

* Section Two

* Links
  This is a number link: [[Section One]]
  This is a textual link: [[Section One][Some text for the link]]

这是LaTeX的输出结果:
\section{Section One}
\label{sec:orgheadline1}

\section{Section Two}
\label{sec:orgheadline2}

\section{Links}
\label{sec:orgheadline3}
This is a number link: \ref{sec:orgheadline1}
This is a textual link: \hyperref[sec:orgheadline1]{Some text for the link}

如果我编译输出的latex文件,一切都正常。如果我使用org导出来生成pdf文件,引用会显示成“???”。 - Saddle Point
1
已验证于2017年6月3日,此解决方案适用于Emacs 25.1.1和Org 9.0.6,并且非常容易。 - Reb.Cabin
在一个活的文本中,或者犯了一个错误,这样做的缺点是如果需要,你必须到处更改章节标题 -- 使用专用目标或自定义ID(如@York的答案所述)将链接与其标题分离,使更改变得更容易。 - Tom Regner

1

TL;DR:
使用包org-ref
使用内部链接标记章节,如<<sec:section-name>>
为图像、表格和源代码使用名称,如#+name: fig:my-fig
使用命令M-x org-ref-insert-ref-link插入内部链接

详情

如果您使用Org-Ref (此处有使用链接),我强烈推荐它用于技术写作,以及良好的引用实践,在其用户友好界面下管理内部引用(内部链接)。

使用内部链接格式<<link-name>>#+name: another-name格式创建标签。使用键序列M-x org-ref-insert-ref-link使用completing-read界面插入链接,这取决于您的设置,可能会增加Icicles、Ido、Helm、Company等功能。

例如,在以下情况下:

* Section with labels
<<sec:sec-w-labels>>

[...] My story here [...]

#+name: fig:my-screenshot
file:my-screenshot.png

* Section with links
I can refer to the section with labels (see \S <I>)
containing an explanatory figure here (see Fig. <I>)

当我的光标在标记为<I>的任一位置时,我使用一个命令M-x org-ref-insert-ref-link,该命令已绑定到C-c C-x )。然后,emacs会显示所有内部链接的完整候选列表,这种情况下,它们分别是sec:sec-w-labelsfig:my-screenshot
完成后,该部分应如下所示,具有ref:链接:
* Section with links
I can refer to the section with labels (see \S
ref:sec:sec-w-labels) containing an explanatory figure
here (see Fig. ref:fig:my-screenshot)

安装方法:将以下内容插入到你的Emacs初始化文件中,并重新启动。或者只是想尝试的话,将以下内容插入到你的*Scratch*缓冲区并执行C-M-x

;; Org Ref
(use-package org-ref
  :ensure t
  :after ob-http
  :hook (org-mode . ref-link-keymap)
  :config

  (defun ref-link-keymap ()
      (define-key org-mode-map (kbd "C-c C-x )")
        #'org-ref-insert-ref-link)))

限制

ref: 链接在所有导出器中都被导出为具有相同文本和 href 值的另一个链接。因此,在 HTML 导出的情况下,该链接将呈现为:

<a href="sec:bfm">sec:bfm</a>

对于Latex,编译器会将此内部链接翻译成章节编号。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接