unofficial mirror of emacs-orgmode@gnu.org
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: orgmode <emacs-orgmode@gnu.org>
Subject: [Tip] Write the LaTeX preamble in a source block
Date: Sat, 13 Feb 2021 21:38:44 +0100
Message-ID: <87sg5zpusr.fsf@posteo.net> (raw)

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

Hi,

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 ()
      (save-excursion
	(goto-char (point-min))
	(while
	    (re-search-forward "_src\s+latex\s+:latexheader" nil t)
	  (when (equal (org-element-type (org-element-at-point)) 'src-block)
	    (save-restriction
	      (org-narrow-to-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
				     (line)
				     (unless (equal line "")
				       (format "#+LaTeX_Header: %s" line)))
				   lines "\n"))))))))
#+end_src

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 ()
      (save-excursion
	(goto-char (point-min))
	(while
	    (re-search-forward "_src\s+lua\s+:luacode" nil t)
	  (when (equal (org-element-type (org-element-at-point)) 'src-block)
	    (save-restriction
	      (org-narrow-to-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
					 (line)
					 (unless (equal line "")
					   (format "#+LaTeX_Header: %s" line)))
				       lines "\n"))))))))))
#+end_src

And finally:

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

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

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_CLASS_OPTIONS: [draft]
  ,#+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
    end
  ,#+end_src

  ,#+begin_src latex :latexheader
    \usepackage{fontspec}
    \setmainfont[Numbers=Lowercase]{Linux Libertine O}
    \usepackage[english,spanish]{babel}
    \usepackage{xcolor}
    \usepackage{ifdraft}
    \usepackage{lipsum}

    \newcommand\babeldraft{\directlua{luatexbase.add_to_callback
		( "process_input_buffer" , foreignlanguage_draft , "foreignlanguage_draft" )}}

    \ifdraft{%
    \AtBeginDocument{\babeldraft}
    }{}
  ,#+end_src

  @@latex:\lipsum[1]@@

  ,#+ATTR_LaTeX: :options {english}
  ,#+begin_otherlanguage
  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.
  ,#+end_otherlanguage
#+end_src

Regards,

Juan Manuel

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

                 reply	other threads:[~2021-02-13 20:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87sg5zpusr.fsf@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    --subject='Re: [Tip] Write the LaTeX preamble in a source block' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

unofficial mirror of emacs-orgmode@gnu.org

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://yhetil.org/orgmode/0 orgmode/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 orgmode orgmode/ https://yhetil.org/orgmode \
		emacs-orgmode@gnu.org
	public-inbox-index orgmode

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.yhetil.org/yhetil.emacs.orgmode
	nntp://news.gmane.io/gmane.emacs.orgmode


code repositories for project(s) associated with this inbox:

	orgmode.git.git (no URL configured)

AGPL code for this site: git clone http://ou63pmih66umazou.onion/public-inbox.git