emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* A function to include a PDF with LaTeX commands for specific pages
@ 2021-11-09 16:25 Juan Manuel Macías
  0 siblings, 0 replies; only message in thread
From: Juan Manuel Macías @ 2021-11-09 16:25 UTC (permalink / raw)
  To: orgmode

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 


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

only message in thread, other threads:[~2021-11-09 16:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-09 16:25 A function to include a PDF with LaTeX commands for specific pages Juan Manuel Macías

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