【问题标题】:Emacs org: Promote all headings of a subtree during export?Emacs org:在导出期间提升子树的所有标题?
【发布时间】:2014-06-13 03:25:47
【问题描述】:

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

* Project 1
** Task 1                   :export:
*** Introduction
    Text text text. 
*** Results
    Text text text. 
* Project 2

将 emacs org 导出到 latex 会生成一个如下结构的 tex 文件:

\section{Project 1}
\subsection{Task 1}
\subsubsection{Introduction}
    Text text text. 
\subsubsection{Results}
    Text text text.

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

\section{Task 1}
\subsection{Introduction}
    Text text text. 
\subsection{Results}
    Text text text.

或者,甚至更好:

\title{Task 1}
\maketitle
\section{Introduction}
    Text text text. 
\section{Results}
    Text text text.

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

谢谢!

斯蒂芬

【问题讨论】:

  • 我会添加评论,但不幸的是我没有足够的声誉。这是一个非常好的教程,应该满足您的需求。 emacs org lisp tutorial
  • 此外,请尝试在此处发布您的问题:tex.stackexchange.com
  • @user3321294 请注意,此答案中提到的教程适用于早于 8.0 的org-mode 版本,并且org-mode“已转换为new export framework”。
  • 我无法重现您描述的行为。当我将您的示例输入文件导出到.tex 时,生成的文件包含Project 1 作为顶级\section:\section{Project 1}。您使用的是哪个版本的org-mode?我在版本 8.2.6 上。你做了什么来阻止org 在输出文件中包含Project 1
  • 您是对的——我更新了示例以纠正该错误。现在看起来像你得到的吗?我正在使用 org-mode 8.2.5。

标签: emacs org-mode


【解决方案1】:

您描述的第一个行为可以通过将以下内容添加到您的 .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)

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


来源:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多