Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: }
like image 822
GP. Avatar asked Oct 25 '25 02:10

GP.


2 Answers

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}
like image 63
Picaud Vincent Avatar answered Oct 26 '25 21:10

Picaud Vincent


You can tangle the source block and then use the resulting file as a package:

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

You will need to do C-c C-v C-t to tangle before you can export, but you only need to tangle when you change the source block. Or you can arrange for the tangling to happen before export by modifying the org-export-before-process-hook:

(add-hook 'org-export-before-processing-hook #'org-babel-tangle)

so that you don't have to worry about it.

like image 29
NickD Avatar answered Oct 26 '25 21:10

NickD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!