From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id EIZMCVShimGHIAEAgWs5BA (envelope-from ) for ; Tue, 09 Nov 2021 17:27:00 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id ULUOBVShimGYOwAA1q6Kng (envelope-from ) for ; Tue, 09 Nov 2021 16:27:00 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 9AF9147A8 for ; Tue, 9 Nov 2021 17:26:59 +0100 (CET) Received: from localhost ([::1]:52248 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mkTxZ-0007fG-RF for larch@yhetil.org; Tue, 09 Nov 2021 11:26:57 -0500 Received: from eggs.gnu.org ([209.51.188.92]:49114) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mkTwn-0007dm-KG for emacs-orgmode@gnu.org; Tue, 09 Nov 2021 11:26:10 -0500 Received: from mout01.posteo.de ([185.67.36.65]:50661) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mkTwj-0004jy-Dz for emacs-orgmode@gnu.org; Tue, 09 Nov 2021 11:26:09 -0500 Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id D84EB240027 for ; Tue, 9 Nov 2021 17:26:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1636475160; bh=mjUOa/uMYw4UXdDnO7IoGOuWvCbx2RNnG/s96y67IVE=; h=From:To:Subject:Date:From; b=Y3+Y+HCfvEOHVZO/rnwel7qMcOBnsPtNI7H4Vj3J2osfdZCBDAw4vC4YHrGueFcax MkTzjWimi98ITW/wyM650j8YsxGF66PcpsdNJrYrogljbE/JGhj1JH2gZWFaU2GzMD qIhrVl3txOIgDv345KC1acyRlh3i+DnAcwHX2xNBhnkT+HoDTsTyk3rhrbInpmJkb8 r19r5mYL+78VwhtrnJL+0Mdv9n+IucHS4NvenksQFW8nZ/r/R6Gr0Jlgp0E2jDxdnn fkKs6USMfFC+q0LmOfj7+fSnsUjKJrLbHzm2q7qcADFaoB9lD0GS3aWb2CS4pcCxKs ObmT07y9U8Q+w== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4HpYJ76SNYz9rxL for ; Tue, 9 Nov 2021 17:25:59 +0100 (CET) From: =?utf-8?Q?Juan_Manuel_Mac=C3=ADas?= To: orgmode Subject: A function to include a PDF with LaTeX commands for specific pages Date: Tue, 09 Nov 2021 16:25:58 +0000 Message-ID: <875yt1cn3d.fsf@posteo.net> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=185.67.36.65; envelope-from=maciaschain@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1636475219; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=GwHsXWdQ82MxDnUBWksECzETWLPhsslIXBiUZnfo/Do=; b=sU4WjpW+Aq6Khbd1tO1DoZt2lCfaTCP9AGzqACp8lvBtZiwVwXEwOIk3HERvd9HICwzzUr L5GnPUYKZ0SrMKVP+A0rdYnXVWNFMchootnUsG6zBqdjC/KvTUCvUq46fcSjhiNrqMef6S nUoK9/cTRhW+8JQzLhPtAYJxcVylv4Mdtjlm5QsMigjtjDtEudlGRC6hmE7p9cV7DgnPnX cUGlzEiS6uKeojpYslXLnKFjoV5oM5G33ZrePddoX0hJW3Iclms/sezROEvpN3O1bzm4qt P9NoS1+dM8k27+6R34/K7swv/cUzAOofgyGyuTgKe5tcxcnc0/73hJjyLLNVVQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1636475219; a=rsa-sha256; cv=none; b=Ck4mhR+BO55w2pTOG1TsVxO8CEOo+E+Ozg+C54Sy8tciV9Khp+6/vJ/7jgDFV/43ebToYG KbRFgpEn2JtRElCyn3nALrUQxBnzCdFArqPTjp5z86WYvf9bEkUBGROSnHhmhdiC0JYp6b wdEl5zxRR7EOExv1+BhiYN+sK6qREzUNIM9KttBImjfV2fZ+/QhJUctWV2VHhA2iDD7XQr fyHxaMN2No2+tvOa1YkK62YmTsXjQ8usx17sM4hkyC9JMNopdEyyq/vfMi7VHqgGriihXA G4prYj33WApJAf1drBEIcibZ7xW/mZLdU4mW9gUL1k/X7Sx+bavq6AqFRbB5mA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=Y3+Y+HCf; dmarc=pass (policy=none) header.from=posteo.net; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Spam-Score: -4.22 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=Y3+Y+HCf; dmarc=pass (policy=none) header.from=posteo.net; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: 9AF9147A8 X-Spam-Score: -4.22 X-Migadu-Scanner: scn1.migadu.com X-TUID: kObEMnQ2vmtT 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