Customizing org-mode exports

前端 未结 4 903
醉话见心
醉话见心 2021-02-01 10:07

So, I have been using org-mode for taking my research notes for some time now. I love how I can seamlessly export to both latex (for my papers) and html (for my blog). However,

4条回答
  •  有刺的猬
    2021-02-01 10:34

    Alternative solution (not stand-alone), using Org's dynamic blocks.

    • It has none of the caveats of my original solution (because it generates what Org actually expects for LaTeX or HTML)
    • It's possible to edit the macros in LaTeX mode (C-c C-')

    Callback

    Create a file named org-dblock-write:block-macro.el with the following content and add it to Emacs' load path.

    (defun org-dblock-write:block-macro (params)
      (let ((block-name (or (plist-get params :from) "macros"))
        (org-buf (current-buffer)))
        (with-temp-buffer
          (let ((tmp-buf (current-buffer)))
        (set-buffer org-buf)
        (save-excursion
          (org-babel-goto-named-src-block block-name)
          (org-babel-mark-block)
          (let ((mblock-begin (region-beginning))
                (mblock-end (region-end)))
            (set-buffer tmp-buf)
            (insert-buffer-substring org-buf mblock-begin mblock-end)))
        (set-buffer org-buf)
        (insert "#+BEGIN_HTML\n\\(\n")
        (insert-buffer-substring tmp-buf)
        (insert "\\)\n#+END_HTML\n")
        (set-buffer tmp-buf)
        (beginning-of-buffer)
        (while (re-search-forward "^" nil t)
          (replace-match "#+LATEX_HEADER: " nil nil))
        (set-buffer org-buf)
        (insert-buffer-substring tmp-buf)))))
    

    Org file

    Somewhere in the file, create:

    • A LaTeX source block named "macros" containing your macros
    • An empty block-macro dynamic block

    You can change the name of the source block, and use a :from header argument in the dynamic block. Also, note the :exports none in the source block (usually you don't want to export the LaTeX source).

    #+NAME: macros
    #+BEGIN_SRC latex :exports none
      \newcommand\a{a}
      \def\b{b}
      \DeclareMathOperator\c{c}
    #+END_SRC
    #+BEGIN: block-macro
    #+END:
    

    Now use C-c C-c with the point on the dynamic block, and it will update to:

    #+BEGIN: block-macro
    #+BEGIN_HTML
    \(
          \newcommand\a{a}
          \def\b{b}
          \DeclareMathOperator\c{c}
    \)
    #+END_HTML
    #+LATEX_HEADER:       \newcommand\a{a}
    #+LATEX_HEADER:       \def\b{b}
    #+LATEX_HEADER:       \DeclareMathOperator\c{c}
    #+LATEX_HEADER: 
    #+END:
    

    Do this whenever the macros are modified.

提交回复
热议问题