怎样在 Org 导出到 LaTeX 时禁止自动生成 labels?

如果我在orgmode 中插入一个heading,比如: * Hello,world 时,C-c C-e l l 导出的 LaTeX 文件中会自动生成一个 label,如:

\section{Hello,world}
\label{sec:org42786f1}

我在配置中已经将org-latex-prefer-user-labels设置为:

(setq org-latex-prefer-user-labels t)

如何禁止自动生成 label, 因为我想自己指定 label,如:

* Hello,again
\label{sec:hello-again}

设置标题的 CUSTOM_ID , 快捷键 C-c C-x p

* Hello, again
  :PROPERTIES:
  :CUSTOM_ID: hello-again
  :END:

设置了 CUSTOM_ID 属性之后,还是不行

* Hello,world
:PROPERTIES:
:CUSTOM_ID: hello-world
:END:

+ hello,one
+ hello,two
+ hello,three

生成的 LaTeX 为:

\section{Hello,world}
\label{sec:orga88409c}
\begin{itemize}
\item hello,one
\item hello,two
\item hello,three
\end{itemize}

Orgmode 版本:

Org mode version 9.1.13 (9.1.13-elpaplus @ /Users/tilabs/.emacs.d/elpa/org-plus-contrib-20180528/)

Emacs 版本:

GNU Emacs 25.3.1 (x86_64-apple-darwin17.5.0, NS appkit-1561.40 Version 10.13.4 (Build 17E199)) of 2018-04-14

你检查下 org-latex-prefer-user-labelst 么?

我用的版本:

GNU Emacs 26.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.30) of 2018-05-29

Org mode version 9.1.13 (9.1.13-elpaplus @ /home/yyj/.emacs.d/elpa/org-plus-contrib-20180528/)

org-latex-prefer-user-labels 文档里说这样做是标准方法:

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.

......

