emacs-orgmode
[Top][All Lists]
Advanced

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

A function to include a PDF with LaTeX commands for specific pages


From: Juan Manuel Macías
Subject: A function to include a PDF with LaTeX commands for specific pages
Date: Tue, 09 Nov 2021 16:25:58 +0000

Hi,

Sometimes I need to include a pre-compiled PDF in my main document. Of
course, this can be done simply with the `pdfpages' LaTeX package. If we
want to insert a complete PDF, it would be enough to add:

#+latex: 
\includepdf[pages=-,noautoscale=true,page-command={\thispagestyle(plain}]{file.pdf}

But the 'problem' arises when we want to add multiple page-commands such
as \label{...} and \index{...} to specific pages of the PDF. It would
have to be done explicitly by putting multiple lines of \includepdf{etc},
which can be a bit monotonous. To simplify that scenario it occurred to
me to write this function:

(my-org/insert-pdfpages PDF &optional PAGE-COMMANDS-ALL PAGE-COMMANDS-PER-PAGE)

PAGE-COMMANDS-ALL is the command that should be applied to all pages;
PAGE-COMMANDS-PER-PAGE must be a list, with the page number and the
commands for that page (a possible improvement could be to allow adding
page ranges...). For example:

#+LaTeX_Header: \usepackage{pdfpages}

#+begin_src emacs-lisp :exports results :results latex
  (my-org/insert-pdfpages "file.pdf" "\\thispagestyle{plain}" '((2 
"\\label{label1}")
                                                                (3 
"\\label{label2}\\index{index1}")
                                                                (7 
"\\label{label3}\\index{index2}")
                                                                (12 
"\\index{index3}")))
#+end_src

Only tested on GNU/Linux; mupdf-tools is required, to be able to get the
number of pages of the PDF. And the function:

#+begin_src emacs-lisp
  (defun my-org/insert-pdfpages (pdf &optional page-commands-all 
page-commands-per-page)
    (let ((pdfpages-result))
      (setq pdfpages-result
            (with-temp-buffer
              (let ((counter 0)
                    (pags-pdf (shell-command-to-string
                               (format "mutool info %s | grep '^Pages' | cut -d 
' ' -f 2"
                                       pdf))))
                (dotimes (num (string-to-number pags-pdf))
                  (insert (concat
                           "\n\\includepdf[pages={"
                           (number-to-string
                            (setf counter (+ counter 1)))
                           "},"
                           "noautoscale=true,"
                           (if page-commands-all
                               (format "pagecommand={%s}" page-commands-all) "")
                           "]{"
                           pdf
                           "}"))))
              (if page-commands-per-page
                  (mapc (lambda (x)
                          (let ((pag (number-to-string (car x)))
                                (str (cadr x)))
                            (save-excursion
                              (goto-char (point-min))
                              (while (re-search-forward (concat "pages={" pag 
"}") nil t)
                                (if (re-search-forward "\\(pagecommand={\\)")
                                    (replace-match (concat "\\1" "\\" str ","))
                                  (re-search-forward "\\(\\[\\)" nil t)
                                  (replace-match (concat "\\1" "pagecommand={" 
"\\" str "},")))))))
                        page-commands-per-page)
                "")
              (buffer-string)))
      pdfpages-result))
#+end_src

Best regards,

Juan Manuel 



reply via email to

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