Handle the latex preamble

* Add a fake language mode called latex-preamble.
This commit is contained in:
Gerard Vermeulen 2021-12-26 11:53:40 +01:00
parent 1963d9d2c1
commit e6be1123a2
2 changed files with 157 additions and 143 deletions

View File

@ -2,61 +2,102 @@
#+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,svgnames]
#+latex_header: \usepackage[AUTO]{babel}
#+latex_header: \usepackage{fvextra}
#+latex_header: \usepackage{csquotes}
#
#+latex_header: \usepackage{caption}
#+latex_header: \usepackage[newfloat]{minted}
#+latex_header: \captionsetup[listing]{position=below,skip=0em}
#+latex_header: \usemintedstyle{xcode}
#
#+latex_header: \hypersetup{
#+latex_header: citecolor=blue,
#+latex_header: colorlinks=true,
#+latex_header: filecolor=blue,
#+latex_header: hyperfootnotes=false,
#+latex_header: linkcolor=blue,
#+latex_header: unicode=true,
#+latex_header: urlcolor=blue
#+latex_header: }
#
#+latex_header: \usepackage{fancyhdr}
#+latex_header: \usepackage{lastpage}
#
#+latex_header: \usepackage[
#+latex_header: headheight=20mm,
#+latex_header: top=40mm,
#+latex_header: bottom=20mm,
#+latex_header: left=60pt,
#+latex_header: right=60pt,
#+latex_header: heightrounded,
#+latex_header: verbose,
#+latex_header: ]{geometry}
#
#+latex_header: % Make section an implicit float barrier:
#+latex_header: \usepackage[section]{placeins}
#+latex_header: % Make subsection an implicit float barrier:
#+latex_header: \makeatletter
#+latex_header: \AtBeginDocument{%
#+latex_header: \expandafter\renewcommand\expandafter\subsection\expandafter{%
#+latex_header: \expandafter\@fb@secFB\subsection
#+latex_header: }%
#+latex_header: }
#+latex_header: \makeatother
#+latex_header: % Make subsubsection an implicit float barrier:
#+latex_header: \makeatletter
#+latex_header: \AtBeginDocument{%
#+latex_header: \expandafter\renewcommand\expandafter\subsubsection\expandafter{%
#+latex_header: \expandafter\@fb@secFB\subsubsection
#+latex_header: }%
#+latex_header: }
#+latex_header: \makeatother
#+latex_class_options: [11pt,a4paper,english,svgnames]
* LaTeX preamble :noexport:
#+begin_src latex-preamble
% Use babel for biblatex.
\usepackage{babel}
\usepackage{fvextra}
\usepackage{csquotes}
% 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.
\usepackage{caption}
\usepackage[newfloat]{minted}
\captionsetup[listing]{position=below,skip=0em}
\usemintedstyle{xcode}
% Use lastpage in fancy header.
\usepackage{fancyhdr}
\usepackage{lastpage}
% Use geometry without warnings.
\usepackage[
headheight=20mm,
top=40mm,
bottom=20mm,
left=60pt,
right=60pt,
heightrounded,
verbose,
]{geometry}
#+end_src
#+begin_src latex-preamble
% Make section, subsection and subsubsection implicit float barriers.
% https://tex.stackexchange.com/questions/118662/use-placeins-for-subsections
% Make section an implicit float barrier:
\usepackage[section]{placeins}
% Make subsection an implicit float barrier:
\makeatletter
\AtBeginDocument{%
\expandafter\renewcommand\expandafter\subsection\expandafter{%
\expandafter\@fb@secFB\subsection
}%
}
\makeatother
% Make subsubsection an implicit float barrier:
\makeatletter
\AtBeginDocument{%
\expandafter\renewcommand\expandafter\subsubsection\expandafter{%
\expandafter\@fb@secFB\subsubsection
}%
}
\makeatother
#+end_src
#+begin_src latex-preamble
% Add fancy headers and footers to normal pages.
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\footrulewidth}{0.4pt}
\fancyfoot[C]{\emph{
Emacs setup for use with \LaTeX{}, Org, and Python -- Gerard Vermeulen}}
\renewcommand{\headrulewidth}{0.4pt}
\fancyhead[L]{\includegraphics[height=1.8cm]{Org-mode-unicorn.png}}
\fancyhead[C]{
Page: \thepage/\pageref{LastPage} \\
\text{ } \\
\text{ } \\
DRAFT
}
\fancyhead[R]{\includegraphics[height=1.8cm]{Emacs-logo.png}}
% Add fancy header and footer to custom titlepage.
% https://tex.stackexchange.com/questions/506102/
% adding-header-and-footer-to-custom-titlepage
\fancypagestyle{titlepage}{%
\fancyhf{}
\renewcommand{\footrulewidth}{0.4pt}
\fancyfoot[C]{\emph{
Emacs setup for use with \LaTeX{}, Org, and Python -- Gerard Vermeulen}}
\renewcommand{\headrulewidth}{0.4pt}
\fancyhead[L]{\includegraphics[height=1.8cm]{Org-mode-unicorn.png}}
\fancyhead[C]{
\pageref{LastPage} pages \\
\text{ } \\
\text{ } \\
DRAFT
}
\fancyhead[R]{\includegraphics[height=1.8cm]{Emacs-logo.png}}
}
#+end_src
* Quick start
:PROPERTIES:
@ -738,31 +779,38 @@ rebuild the =epdfinfo= executable that serves the [[https://en.wikipedia.org/wik
:END:
Loading =tex.el= immediately instead of lazily ensures proper initialization of
the [[https://en.wikipedia.org/wiki/AUCTeX][AUCTeX]]. For instance, the ~TeX-master~ safe local variable in the =tex.el=
[[https://en.wikipedia.org/wiki/AUCTeX][AUCTeX]]. For instance, the ~TeX-master~ safe local variable in the =tex.el=
elisp library file has no autoload cookie. Without prior loading of =tex.el=,
Emacs will complain that ~TeX-master~ is no safe local variable in case it
reads a LaTeX file that sets ~TeX-master~.
Emacs will complain that ~TeX-master~ is no safe local variable in case it reads
a LaTeX file that sets ~TeX-master~. Listing [[lst:require-auctex]] initializes
[[https://en.wikipedia.org/wiki/AUCTeX][AUCTeX]] properly.
#+caption[Require =AUCTeX=]:
#+caption: Require =AUCTeX=.
#+label: lst:require-auctex
#+begin_src emacs-lisp
;; Try to get the `safe-local-variable' predicate for `TeX-master'
;; Use `require' to make `TeX-master' a safe local variable.
(when (require 'tex nil 'noerror)
(custom-set-variables
'(TeX-auto-save t)
'(TeX-install-font-lock #'font-latex-setup)
'(TeX-parse-self t)))
#+end_src
Although, the LaTeX =biblatex= is in use, listing [[lst:configure-bibtex]]
configures the Emacs =bibtex= library for the LaTeX =BiBTeX= package to maintain
backwards compatibility.
#+caption[Configure =bibtex=]:
#+caption: Configure =bibtex=.
#+label: lst:configure-bibtex
#+begin_src emacs-lisp
(with-eval-after-load 'bibtex
(custom-set-variables
'(bibtex-dialect 'BibTeX)))
(custom-set-variables '(bibtex-dialect 'BibTeX)))
#+end_src
Listing [[lst:configure-font-latex]] disables font scaling of section titles.
#+caption[Configure =font-latex=]:
#+caption: Configure =font-latex=.
#+label: lst:configure-font-latex
@ -772,6 +820,9 @@ reads a LaTeX file that sets ~TeX-master~.
'(font-latex-fontify-sectioning 1.0)))
#+end_src
Listing [[lst:configure-latex]] configures =latex= for a full featured
=LaTeX-section-command=.
#+caption[Configure =latex=]:
#+caption: Configure =latex=.
#+label: lst:configure-latex
@ -922,7 +973,21 @@ The code in listing [[lst:customize-org-babel]], [[lst:customize-org]], and
(with-eval-after-load 'ox-latex
(custom-set-variables
'(org-latex-compiler "lualatex")
'(org-latex-hyperref-template nil)
'(org-latex-hyperref-template "\\hypersetup{
pdfauthor={%a},
pdftitle={%t},
pdfkeywords={%k},
pdfsubject={%d},
pdfcreator={%c},
pdflang={%L},
citecolor=blue,
colorlinks=true,
filecolor=blue,
hyperfootnotes=false,
linkcolor=blue,
unicode=true,
urlcolor=blue,
}\n")
'(org-latex-listings 'minted)
'(org-latex-minted-options '(("bgcolor" "LightGoldenrodYellow")))
'(org-latex-logfiles-extensions '("blg" "lof" "log" "lot" "out" "toc"))
@ -1053,11 +1118,14 @@ this document to =html= and LaTeX correctly.
(format "@ref{%s,%s,,%s,}" node title manual)))
(_ nil))))
(make-variable-buffer-local 'org-info-emacs-documents)
(setq org-info-emacs-documents (delete "org" org-info-emacs-documents))
(make-variable-buffer-local 'org-info-other-documents)
(setq org-info-other-documents
(cl-union '(("consult" . "https://github.com/minad/consult")
("embark" . "https://github.com/oantolin/embark")
("marginalia" . "https://github.com/minad/marginalia")
("org" . "https://orgmode.org/org.html")
("orderless" . "https://github.com/oantolin/orderless")
("vertico" . "https://github.com/minad/vertico"))
org-info-other-documents
@ -1104,9 +1172,10 @@ this document to =html= and LaTeX correctly.
(org-element-map
(org-element-parse-buffer 'greater-element nil) 'src-block
(lambda (block)
(message "%s" (org-element-property :name block))
(when (string-match-p infix (org-element-property :name block))
block)))))
(let ((name (org-element-property :name block)))
(message "%s" name)
(when (and name (string-match-p infix name))
block))))))
(dolist (block blocks)
(goto-char (org-element-property :begin block))
(org-babel-execute-src-block)))))
@ -1125,6 +1194,28 @@ this document to =html= and LaTeX correctly.
Stolen from [[https://git.sr.ht/~bzg/org-contrib/tree/master/item/lisp/ox-extra.el][ox-extra.el]]
#+name: lst:emacs-lisp-setup-latex-preamble
#+begin_src emacs-lisp
(with-eval-after-load 'org-src
(add-to-list 'org-src-lang-modes '("latex-preamble" . latex))
(defvar org-babel-default-header-args:latex-preamble
'((:results . "raw")
(:exports . "results")))
(defun prefix-all-lines (prefix body)
(with-temp-buffer
(insert body)
(string-insert-rectangle (point-min) (point-max) prefix)
(buffer-string)))
(defun org-babel-execute:latex-preamble (body _params)
(prefix-all-lines "#+latex_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/]]
#+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
@ -1173,46 +1264,6 @@ Stolen from [[https://git.sr.ht/~bzg/org-contrib/tree/master/item/lisp/ox-extra.
[[https://emacs.stackexchange.com/questions/47347/customizing-org-latex-title-command-to-edit-title-page][How to customize =org-latex-title-command= only in this buffer]]
#+caption[Hack LaTeX export buffer local variables]:
#+caption: Hack LaTeX export buffer local variables.
#+header: :var title-page=title-page
#+name: lst:emacs-lisp-setup-hack
#+begin_src emacs-lisp :exports code :results silent :tangle no
(with-eval-after-load 'ox
(make-variable-buffer-local 'org-export-before-parsing-hook)
(cl-pushnew #'org-latex-header-blocks-filter
org-export-before-parsing-hook))
(when (require 'ox-latex nil 'noerror)
(make-variable-buffer-local 'org-latex-classes)
(cl-pushnew '("article-local"
"\\documentclass[11pt]{article}
[NO-DEFAULT-PACKAGES]
[PACKAGES]
[EXTRA]"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
org-latex-classes :key #'car :test #'equal)
(make-variable-buffer-local 'org-latex-title-command)
(setq org-latex-title-command (concat title-page))
(make-variable-buffer-local 'org-latex-toc-command)
(setq org-latex-toc-command
(mapconcat 'identity '(""
"\\tableofcontents\\label{toc}"
"\\listoffigures"
"\\listoflistings"
"\\listoftables"
"\\newpage"
"") "\n"))
(make-variable-buffer-local 'org-latex-subtitle-format)
(setq org-latex-subtitle-format ""))
#+end_src
#+caption[Define =my-ox-latex-export-buffer-local-variables=]:
#+caption: Define =my-ox-latex-export-buffer-local-variables=.
#+name: lst:emacs-lisp-setup-defun
@ -1250,47 +1301,9 @@ Stolen from [[https://git.sr.ht/~bzg/org-contrib/tree/master/item/lisp/ox-extra.
"") "\n"))
(make-variable-buffer-local 'org-latex-subtitle-format)
(setq org-latex-subtitle-format "")
org-latex-title-command))
(setq org-latex-subtitle-format "")))
#+end_src
#+header: :header yes
#+begin_export latex
% Add fancy headers and footers to normal pages
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\footrulewidth}{0.4pt}
\fancyfoot[C]{\emph{
Emacs setup for use with \LaTeX{}, Org, and Python -- Gerard Vermeulen}}
\renewcommand{\headrulewidth}{0.4pt}
\fancyhead[L]{\includegraphics[height=1.8cm]{Org-mode-unicorn.png}}
\fancyhead[C]{
Page: \thepage/\pageref{LastPage} \\
\text{ } \\
\text{ } \\
DRAFT
}
\fancyhead[R]{\includegraphics[height=1.8cm]{Emacs-logo.png}}
% https://tex.stackexchange.com/questions/506102/
% adding-header-and-footer-to-custom-titlepage
\fancypagestyle{titlepage}{%
\fancyhf{}
\renewcommand{\footrulewidth}{0.4pt}
\fancyfoot[C]{\emph{
Emacs setup for use with \LaTeX{}, Org, and Python -- Gerard Vermeulen}}
\renewcommand{\headrulewidth}{0.4pt}
\fancyhead[L]{\includegraphics[height=1.8cm]{Org-mode-unicorn.png}}
\fancyhead[C]{
\pageref{LastPage} pages \\
\text{ } \\
\text{ } \\
DRAFT
}
\fancyhead[R]{\includegraphics[height=1.8cm]{Emacs-logo.png}}
}
#+end_export
[[lst:emacs-lisp-setup-call]]
#+caption[Call =my-ox-latex-export-local-variables=]:

View File

@ -4,14 +4,15 @@ emacs -Q --batch --eval "
(progn
(require 'org)
(require 'ob-latex)
(defun my-org-eval-blocks-named (name)
(defun my-org-eval-blocks-named (infix)
(when (eq major-mode 'org-mode)
(let ((blocks
(org-element-map
(org-element-parse-buffer 'greater-element nil) 'src-block
(lambda (block)
(when (string= name (org-element-property :name block))
block)))))
(let ((name (org-element-property :name block)))
(when (and name (string-match-p infix name))
block))))))
(dolist (block blocks)
(goto-char (org-element-property :begin block))
(org-babel-execute-src-block)))))