Improve LaTeX export settings

* Rename latex-preamble source block to latex-header.
* Add latex-extra-header source block.
* Improve the relevant documentation.
This commit is contained in:
Gerard Vermeulen 2021-12-28 08:32:51 +01:00
parent e6be1123a2
commit 9f51a5c599

View File

@ -1,22 +1,33 @@
#+startup: content
#+title: Emacs setup for use with LaTeX, Org, and Python
#+author: Gerard Vermeulen
#+babel: :cache no
#+property: header-args :tangle init.el :comments link
#+property: header-args:emacs-lisp :exports code :results silent
#+property: header-args:latex-preamble :tangle no
#
#+latex_class: article
#+latex_class_options: [11pt,a4paper,english,svgnames]
#+latex_class_options: [11pt,a4paper,english,svgnames,tables]
#
#+babel: :cache no
#
#+macro: kbd @@html:<kbd>$1</kbd>@@@@latex:\colorbox{PaleTurquoise}{\texttt{$1}}@@
#
#+property: header-args :tangle init.el
#+property: header-args:emacs-lisp :exports code :results silent
#+property: header-args:latex-header :tangle no
#+property: header-args:latex-extra-header :tangle no
#
#+startup: content
* LaTeX preamble :noexport:
* LaTeX headers :noexport:
#+begin_src latex-preamble
% Use babel for biblatex.
#+begin_src latex-header
% LANGUAGE:
\usepackage{babel}
\usepackage{fvextra}
\usepackage{csquotes}
% LISTS:
\usepackage{enumitem}
\setlist{noitemsep}
% TECHNICS:
\usepackage{siunitx}
\usepackage{tikz}
% LISTINGS:
% Section 2.6 of caption-eng.pdf (texdoc caption) explains that the sign
% of "skip" depends on the assumption "position=above" or "position=below".
% The assumption should match the real caption position in the LaTeX code.
@ -24,10 +35,20 @@
\usepackage[newfloat]{minted}
\captionsetup[listing]{position=below,skip=0em}
\usemintedstyle{xcode}
% Use lastpage in fancy header.
% TABLES:
% https://tex.stackexchange.com/questions/341205/
% what-is-the-difference-between-tabular-tabular-and-tabularx-environments
% https://emacs.stackexchange.com/questions/26179/
% change-org-mode-table-style-just-for-latex-export
% https://tex.stackexchange.com/questions/468585/
% table-formatting-using-siunitx
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{tabularx} % DANGER: beware of Org table :width and :align options!
% FANCY HEADERS:
\usepackage{fancyhdr}
\usepackage{lastpage}
% Use geometry without warnings.
% PAGE GEOMETRY:
\usepackage[
headheight=20mm,
top=40mm,
@ -39,7 +60,7 @@
]{geometry}
#+end_src
#+begin_src latex-preamble
#+begin_src latex-header
% Make section, subsection and subsubsection implicit float barriers.
% https://tex.stackexchange.com/questions/118662/use-placeins-for-subsections
% Make section an implicit float barrier:
@ -62,7 +83,7 @@
\makeatother
#+end_src
#+begin_src latex-preamble
#+begin_src latex-header
% Add fancy headers and footers to normal pages.
\pagestyle{fancy}
\fancyhf{}
@ -1187,38 +1208,61 @@ this document to =html= and LaTeX correctly.
'(apply 'my-org-eval-blocks-named '("python-setup")))
#+end_src
*** [[info:org#Advanced Export Configuration][Advanced Export Configuration (info)]]
*** [[info:org#LaTeX header and sectioning][Easy LaTeX preamble editing]]
:PROPERTIES:
:CUSTOM_ID: sec:org-document-export-hacks
:CUSTOM_ID: sec:easy-latex-preamble-editing
:END:
Stolen from [[https://git.sr.ht/~bzg/org-contrib/tree/master/item/lisp/ox-extra.el][ox-extra.el]]
There are at least two ways (new and old) to edit the LateX preamble
=latex_header= and =latex-extra_header= export options easily in LaTeX source or
export blocks. This [[info:org#Top][Org (info)]] file uses the new way, but keeps the old way for
backwards compatibility.
#+name: lst:emacs-lisp-setup-latex-preamble
The new way -- exploiting an idea of [[https://www.matem.unam.mx/~omar/][Omar Antolin Camarena]] -- is to code new
[[info:org#Editing Source Code][<LANGUAGE>-modes]] allowing to edit in LaTeX mode and to export to LaTeX code with
[[info:org#LaTeX specific export settings][correct LaTeX preamble export setting prefixes]]. Here, are links to three posts
exposing his idea:
1. [[https://www.reddit.com/r/orgmode/comments/7u2n0h/tip_for_defining_latex_macros_for_use_in_both/][Export LaTeX macros to LaTeX and HTML/MathJax preambles (reddit)]],
2. [[https://www.reddit.com/r/orgmode/comments/5bi6ku/tip_for_exporting_javascript_source_block_to/][Export JavaScript source blocks to script tags in HTML (reddit)]],
3. [[https://emacs.stackexchange.com/questions/28301/export-javascript-source-block-to-script-tag-in-html-when-exporting-org-file-to][Export JavaScript source blocks to script tags in HTML (SX)]].
Listing [[lst:emacs-lisp-setup-latex-header]] implements this way by means of two
new [[info:org#Editing Source Code][<LANGUAGE>-modes]]: =latex-header= and =latex-extra-header=.
#+caption[New =<LANGUAGE>-modes= to edit the LaTeX preamble easily]:
#+caption: Add =latex-header= and =latex-extra-header= language modes to edit
#+caption: LaTeX preamble =latex_header= and =latex_extra_header= export options
#+caption: easily.
#+name: lst:emacs-lisp-setup-latex-header
#+begin_src emacs-lisp
(with-eval-after-load 'org-src
(add-to-list 'org-src-lang-modes '("latex-preamble" . latex))
(with-eval-after-load 'org-src (defun prefix-all-lines (prefix body)
(with-temp-buffer (insert body) (string-insert-rectangle (point-min)
(point-max) prefix) (buffer-string)))
(defvar org-babel-default-header-args:latex-preamble
'((:results . "raw")
(:exports . "results")))
(add-to-list 'org-src-lang-modes '("latex-header" . latex))
(defun prefix-all-lines (prefix body)
(with-temp-buffer
(insert body)
(string-insert-rectangle (point-min) (point-max) prefix)
(buffer-string)))
(defvar org-babel-default-header-args:latex-header '((:results
. "raw") (:exports . "results")))
(defun org-babel-execute:latex-preamble (body _params)
(prefix-all-lines "#+latex_header: " body)))
(defun org-babel-execute:latex-header (body _params)
(prefix-all-lines "#+latex_header: " body))
(add-to-list 'org-src-lang-modes '("latex-extra-header" . latex))
(defvar org-babel-default-header-args:latex-extra-header '((:results
. "raw") (:exports . "results")))
(defun org-babel-execute:latex-extra-header (body _params)
(prefix-all-lines "#+latex_extra_header: " body)))
#+end_src
[[https://www.reddit.com/r/orgmode/comments/7u2n0h/tip_for_defining_latex_macros_for_use_in_both/]]
[[https://www.reddit.com/r/orgmode/comments/5bi6ku/tip_for_exporting_javascript_source_block_to/]]
The old way is to use a special export attribute as in the function
=org-latex-header-blocks-filter= in [[https://git.sr.ht/~bzg/org-contrib/tree/master/item/lisp/ox-extra.el][ox-extra.el]]. Apparently, nobody is using
this broken function (broken, since it relies on support only in org-mode before
=2014-11-11=). Listing [[lst:org-latex-header-blocks-filter]] proposes a fix.
#+caption[Convert marked LaTeX export blocks to LaTeX header lines]:
#+caption: Convert marked LaTeX export blocks to LaTeX header lines.
#+label: lst:org-latex-header-blocks-filter
#+name: lst:org-latex-header-blocks-filter
#+begin_src emacs-lisp
(with-eval-after-load 'ox
(defun org-latex-header-blocks-filter (backend)