emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A function that converts a LaTeX document to an Elisp expression (for or


From: Juan Manuel Macías
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

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 via email to

[Prev in Thread] Current Thread [Next in Thread]