来自 src 块的多行乳胶 header

Multiline latex header from src block

我有几行应该添加到 \begin{document} 之前。通常,我用 #+LATEX_HEADER: 来做到这一点。但是,#+LATEX_HEADER: 无法评估引用(例如 noweb)或组织宏。我能看到的唯一解决方案是在每行之前添加 #+LATEX_HEADER: 。有更优雅的方法吗?

我的“理想”组织文件(不工作):

#+TITLE: Title
#+AUTHOR: Someone

#+LATEX_CLASS: article

#+LATEX_HEADER: <<headerthings>>

* First Section
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. 

* header things                                                    :noexport:
#+name: headerthings
#+begin_src latex
\somemacro{
Morbi rutrum eros 
luctus. Maecenas n
nunc nec vulputate
diam in urna. Susp
gravida nisl a lor
ullamcorper sodalet
per conubia nostra
eros. In mollis el
convallis laoreet.
efficitur aliquet 
}
#+end_src

Non-elegant 工作解决方案:

#+TITLE: Title
#+AUTHOR: Someone

#+LATEX_CLASS: article

#+LATEX_HEADER: <<headerthings>>

* First Section
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. 

* header things                                                    :noexport:
#+LATEX_HEADER: \somemacro{
#+LATEX_HEADER: Morbi rutrum eros 
#+LATEX_HEADER: luctus. Maecenas n
#+LATEX_HEADER: nunc nec vulputate
#+LATEX_HEADER: diam in urna. Susp
#+LATEX_HEADER: gravida nisl a lor
#+LATEX_HEADER: ullamcorper sodalet
#+LATEX_HEADER: per conubia nostra
#+LATEX_HEADER: eros. In mollis el
#+LATEX_HEADER: convallis laoreet.
#+LATEX_HEADER: efficitur aliquet 
#+LATEX_HEADER: }

这对我有用(组织模式版本 9.1.2)

#+TITLE: Title
#+CALL: my_latex_header()

* Your section 1
* Your section 2 
* Configuration                                                    :noexport:
#+NAME: my_latex_header
#+BEGIN_SRC emacs-lisp :results drawer 
(mapconcat (function (lambda(x) (format "#+LATEX_HEADER: %s" x)))
       '( 
         "% line_1"
         "% line_2"
         "% line_3"
         )
       "\n")
#+END_SRC

导出的 LaTeX 文件是(用我的配置)

% Created 2020-07-02 Thu 06:17
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
% line_1
% line_2
% line_3
\author{picaud}
\date{\today}
\title{Title}
\hypersetup{
 pdfauthor={picaud},
 pdftitle={Title},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.1 (Org mode 9.1.2)}, 
 pdflang={English}}
\begin{document}

\maketitle
\tableofcontents
    
\section{Your section 1}
\label{sec:orgcfaa171}
\section{Your section 2}
\label{sec:orgead0a5c}
\end{document}

您可以tangle源代码块,然后将生成的文件作为一个包使用:

#+TITLE: Title
#+AUTHOR: Someone

#+LATEX_CLASS: article

#+LATEX_HEADER: \usepackage{headerthings}

* First Section
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. 

\somemacro

* header things                                                    :noexport:
#+name: headerthings
#+begin_src latex :tangle headerthings.sty
\newcommand{\somemacro}{
Morbi rutrum eros 
luctus. Maecenas n
nunc nec vulputate
diam in urna. Susp
gravida nisl a lor
ullamcorper sodalet
per conubia nostra
eros. In mollis el
convallis laoreet.
efficitur aliquet 
}
#+end_src

导出前需要C-c C-v C-t纠结,但只有在更改源块时才需要纠结。或者您可以通过修改 org-export-before-process-hook:

来安排在导出之前发生缠结
(add-hook 'org-export-before-processing-hook #'org-babel-tangle)

这样你就不用担心了。