emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* A function that converts a LaTeX document to an Elisp expression (for org-latex-classes)
@ 2022-05-10 10:32 Juan Manuel Macías
  0 siblings, 0 replies; only message in thread
From: Juan Manuel Macías @ 2022-05-10 10:32 UTC (permalink / raw)
  To: orgmode

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


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

only message in thread, other threads:[~2022-05-10 10:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-10 10:32 A function that converts a LaTeX document to an Elisp expression (for org-latex-classes) Juan Manuel Macías

Code repositories for project(s) associated with this 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).