For example, when this variable is non-nil, a headline like this:

  ** Some section
     :PROPERTIES:
     :CUSTOM_ID: sec:foo
     :END:
  This is section [[#sec:foo]].
  #+BEGIN_EXPORT latex
  And this is still section \ref{sec:foo}.
  #+END_EXPORT

will be exported to LaTeX as:

  \subsection{Some section}
  \label{sec:foo}
  This is section \ref{sec:foo}.
  And this is still section \ref{sec:foo}.

org-latex-prefer-user-labels 确定是 t, 我在vanilla emacs 下按你说的做的话,是working的。可能是我的配置有毛病,我在排查一下,谢谢!

这个配置是不是有问题啊?

(use-package org
  :ensure org-plus-contrib
  :config
  (progn
    (org-reload)

    ;; File extensions for org-mode major mode
    (add-to-list 'auto-mode-alist '("\\.\\(trello\\|org\\|txt\\)$" . org-mode))

    (setq ellipsis-rounded-down-arrow "⤵"
          ellipsis-lightening "⚡"
          ellipsis-down-triangle "▼"
          ellipsis-down-arrow "↴")

    (setq org-directory (expand-file-name (file-name-as-directory "~/org-notes"))
          org-special-ctrl-a/e t
          org-special-ctrl-k t
          org-time-clocksum-format "%d:%02d"
          org-insert-heading-respect-content t
          org-tags-column -80
          org-speed-commands t
          org-pretty-entities t
          org-confirm-babel-evaluate nil
          org-startup-indented t
          org-indent-mode t
          org-latex-create-formula-image-program 'imagemagick
          org-ellipsis ellipsis-down-arrow
          org-return-follows-link t
          org-startup-with-inline-images t
          org-log-into-drawer t
          org-log-done 'time
          org-edit-timestamp-down-means-later t
          org-agenda-start-on-weekday nil
          org-tags-match-list-sublevels nil
          org-html-validation-link nil ;; do not export html pages with "Validator" information

          ;; othere org variable settings
          org-export-backends '(ascii beamer html texinfo latex)
          org-export-babel-evaluate nil
          org-export-author-info t
          org-export-default-language "en"
          org-export-email-info t
          org-export-headline-levels 3
          org-export-highlight-first-table-line t
          org-export-html-expand t
          org-export-html-extension "html"
          org-html-doctype "html5"
          org-export-html-style-include-default nil
          org-export-htmlize-output-type 'css
          org-export-latex-listings-w-names nil
          org-export-preserve-breaks nil
          org-export-skip-text-before-1st-heading nil
          org-export-with-LaTeX-fragments t
          org-export-with-TeX-macros t
          org-export-with-archived-trees nil
          org-export-with-drawers '("LOGBOOK")
          org-export-with-emphasize t
          org-export-with-fixed-width t
          org-export-with-footnotes t
          org-export-with-priority t
          org-export-with-section-numbers t
          org-export-with-special-strings t
          org-export-with-sub-superscripts '{}
          org-export-with-tables t
          org-export-with-tags 'not-in-toc
          org-export-with-timestamps t
          org-export-with-toc t
          org-export-with-todo-keywords t
          org-hide-leading-stars t
          org-publish-list-skipped-files t
          org-publish-use-timestamps-flag t
          org-startup-align-all-tables t
          org-deadline-warning-days 365
          org-src-fontify-natively t
          org-html-footnote-format "[%s]" ;; change export hmtl markup for footnotes
          org-latex-create-formula-image-program 'imagemagick ;; using imagemagick to create image for LaTeX equations
          org-latex-default-class "simple-article"
          org-latex-caption-above nil ;; place caption under the table
          org-latex-tables-booktabs t
          org-latex-listings t
          org-latex-prefer-user-labels t)

    ;; Latex Export
    (setq org-latex-pdf-process
      '("xelatex -interaction nonstopmode -output-directory %o %f"
	"bibtex %b"
	"xelatex -interaction nonstopmode -output-directory %o %f"
	"xelatex -interaction nonstopmode -output-directory %o %f"))

    ;; or try
    ;; (setq org-latex-pdf-process (list "latexmk -shell-escape -bibtex -f -pdf %f"))

    ;; set a constant width for displaying inline images
    (setq org-image-actual-width '(300))

    ;; set default width for latex export from orgmode
    (setq org-latex-image-default-width ".8\\textwidth")

    ;; set default exports to both code and results
    (setq org-babel-default-header-args
          (cons '(:exports . "both")
                (assq-delete-all :exports org-babel-default-header-args)))

    ;; pdf export
    ;; Wrap source block within listings environment
    (setq org-latex-listings t)

    ;; properly display _ underscore in orgmode
    (setq-default org-use-sub-superscripts nil)

    ;; Lay caption below table
    (setq org-latex-caption-above nil)
    (setq org-latex-tables-booktabs t)

    ;; Table border
    (setq org-export-latex-tables-column-borders nil)

    ;; Split TOC in a single page for easy viewing
    ;;(setq org-latex-toc-command "\\begin{center}\\rule{\\textwidth}{0.5pt}\\end{center}\\tableofcontents\\clearpage")

    ;; Diabsble exporting stuff
    ;; + drawer
    ;; + tags
    ;; + todo keywords
    ;; + timestapes
    ;; + hyperref
    (setq org-export-with-drawers 'nil)
    (setq org-export-with-tags 'nil)
    (setq org-export-with-todo-keywords nil)
    (setq org-export-with-timestamps nil)

    ;; hyperref will be used and set in individual document or in corresponding cls/sty files
    (setq org-latex-with-hyperref nil)

    (setq org-latex-listings-options
          '(("frame" "none")
            ("basicstyle" "\\tiny\\codefont")
            ("numbers" "left")
            ("breaklines" "true")
            ("showspaces" "false")
            ("showstringspaces" "false")
            ("showtabs" "false")
            ("numberstyle" "\\tiny")
            ("literate" "{-}{-}1")
            ("identifierstyle" "\\color{codegreen}")
            ("keywordstyle" "\\tiny\\color{codepurple}\\texttt ")
            ("commentstyle" "\\color{codegray}\\textit")
            ("rulecolor" "\\color{codegray}")
            ("frame" "L")
            ))

    (setq org-latex-default-class "article")

    ;; peview LaTeX in orgmode
    (setq org-latex-create-formula-image-program 'imagemagick
          org-startup-with-inline-images 'inlineimages
          org-latex-to-pdf-process (list "latexmk -pdf %f")
          org-startup-indented (quote indent))

    ;; org-babel
    (org-babel-do-load-languages
     'org-babel-load-languages
     '(
       (awk . t)
       (calc . t)
       (C . t)
       (emacs-lisp . t)
       (haskell . t)
       (gnuplot . t)
       (latex . t)
       (ledger . t)
       (js . t)
       (perl . t)
       (python . t)
       (ipython . t)
       (clojure . t)
       (ditaa .t)
       (gnuplot . t)
       (R . t)
       (scheme . t)
       (shell . t)
       (sql . t)
       (plantuml . t)
       (http . t)
       ))

    ;; Plantuml & ditta
    (setq org-plantuml-jar-path
          (expand-file-name "~/.emacs.d/misc/plantuml.jar"))
    (setq org-ditaa-jar-path "~/.emacs.d/misc/ditaa.jar")

    ;; Todo key words
    (setq org-todo-keywords
          '((sequence "TODO(t!)" "PROJECT(r)" "STARTED(s!)"
                      "WAITING(w!)" "CHARGED(c!)" "|" "PAID(p!)" "DONE(d!)")))

    ;; Keywords faces
    (setq org-todo-keyword-faces
          '(("PROJECT" . "#4da6ff")
            ("STARTED" . "#ffff99")
            ("WAITING" . "#ffcc00")
            ("CHARGED" .  "#e085e0")))

    ;; Org-mode hooks
    (add-hook 'org-mode-hook (lambda()
                               (setq truncate-lines t)
                               (auto-fill-mode 1)
                               (visual-line-mode)
                               (org-indent-mode)
                               (company-mode 1)
                               (nolinum)
                               (sangjee/turn-strings-into-utf8-chars)
                               (local-set-key (kbd "C-c i s") 'sangjee/org-insert-src-block) ;; insert src_block
                               ))

    ;; org latex classes for exporting to pdf
    ;; 1 - article
    ;; 2 - note
    ;; 3 - cv
    ;; 3 - letter
    (require 'ox-publish)
    (load "init-org-exports")
    ))

