latexorg-modeorg-babel

Multiline latex header from src block


I have several lines that should be added before \begin{document}. Usually, I do that with #+LATEX_HEADER:. However, #+LATEX_HEADER: is not able to evaluate references (e.g., noweb) nor org macros. The only solution I can see is to add #+LATEX_HEADER: before each line. Is there a more elegant way to do that?

My "ideal" org file (not working):

#+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 working solution:

#+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: }

Solution

  • This works for me (org mode version 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
    

    The exported LaTeX file is (with my config)

    % 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}