emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: orgmode <emacs-orgmode@gnu.org>
Subject: A function that converts a LaTeX document to an Elisp expression (for org-latex-classes)
Date: Tue, 10 May 2022 10:32:51 +0000	[thread overview]
Message-ID: <87czgly8x8.fsf@posteo.net> (raw)

Hi all,

In case anyone finds it useful, I'm sharing this function here that I
recently wrote, to convert a LaTeX buffer to an Elisp expression,
suitable for adding to `org-latex-classes'. It's a bit rudimentary, but
I think it does the trick. It can be useful for long preambles with a
lot of (La)TeX code. Although if you are dealing with bizarrely large preambles
that are used often, I would recommend writing your own .sty file
(https://tex.stackexchange.com/questions/8750/make-your-own-sty-files).
The funny thing is that doing it from Org is much easier than from the
official LaTeX utility for creating packages through literary
programming, docstrip.

As for my function, the LaTeX document to be converted must have this skeleton:

- Preamble

- `\begin{document}'

- An ordered list of section names (one name per line). For example:

  chapter
  section
  subsection
  susbsubsection
  etc

- `\end{document}'

Best regards,

Juan Manuel

#+begin_src emacs-lisp
  (defun create-new-org-latex-class-from-latex-buffer ()
    "Convert the current LaTeX buffer to an appropriate Elisp
  expression to add to `org-latex-classes'. The LaTeX document must
  have the following structure:
   - Preamble
   - `\begin{document}'
   - A list of section names (one name per line). For example:
     section
     subsection
     susbsubsection
     etc
  - `\end{document}'"
    (interactive)
    (if (not (equal (format "%s" major-mode) "latex-mode"))
	(error "Not in a LaTeX buffer")
      (let* ((class-name (read-from-minibuffer "Class name: "))
	     (preamble-beg (with-current-buffer
			       (buffer-name)
			     (save-excursion
			       (goto-char (point-min))
			       (point))))
	     (preamble-end (with-current-buffer
			       (buffer-name)
			     (save-excursion
			       (goto-char (point-min))
			       (re-search-forward "\\\\begin{document}" nil t)
			       (beginning-of-line)
			       (point))))
	     (packages "[NO-DEFAULT-PACKAGES]
		       [PACKAGES]
		       [EXTRA]")
	     (preamble (concat
			(buffer-substring-no-properties preamble-beg preamble-end)
			packages))
	     (preamble-list (list preamble))
	     (sections-list)
	     (sections-list-populate (with-current-buffer (buffer-name)
				       (save-excursion
					 (goto-char (point-min))
					 (let ((beg-sec
						(save-excursion
						  (re-search-forward "\\\\begin{document}" nil t)
						  (point)))
					       (end-sec
						(save-excursion
						  (re-search-forward "\\\\end{document}" nil t)
						  (beginning-of-line)
						  (point))))
					   (save-restriction
					     (narrow-to-region beg-sec end-sec)
					     (while (re-search-forward "\\(^.+\\)" nil t)
					       (push (substring-no-properties (match-string 1)) sections-list)))
					   (reverse sections-list)))))
	     (section-list-final (mapcar
				  (lambda (x)
				    (let ((car (format "\\%s{%%s}" x))
					  (cdr (format "\\%s*{%%s}" x)))
				      (cons car cdr)))
				  sections-list-populate))
	     (list-final (append (list class-name) preamble-list section-list-final))
	     (format-list (format "%S" list-final)))
	(when (get-buffer "*class*")
	  (kill-buffer "*class*"))
	(get-buffer-create "*class*")
	(with-current-buffer "*class*"
	  (insert format-list))
	(temp-buffer-window-show "*class*"))))
#+end_src


                 reply	other threads:[~2022-05-10 10:36 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=87czgly8x8.fsf@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

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).