# keep org-mode comments on export

I am drafting a document in org. Because I will have some coauthors, I will have to switch to LaTeX at some point. I am usually putting a very short description of the paragraph on top:

# why do we use the method
We use the method because it is great!


The org-mode comments get lost during the export. For now I am just doing the following

#+LATEX: % why do we use the method
We use the method because it is great!


which works but it is not very nice because it looks ugly and M-q doesn't work for reflowing. Is there a way to include the org-mode comments in the export? Or maybe there is even a better solution using tags and headings.

• Not possible AFAICT, not with the current code: comments and comment-blocks are skipped early in generic export code, when the function org-export--prune-tree is called and long before the LaTeX exporter itself (or any other backend for that matter) lays hands on the tree: by that time there are no comments left. You'd have to change org-export--prune-tree to pass the comments through to the backend and then change the backend to teach it what to do with them. I don't think either is particularly difficult, but it would require some time, lisp knowledge and familiarity with the code. Aug 12 at 2:26
• Thanks for the insightful comment. Can you think of any better way of doing it than #+LATEX: % ... ? Aug 12 at 7:49
• I cannot think of anything better for single-line comments. For multiline comments, you can install the comment package, then add #+LATEX_HEADER: \usepackage{comment} to your Org file and then delimit multiline comments with #+LATEX: \begin{comment} and #+LATEX: \end{comment} - a slight generalization of your single-line comment method. Structure templates or some other template expansion package can make this method more tolerable. Aug 12 at 14:41
• Is there a hook that I can run before the comments get removed? Then it would be easy to just do a search/replace. Aug 15 at 14:15
• Yes, there is: org-export-before-processing-hook. Aug 15 at 15:06

Try this

;; Don't skip comment' or comment-block' elements
(defun my-org-export--skip-p (orig-fun &rest args)
(pcase-let (((,datum ,options ,selected ,excluded) args))
(cond
((memq (org-element-type datum) '(comment comment-block))
;; Don't skip
(ignore))
(t
(apply orig-fun args))))))

;; A generic transcoder for comment' and comment-block' elements
;; which works with html', odt' and latex' backends.
(defun org-*-comment (comment contents info)
(with-temp-buffer
(insert (org-element-property :value comment))
(when-let ((mode-fn (alist-get org-export-current-backend
'((html . nxml-mode)
(odt . nxml-mode)
(latex . latex-mode)))))
(funcall mode-fn)
(comment-region (point-min) (point-max))
(buffer-substring-no-properties (point-min) (point-max)))))

;; Teach the backends about the above transcoder
(cl-loop for backend in '(html latex odt)
for transcoders = (org-export-backend-transcoders
(org-export-get-backend backend))
do (cl-loop for els in '(comment comment-block)
do (setf (org-export-backend-transcoders
(org-export-get-backend backend))
(append
(list (cons 'comment 'org-*-comment)
(cons 'comment-block 'org-*-comment))
transcoders))))


1. Create somefile.org with following content
# why do we use the method

#+begin_comment
... and this method
#+end_comment

We use the method because it is great!


1. If you do C-c C-e C-b l L, you get
% why do we use the method

% ... and this method

We use the method because it is great!


1. If you do C-c C-e C-b h H, you get
<!-- why do we use the method -->

<!-- ... and this method -->

<p>
We use the method because it is great!
</p>



Include the following source block in your org file:

#+begin_src emacs-lisp :exports results
(while (re-search-forward "[:blank:]*# " nil t)
(replace-match "#+LATEX: % ")))
#+end_src

• This will probably have an issue if you have source code blocks in a language where # is used for comments.
• It will also change your global org-export-before-parsing-hook.
• It only works for exporting to LaTeX but is easily adaptable.
• The advantage is, that it is much more portable because you don't need to change your global config.