如何将对 #begin_src 块的引用导出到 LaTeX

How to export a reference to a #begin_src block to LaTeX

我正在将 org-latex-listings 设置为 t 的 ORG 文件导出到 LaTeX。该文件包含一些源列表,我想插入一些对它们的引用。

在生成的 TEX 文件中,\ref 中包含的引用名称似乎是由 org-mode 翻译的,但 \lstsetlabel 选项保持原样。这样,pdflatex 找不到正确的引用并给出消息 LaTeX Warning: There were undefined references.

这是一个 MWE 的例子:

#+latex_header: \usepackage{listings}
First simple java program in listing [[thesourcelisting]]
#+name: thesourcelisting
#+caption: Simple java program
#+BEGIN_SRC java
public static void main(String[] args){
  System.exit(0);
}
#+END_SRC

当导出为 LaTeX 文件时(C-c C-e l t),我得到了

% Created 2016-10-18 mar 14:50
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{listings}

\author{alvaro}
\date{\today}
\title{}
\hypersetup{
 pdfauthor={alvaro},
 pdftitle={},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 25.1.2 (Org mode 8.3.6)}, 
 pdflang={English}}
\begin{document}

\tableofcontents

First simple java program in listing \ref{orgsrcblock1}
\lstset{language=java,label=thesourcelisting,caption={Simple java program},captionpos=b,numbers=none}
\begin{lstlisting}
public static void main(String[] args){
  System.exit(0);
}
\end{lstlisting}
\end{document}

原名thesourcelisting\ref命令中被翻译成orgsrcblock1,而在\lstset命令中则没有。

我正在使用 GNU Emacs 25.1.2。

尝试将 org-latex-prefer-user-labels 设置为 t

变量的文档字符串部分说明(执行 C-h v org-latex-prefer-user-labels RET 以获得全部内容):

Use user-provided labels instead of internal ones when non-nil.

When this variable is non-nil, Org will use the value of CUSTOM_ID property, NAME keyword or Org target as the key for the \label commands generated.

By default, Org generates its own internal labels during LaTeX export. This process ensures that the \label keys are unique and valid, but it means the keys are not available in advance of the export process.