I have a simple template in emacs org-mode:



Phone no: 

Billed amount: 


\end {flushright}


I export the document to PDF and print it. I create multiple such documents in a day.

I want to capture the information to table as below for final accounting with a single keystroke:

| Name | Billed amount |

Note: I don’t want to parse multiple files in one go but do this for one file at a time.

Is this possible?

  • How do you get the phone number? – NickD Aug 1 '18 at 21:11
  • Manual entry on template. I just want to avoid duplicating work. – Vaibhav Aug 2 '18 at 2:03

Here's a bare-bones implementation. It uses a template file in the current directory called template.org and substitutes name, phone, amount into placeholders in the template (%NAME%, %PHONE%, %AMOUNT% resp.) It then saves the resulting org-mode file in /tmp/munged-name.org where the munged-name is just the name variable with spaces replaced by dashes. It then runs the org-latex-export-to-pdf function to process the file to PDF (in /tmp/munged-name.pdf). The function is bound to F10 z (i.e. function key F10 followed by letter z) although you can bind it to whatever you want - see the code for details.

The lisp code:

(defun fill-template-and-export (name phone amt)
  (interactive "sName: \nsPhone: \nsAmount: ")
    (insert-file-contents "template.org")
       (while (re-search-forward "%NAME%" nil t)
         (replace-match name)))
       (while (re-search-forward "%PHONE%" nil t)
         (replace-match phone)))
    (while (re-search-forward "%AMOUNT%" nil t)
      (replace-match amt))
    (write-file (format "/tmp/%s.org" (replace-regexp-in-string " " "-" name)))

;;; define the function key F10 as a prefix key
(setq f10-keymap (make-sparse-keymap))
(define-key global-map [f10] f10-keymap)

;;; F10 z calls the function above
(define-key f10-keymap "z" 'fill-template-and-export)

The template file:

 #+OPTIONS: toc:nil num:nil
 #+LATEX_HEADER: \setlength{\parindent}{0pt}

 * Invoice

 Name: %NAME%\\

 Phone: %PHONE%\\

 Amount: %AMOUNT%\\

 Dear Sir/Madam:

 We would appreciate your immediate attention to this invoice.


 A.U. Thor

You can of course tweak it if you don't like the design decisions I made.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.