emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [Tip] Write the LaTeX preamble in a source block
@ 2021-02-13 20:38 Juan Manuel Macías
  0 siblings, 0 replies; only message in thread
From: Juan Manuel Macías @ 2021-02-13 20:38 UTC (permalink / raw)
  To: orgmode

[-- Attachment #1: Type: text/plain, Size: 3656 bytes --]


Although I almost always use custom LaTeX classes and a separate file
for the preamble, I came up with this method to take advantage of
the 'latex' source blocks and write the entire preamble there. I guess
there will be a more elegant way to do it, but I think that it works
reasonably well ;-)

First, this function:

#+begin_src emacs-lisp
  (defun multiple-latex-header ()
	(goto-char (point-min))
	    (re-search-forward "_src\s+latex\s+:latexheader" nil t)
	  (when (equal (org-element-type (org-element-at-point)) 'src-block)
	      (goto-char (point-min))
	      (let ((lines (split-string (replace-regexp-in-string "#\\+.+" "" (buffer-string)) "\n" nil)))
		(delete-region (point-min) (point-max))
		(insert (mapconcat (lambda
				     (unless (equal line "")
				       (format "#+LaTeX_Header: %s" line)))
				   lines "\n"))))))))

And based on this concept, we could also take advantage of the 'lua'
source blocks to generate the luacode environment (with or without
asterisk) and send it to the preamble:

#+begin_src emacs-lisp
  (defun env-luacode ()
	(goto-char (point-min))
	    (re-search-forward "_src\s+lua\s+:luacode" nil t)
	  (when (equal (org-element-type (org-element-at-point)) 'src-block)
	      (goto-char (point-min))
	      (let ((luacode (save-excursion
			       (re-search-forward "\\(luacode\\**\\)" nil t)
			       (match-string 1))))
		(when luacode
		  (while (re-search-forward "\\(#\\+begin_src\s+lua.+\\)" nil t)
		    (replace-match (format "\\\\begin{%s}" luacode) t nil))
		  (while (re-search-forward "\\(#\\+end_src\\)" nil t)
		    (replace-match (format "\\\\end{%s}" luacode) t nil))
		  (let ((lines (split-string (buffer-string) "\n" nil)))
		    (delete-region (point-min) (point-max))
		    (insert (mapconcat (lambda
					 (unless (equal line "")
					   (format "#+LaTeX_Header: %s" line)))
				       lines "\n"))))))))))

And finally:

#+begin_src emacs-lisp
  (defun luacode-latexheader-filter (backend)
     (when  (eq backend 'latex)

   (add-hook 'org-export-before-processing-hook #'luacode-latexheader-filter)

It can be tested with this example that includes a simple function in
Lua (in a luacode* environment) to colorize the texts in 'otherlanguage', but
only in draft mode:

#+begin_src org
  ,#+LATEX_CLASS: article
  ,#+LATEX_COMPILER: lualatex
  ,#+OPTIONS: toc:nil
  ,#+LaTeX_Header: \usepackage{luacode}

  ,#+begin_src lua :luacode*
    function foreignlanguage_draft ( text )
       text = string.gsub ( text, "(\\begin{otherlanguage}{[^%s]+})", "%1\\color{teal}")
       return text

  ,#+begin_src latex :latexheader
    \setmainfont[Numbers=Lowercase]{Linux Libertine O}

		( "process_input_buffer" , foreignlanguage_draft , "foreignlanguage_draft" )}}



  ,#+ATTR_LaTeX: :options {english}
  Most GNU/Linux distributions provide GNU Emacs in their repositories, which is the
  recommended way to install Emacs unless you always want to use the latest release.


Juan Manuel

[-- Attachment #2: Type: text/html, Size: 0 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-02-13 20:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-13 20:38 [Tip] Write the LaTeX preamble in a source block Juan Manuel Macías

Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).