;; https://github.com/jbranso/.emacs.d/blob/4ad15c18972955bc3c7fdf01cca761dd926c507c/lisp/init-org.org
(use-package writegood-mode :ensure t :defer t)

;; Enable making http request using http src block
(use-package ob-http :ensure t)

;; Tufte backend for orgmode
(use-package ox-tufte
  :ensure t)

;; Orgbullets make orgmode file more attractive
(setq bullet-circles         '("○" "☉" "◎" "◉" "○" "◌" "◎" "●" "◦" "◯" "⚪" "⚫" "⚬" "❍" "○" "⊙" "⊚" "⊛" "∙" "∘")
      bullet-special-circles '("◐" "◑" "◒" "◓" "◴" "◵" "◶" "◷" "⚆" "⚇" "⚈" "⚉" "♁" "⊖" "⊗" "⊘")
      bullet-special-symbols '("☀" "♼" "☼" "☾" "☽" "☣" "§" "¶" "‡" "※" "✕" "△" "◇" "▶" "◀" "◈"))

(use-package org-bullets
  :ensure t
  :config
  (progn
    (add-hook 'org-mode-hook (lambda ()
                               (org-bullets-mode 1)
                               (setq  org-bullets-bullet-list bullet-circles)))))

;; Change postamble from "created" to "Last Update" when exporting orgmode to html
(defun sangjee/org-html-postamble (plist)
  (format "Last update : %s" (format-time-string "%d %b %Y")))
(setq org-html-postamble 'sangjee/org-html-postamble)

;; Add custom css to html exported from org
(defun sangjee/org-inline-css-hook (exporter)
  "Insert custom inline css"
  (when (eq exporter 'html)
    (let* ((dir (ignore-errors (file-name-directory (buffer-file-name))))
           (path (concat dir "style.css"))
           (homestyle (or (null dir) (null (file-exists-p path))))
           (final (if homestyle "~/.emacs.d/misc/org-style/css/org.css" path)))
      (setq org-html-head-include-default-style nil)
      (setq org-html-head (concat
                           "<style type=\"text/css\">\n"
                           "<!--/*--><![CDATA[/*><!--*/\n"
                           (with-temp-buffer
                             (insert-file-contents final)
                             (buffer-string))
                           "/*]]>*/-->\n"
                           "</style>\n")))))

(add-hook 'org-export-before-processing-hook 'sangjee/org-inline-css-hook)

;; ob-ipython
(use-package ob-ipython
  :ensure t)

;; interleave makes emacs to view output more easily
(use-package interleave
  :ensure t)

(provide 'setup-org)

我自己不用 use-package,所以不太熟悉,没看出问题在哪

检查一下你的 elpa 文件夹里是否有多个版本的 org,如果有的话删掉一个,比如 org-20180528xxxx,只留下 org-plus-contrib。可以先看一下命令 org-version 的输出,看看有没有什么问题。