2

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.

7
  • 1
    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.
    – NickD
    Commented Aug 12, 2022 at 2:26
  • Thanks for the insightful comment. Can you think of any better way of doing it than #+LATEX: % ... ?
    – gdkrmr
    Commented Aug 12, 2022 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.
    – NickD
    Commented Aug 12, 2022 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.
    – gdkrmr
    Commented Aug 15, 2022 at 14:15
  • Yes, there is: org-export-before-processing-hook.
    – NickD
    Commented Aug 15, 2022 at 15:06

2 Answers 2

2

Try this

;; Don't skip `comment' or `comment-block' elements
(advice-add 'org-export--skip-p :around
            (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>

1

Include the following source block in your org file:

#+begin_src emacs-lisp :exports results
  (defun transform-comments (backend)
    (while (re-search-forward "[:blank:]*# " nil t)
      (replace-match "#+LATEX: % ")))
    (add-hook 'org-export-before-parsing-hook #'transform-comments)
#+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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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