Emacs org:在导出过程中提升子树的所有标题?

use*_*294 5 emacs org-mode

我经常使用 emacs org 将部分 org 文档导出到 Latex/pdf。我想知道是否有办法在导出过程中提升所选部分的所有标题。例如,假设该文件如下所示:

* Project 1
** Task 1                   :export:
*** Introduction
    Text text text. 
*** Results
    Text text text. 
* Project 2
Run Code Online (Sandbox Code Playgroud)

emacs org 导出到 Latex 将生成以下结构的 tex 文件:

\section{Project 1}
\subsection{Task 1}
\subsubsection{Introduction}
    Text text text. 
\subsubsection{Results}
    Text text text.
Run Code Online (Sandbox Code Playgroud)

但由于要导出的部分没有最高级别,因此采用以下结构更有意义:

\section{Task 1}
\subsection{Introduction}
    Text text text. 
\subsection{Results}
    Text text text.
Run Code Online (Sandbox Code Playgroud)

或者,甚至更好:

\title{Task 1}
\maketitle
\section{Introduction}
    Text text text. 
\section{Results}
    Text text text.
Run Code Online (Sandbox Code Playgroud)

我想知道是否有人知道如何解决这个问题?不幸的是,我的 lisp 技能非常初级,看起来应该不会太难。

谢谢!

斯蒂芬

its*_*eyd 5

您描述的第一个行为可以通过将以下内容添加到您的 来实现.emacs

;; Define a function for turning a single subtree into a top-level tree
;; (:export: headings might be located at an arbitrary nesting level,
;; so a single call to "org-promote-subtree" is not enough):
(defun org-promote-to-top-level ()
  "Promote a single subtree to top-level."
  (let ((cur-level (org-current-level)))
    (loop repeat (/ (- cur-level 1) (org-level-increment))
          do (org-promote-subtree))))

;; Define a function that applies "org-promote-to-top-level" 
;; to each :export: subtree:
(defun org-export-trees-to-top-level (backend)
  "Promote all subtrees tagged :export: to top-level.
BACKEND is the export back-end being used, as a symbol."
  (org-map-entries 'org-promote-to-top-level "+export"))

;; Make org-mode run "org-export-subtrees-to-top-level" as part of the export
;; process:
(add-hook 'org-export-before-parsing-hook 'org-export-trees-to-top-level)
Run Code Online (Sandbox Code Playgroud)

实现第二个行为有点棘手,但org-export-trees-to-top-level如果您最终需要的话,您可以使用该函数作为起点。然而,我想指出,这对于具有多个子树的文件不起作用:export:(除非您还想出一种方法来决定\title在这些情况下将成为哪个标题)。


资料来源: