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 #+title: Emacs setup for use with LaTeX, Org, and Python
#+author: Gerard Vermeulen #+author: Gerard Vermeulen
#+babel: :cache no #+babel: :cache no
#+property: header-args :tangle init.el :comments link #+property: header-args :tangle init.el :comments link
#+property: header-args:emacs-lisp :exports code :results silent #+property: header-args:emacs-lisp :exports code :results silent
#+property: header-args:latex-preamble :tangle no
# #
#+latex_class: article #+latex_class: article
#+latex_class_options: [11pt,a4paper,svgnames] #+latex_class_options: [11pt,a4paper,english,svgnames]
#+latex_header: \usepackage[AUTO]{babel}
#+latex_header: \usepackage{fvextra} * LaTeX preamble :noexport:
#+latex_header: \usepackage{csquotes}
# #+begin_src latex-preamble
#+latex_header: \usepackage{caption} % Use babel for biblatex.
#+latex_header: \usepackage[newfloat]{minted} \usepackage{babel}
#+latex_header: \captionsetup[listing]{position=below,skip=0em} \usepackage{fvextra}
#+latex_header: \usemintedstyle{xcode} \usepackage{csquotes}
# % Section 2.6 of caption-eng.pdf (texdoc caption) explains that the sign
#+latex_header: \hypersetup{ % of "skip" depends on the assumption "position=above" or "position=below".
#+latex_header: citecolor=blue, % The assumption should match the real caption position in the LaTeX code.
#+latex_header: colorlinks=true, \usepackage{caption}
#+latex_header: filecolor=blue, \usepackage[newfloat]{minted}
#+latex_header: hyperfootnotes=false, \captionsetup[listing]{position=below,skip=0em}
#+latex_header: linkcolor=blue, \usemintedstyle{xcode}
#+latex_header: unicode=true, % Use lastpage in fancy header.
#+latex_header: urlcolor=blue \usepackage{fancyhdr}
#+latex_header: } \usepackage{lastpage}
# % Use geometry without warnings.
#+latex_header: \usepackage{fancyhdr} \usepackage[
#+latex_header: \usepackage{lastpage} headheight=20mm,
# top=40mm,
#+latex_header: \usepackage[ bottom=20mm,
#+latex_header: headheight=20mm, left=60pt,
#+latex_header: top=40mm, right=60pt,
#+latex_header: bottom=20mm, heightrounded,
#+latex_header: left=60pt, verbose,
#+latex_header: right=60pt, ]{geometry}
#+latex_header: heightrounded, #+end_src
#+latex_header: verbose,
#+latex_header: ]{geometry} #+begin_src latex-preamble
# % Make section, subsection and subsubsection implicit float barriers.
#+latex_header: % Make section an implicit float barrier: % https://tex.stackexchange.com/questions/118662/use-placeins-for-subsections
#+latex_header: \usepackage[section]{placeins} % Make section an implicit float barrier:
#+latex_header: % Make subsection an implicit float barrier: \usepackage[section]{placeins}
#+latex_header: \makeatletter % Make subsection an implicit float barrier:
#+latex_header: \AtBeginDocument{% \makeatletter
#+latex_header: \expandafter\renewcommand\expandafter\subsection\expandafter{% \AtBeginDocument{%
#+latex_header: \expandafter\@fb@secFB\subsection \expandafter\renewcommand\expandafter\subsection\expandafter{%
#+latex_header: }% \expandafter\@fb@secFB\subsection
#+latex_header: } }%
#+latex_header: \makeatother }
#+latex_header: % Make subsubsection an implicit float barrier: \makeatother
#+latex_header: \makeatletter % Make subsubsection an implicit float barrier:
#+latex_header: \AtBeginDocument{% \makeatletter
#+latex_header: \expandafter\renewcommand\expandafter\subsubsection\expandafter{% \AtBeginDocument{%
#+latex_header: \expandafter\@fb@secFB\subsubsection \expandafter\renewcommand\expandafter\subsubsection\expandafter{%
#+latex_header: }% \expandafter\@fb@secFB\subsubsection
#+latex_header: } }%
#+latex_header: \makeatother }
\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 * Quick start
:PROPERTIES: :PROPERTIES:
@ -738,31 +779,38 @@ rebuild the =epdfinfo= executable that serves the [[https://en.wikipedia.org/wik
:END: :END:
Loading =tex.el= immediately instead of lazily ensures proper initialization of 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=, 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 Emacs will complain that ~TeX-master~ is no safe local variable in case it reads
reads a LaTeX file that sets ~TeX-master~. 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=]:
#+caption: Require =AUCTeX=. #+caption: Require =AUCTeX=.
#+label: lst:require-auctex #+label: lst:require-auctex
#+begin_src emacs-lisp #+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) (when (require 'tex nil 'noerror)
(custom-set-variables (custom-set-variables
'(TeX-auto-save t) '(TeX-auto-save t)
'(TeX-install-font-lock #'font-latex-setup)
'(TeX-parse-self t))) '(TeX-parse-self t)))
#+end_src #+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=]:
#+caption: Configure =bibtex=. #+caption: Configure =bibtex=.
#+label: lst:configure-bibtex #+label: lst:configure-bibtex
#+begin_src emacs-lisp #+begin_src emacs-lisp
(with-eval-after-load 'bibtex (with-eval-after-load 'bibtex
(custom-set-variables (custom-set-variables '(bibtex-dialect 'BibTeX)))
'(bibtex-dialect 'BibTeX)))
#+end_src #+end_src
Listing [[lst:configure-font-latex]] disables font scaling of section titles.
#+caption[Configure =font-latex=]: #+caption[Configure =font-latex=]:
#+caption: Configure =font-latex=. #+caption: Configure =font-latex=.
#+label: lst: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))) '(font-latex-fontify-sectioning 1.0)))
#+end_src #+end_src
Listing [[lst:configure-latex]] configures =latex= for a full featured
=LaTeX-section-command=.
#+caption[Configure =latex=]: #+caption[Configure =latex=]:
#+caption: Configure =latex=. #+caption: Configure =latex=.
#+label: lst: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 (with-eval-after-load 'ox-latex
(custom-set-variables (custom-set-variables
'(org-latex-compiler "lualatex") '(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-listings 'minted)
'(org-latex-minted-options '(("bgcolor" "LightGoldenrodYellow"))) '(org-latex-minted-options '(("bgcolor" "LightGoldenrodYellow")))
'(org-latex-logfiles-extensions '("blg" "lof" "log" "lot" "out" "toc")) '(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))) (format "@ref{%s,%s,,%s,}" node title manual)))
(_ nil)))) (_ 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) (make-variable-buffer-local 'org-info-other-documents)
(setq org-info-other-documents (setq org-info-other-documents
(cl-union '(("consult" . "https://github.com/minad/consult") (cl-union '(("consult" . "https://github.com/minad/consult")
("embark" . "https://github.com/oantolin/embark") ("embark" . "https://github.com/oantolin/embark")
("marginalia" . "https://github.com/minad/marginalia") ("marginalia" . "https://github.com/minad/marginalia")
("org" . "https://orgmode.org/org.html")
("orderless" . "https://github.com/oantolin/orderless") ("orderless" . "https://github.com/oantolin/orderless")
("vertico" . "https://github.com/minad/vertico")) ("vertico" . "https://github.com/minad/vertico"))
org-info-other-documents org-info-other-documents
@ -1104,9 +1172,10 @@ this document to =html= and LaTeX correctly.
(org-element-map (org-element-map
(org-element-parse-buffer 'greater-element nil) 'src-block (org-element-parse-buffer 'greater-element nil) 'src-block
(lambda (block) (lambda (block)
(message "%s" (org-element-property :name block)) (let ((name (org-element-property :name block)))
(when (string-match-p infix (org-element-property :name block)) (message "%s" name)
block))))) (when (and name (string-match-p infix name))
block))))))
(dolist (block blocks) (dolist (block blocks)
(goto-char (org-element-property :begin block)) (goto-char (org-element-property :begin block))
(org-babel-execute-src-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]] 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]:
#+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 #+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]] [[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=]:
#+caption: Define =my-ox-latex-export-buffer-local-variables=. #+caption: Define =my-ox-latex-export-buffer-local-variables=.
#+name: lst:emacs-lisp-setup-defun #+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")) "") "\n"))
(make-variable-buffer-local 'org-latex-subtitle-format) (make-variable-buffer-local 'org-latex-subtitle-format)
(setq org-latex-subtitle-format "") (setq org-latex-subtitle-format "")))
org-latex-title-command))
#+end_src #+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]] [[lst:emacs-lisp-setup-call]]
#+caption[Call =my-ox-latex-export-local-variables=]: #+caption[Call =my-ox-latex-export-local-variables=]:

View File

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