From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id wFMXNPU4KGCHcwAA0tVLHw (envelope-from ) for ; Sat, 13 Feb 2021 20:39:17 +0000 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id wE2zL/U4KGCIPwAAbx9fmQ (envelope-from ) for ; Sat, 13 Feb 2021 20:39:17 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 6B289149DD for ; Sat, 13 Feb 2021 21:39:17 +0100 (CET) Received: from localhost ([::1]:52184 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lB1hE-0005AP-K5 for larch@yhetil.org; Sat, 13 Feb 2021 15:39:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:42730) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lB1gp-00059x-Qj for emacs-orgmode@gnu.org; Sat, 13 Feb 2021 15:38:52 -0500 Received: from mout01.posteo.de ([185.67.36.65]:42190) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lB1gn-0003zz-D3 for emacs-orgmode@gnu.org; Sat, 13 Feb 2021 15:38:51 -0500 Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id EE0D416005F for ; Sat, 13 Feb 2021 21:38:45 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1613248726; bh=8vmUyXpSzrliDCFwpn8Mp0rB8mkSPL6w17h8A8/08Bw=; h=From:To:Subject:Date:From; b=r3mCEoP78ttXs8QXRamJaln+/cF6w7iPavjE8SddSLLz5fFtYhBF8kmcTDXGvqZJE yvhUZuLGcKYQdyl9b9851FdfDK8CH+P6KZqQ+VXwjgNSP+hO6OyxPoiMwSk3EMhfWS jjgP18J1WZFm9ELZzdcPq4fIFXjF42oEjaWdE3KmXYbSXVWtcsMWzXOFYxHhsdQ+z1 fQxW8wAwsgPyiSMw3rTpw8C7/XlGoHGgLS4VC33HGCoxJDD+QGbtvqZmgVE3rYBm49 B4axkT+lEMZwsVT2Wo3CoZ4lo1N00nRsCePPN26SWQFMqlHGL9fbfdRKA8AXUwOgvC rdS6HEYvm10CQ== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4DdMdx25yjz6tm9 for ; Sat, 13 Feb 2021 21:38:45 +0100 (CET) From: =?utf-8?Q?Juan_Manuel_Mac=C3=ADas?= To: orgmode 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> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=-=-=" Received-SPF: pass client-ip=185.67.36.65; envelope-from=maciaschain@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -1.56 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=r3mCEoP7; dmarc=pass (policy=none) header.from=posteo.net; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: 6B289149DD X-Spam-Score: -1.56 X-Migadu-Scanner: scn0.migadu.com X-TUID: 2xr8gBShjcHn --=-=-= Content-Type: text/plain 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 --=-=-= Content-Type: text/html --=-=-=--