emacs org html 模板,emacs - org-mode html export - Stack Overflow

With recent versions of Org-mode (yet, older than version 9 --- see edit below), you could use something like the following, that can export to LaTeX as well as to HTML. In the latter case, convert (from the ImageMagick toolkit) is used to translate a PDF generated from the tikz code snippet into a PNG image of size 100px by 100px.

#+TITLE: Test

#+AUTHOR: Blah

#+LATEX_CLASS: article

#+LATEX_CLASS_OPTIONS: [american]

#

# Setup tikz package for both LaTeX and HTML export:

#+LATEX_HEADER: \usepackage{tikz}

#+PROPERTY: header-args:latex+ :packages '(("" "tikz"))

#

#+PROPERTY: header-args:latex+ :imagemagick (by-backend (latex nil) (t "yes"))

#+PROPERTY: header-args:latex+ :exports results :fit yes

* One Diamond

#+name: diamond

#+header: :iminoptions -density 600 -resample 100x100

#+header: :file (by-backend (latex "diamond.tikz") (t "diamond.png"))

#+begin_src latex :results raw file

\begin{tikzpicture}

\draw (1,0) -- (0,1) -- (-1,0) -- (0,-1) -- cycle;

\end{tikzpicture}

#+end_src

#+attr_latex: :float nil :width ""

#+results: diamond

* COMMENT setup

#+name: setup

#+begin_src emacs-lisp :results silent :exports none

(defmacro by-backend (&rest body)

`(case (if (boundp 'backend) (org-export-backend-name backend) nil) ,@body))

#+end_src

# Local variables:

# eval: (org-sbe "setup")

# End:

Besides, one can add a caption, and insert the picture in a floating figure environment in LaTeX, by using:

#+caption: A diamond.

#+attr_latex: :float t :width ""

#+results: diamond

Note the :width attribute is set to the empty string to erase the default behavior of Org-mode, which sets the width of the picture to 0.9\textwidth when exporting to LaTeX.

According to this page, it is also possible to export pictures in SVG instead of PNG, simply by replacing diamond.png by diamond.svg and removing ImageMagick-related headers, as in:

#+TITLE: Test

#+AUTHOR: Blah

#+LATEX_CLASS: article

#+LATEX_CLASS_OPTIONS: [american]

#

# Setup tikz package for both LaTeX and HTML export:

#+LATEX_HEADER: \usepackage{tikz}

#+PROPERTY: header-args:latex+ :packages '(("" "tikz"))

#

#+PROPERTY: header-args:latex+ :exports results

* One Diamond

#+name: diamond

#+header: :file (by-backend (latex "diamond.tikz") (t "diamond.svg"))

#+begin_src latex :results raw file

\begin{tikzpicture}

\draw (1,0) -- (0,1) -- (-1,0) -- (0,-1) -- cycle;

% \node at (0,0) {\(x_i\)};

\end{tikzpicture}

#+end_src

#+caption: A diamond.

#+attr_latex: :float t :width ""

#+results: diamond

* COMMENT setup

#+name: setup

#+begin_src emacs-lisp :results silent :exports none

;; (setq org-babel-latex-htlatex "htlatex")

(defmacro by-backend (&rest body)

`(case (if (boundp 'backend) (org-export-backend-name backend) nil) ,@body))

#+end_src

# Local variables:

# eval: (org-sbe "setup")

# End:

Note however that this solution does not support mathematical macros in tikz code as is. htlatex ought to support basic mathematical constructs (to be tried by un-commenting the 2 tikz and lisp lines above), but this feature needs some rework apparently, as the resulting SVG is invalid.

Edit

Since version 9, the code above becomes (with a reference to the figure, for illustrative purposes):

#+TITLE: Test

#+AUTHOR: Blah

#+LATEX_CLASS: article

#+LATEX_CLASS_OPTIONS: [american]

#

# Setup tikz package for both LaTeX and HTML export:

#

#+LATEX_HEADER: \usepackage{tikz}

#+PROPERTY: header-args:latex+ :packages '(("" "tikz"))

#+PROPERTY: header-args:latex+ :imagemagick yes :fit yes

* One Diamond

#+name: diamond

#+header: :iminoptions -density 600 -resample 100x100

#+header: :file (by-backend (latex "diamond.tikz") (t "diamond.png"))

#+begin_src latex :results raw graphics

\begin{tikzpicture}

\draw (1,0) -- (0,1) -- (-1,0) -- (0,-1) -- cycle;

\end{tikzpicture}

#+end_src

#+name: fig:diamond

#+caption: A diamond.

#+attr_latex: :float t :width ""

#+results: diamond

Figure [[fig:diamond]] is a diamond.

* Setup :noexport:

#+name: setup

#+begin_src emacs-lisp :exports none :results silent

(defmacro by-backend (&rest body)

`(case org-export-current-backend ,@body))

#+end_src

# Local variables:

# eval: (org-sbe "setup")

# End:

The main differences are in the "COMMENT" becoming a ":noexport" tag for the setup section (see this answer), the code of by-backend macro, and the "graphics" attribute for the result of the latex code block.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值