Hello,

Florian Beck <f...@miszellen.de> writes:

> I have a document with many sections not yet written (i.e. heading
> without content) and I would like them to be ignored in the exported
> file. I know, I could manually tag these headings with :noexport:, but
> what about a more automated approach?
>
> Now, I tried this:
>
>
>   (defun ignore-empty-section (headline contents info)
>     (when contents
>       (org-e-latex-headline headline contents info)))
>
>
>   (add-to-list 'org-e-latex-translate-alist
>   '(headline . ignore-empty-section))

The global idea is correct, but I find that the implementation is too
low level. It also taints original e-latex back-end.

I suggest the following instead:

#+begin_src emacs-lisp
(defun ignore-empty-section (headline contents info)
  (when (org-string-nw-p contents)
    (org-export-with-backend 'e-latex headline contents info)))

(org-export-define-derived-backend my-draft-latex e-latex
  :translate-alist ((headline . ignore-empty-section))
  ;; Optionally add a sub-menu entry in the latex menu.
  :menu-entry (?l 99 ((?d "As a draft PDF file" my-draft-latex))))

(defun my-draft-latex (&optional async subtreep visible-only body-only 
ext-plist)
  (interactive)
  (let ((outfile (org-export-output-file-name ".tex" subtreep)))
    (if async
        (org-export-async-start
            (lambda (f) (org-export-add-to-stack f 'e-latex))
          `(expand-file-name
            (org-e-latex-compile
             (org-export-to-file
              'my-draft-latex ,outfile ,subtreep ,visible-only ,body-only
              ',ext-plist))))
      (org-e-latex-compile
       (org-export-to-file
        'my-draft-latex outfile subtreep visible-only body-only
        ext-plist)))))
#+end_src

Note that if you don't want the hassle of creating a derived back-end,
you can also implement a function that will remove every section
containing only sections or nothing from the buffer, and add it to
`org-export-before-processing-hook' (or
`org-export-before-parsing-hook').


Regards,

-- 
Nicolas Goaziou

Reply via email to