In latex export of orgmode, I would like to be able to add #+LATEX: \newpage before a heading with newpage tag, so I don't have to manually specify #+LATEX: \newpage when I want a heading to be on a new page.

It seems I can add a hook to org-export-before-parsing-hook, but I have not been able to do it. The following code successfully adds #+LATEX: \newpage before a heading with newpage tag, but when I try to add \n ((insert-string "#+LATEX: \\newpage\n")), exporting gets stuck with the message org-babel-exp process txt at position 280541.... I suspect inserting \n messes up the position.

(defun org/parse-headings (backend)
  (if (member backend '(latex))
       (lambda ()
           (insert-string "#+LATEX: \\newpage")

(add-hook 'org-export-before-parsing-hook 'org/parse-headings)

You need simple modification of Rasmus solution given by mankoff.

(defun org/get-headline-string-element  (headline backend info)
  (let ((prop-point (next-property-change 0 headline)))
    (if prop-point (plist-get (text-properties-at prop-point headline) :parent))))

(defun org/ensure-latex-clearpage (headline backend info)
  (when (org-export-derived-backend-p backend 'latex)
    (let ((elmnt (org/get-headline-string-element headline backend info)))
      (when (member "newpage" (org-element-property :tags elmnt))
        (concat "\\clearpage\n" headline)))))

(add-to-list 'org-export-filter-headline-functions

I think that \clearpage suits here better than \newpage (check here for more info).

All credits goes to Rasmus and mankoff.

| improve this answer | |
  • Note, org/get-headline-string-element relies on a hack and it will only work for headlines that are not all ~verbatim~ or =code=. Also, wouldn't you want to put "\\clearpage\n" before headline? – rasmus Mar 22 '17 at 17:16
  • Thanks for the answer! I'm not sure if this is related to the org-version, but I'm getting Symbol's value as variable is void: org-export-filter-headline-functions when I try to run this. – joon Apr 4 '17 at 21:40
  • I got it - I should have done (eval-after-load 'ox '(add-to-list .... – joon Apr 4 '17 at 21:57
  • It seems to be working well after I put "\\clearpage\n" before headline. (the answer is edited). Thank you. As @rasmus said, it does not work for headlines that are all with ~verbatim~ or `=code=, but I rarely use that so this script is currently my best option. – joon Apr 4 '17 at 22:03

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.