I'm exporting an ORG file to LaTeX with org-latex-listings
set to t
. The file contains some source listings, and I want to insert some references to them.
In the generated TEX file, the reference names included in \ref
seems to be translated by org-mode, but the label
option of \lstset
is keept as is. This way, pdflatex
can't find the correct reference and gives the message LaTeX Warning: There were undefined references.
Here is an example of a 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
When exported as a LaTeX file (C-c
C-e
l
t
), I got
% 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}
The original name thesourcelisting
is translated to orgsrcblock1
in the \ref
command, but not in the \lstset
command.
I'm using GNU Emacs 25.1.2.
Try setting org-latex-prefer-user-labels
to t
.
The doc string of the variable says in part (do C-h v org-latex-prefer-user-labels RET
to get the whole thing):
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.