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 to include a PDF with LaTeX commands for specific pages
Date: Tue, 09 Nov 2021 16:25:58 +0000	[thread overview]
Message-ID: <875yt1cn3d.fsf@posteo.net> (raw)

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	other threads:[~2021-11-09 16:27 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=875yt1cn3d.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).