组织模式 html 导出

发布于 2024-12-05 01:41:27 字数 564 浏览 0 评论 0原文

我有一个包含源代码、文档和乳胶代码的组织文件。 Latex 的东西画了一堆图表来解释函数如何相互作用。根据

http://orgmode.org/manual/LaTeX-fragments.html

组织-mode 在使用 html 导出时应将 Latex 代码导出为图像。

#+TITLE: Test
#+AUTHOR: Blah
#+LATEX_HEADER: \usepackage{tikz}
#+OPTIONS: LaTeX:dvipng

#+begin_latex

  \begin{tikzpicture}
    \draw (1,0) -- (0,1) -- (-1,0) -- (0,-1) -- cycle;
  \end{tikzpicture}

#+end_latex

如果我使用 pdf 导出,那么我的乳胶设置可以正常工作,并且 dvipng 也存在于我的系统上,但是当导出到 html 乳胶块时,上面的工作将被完全忽略。

I have a org file with source, documentation and latex code. Latex stuff draws a bunch of graphs explaining how functions interact with each other. According to,

http://orgmode.org/manual/LaTeX-fragments.html

org-mode should export latex code as images when using html export.

#+TITLE: Test
#+AUTHOR: Blah
#+LATEX_HEADER: \usepackage{tikz}
#+OPTIONS: LaTeX:dvipng

#+begin_latex

  \begin{tikzpicture}
    \draw (1,0) -- (0,1) -- (-1,0) -- (0,-1) -- cycle;
  \end{tikzpicture}

#+end_latex

Above works if I use pdf export so my latex setup is working also dvipng is present on my system but when exporting to html latex block is completely ignored.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

書生途 2024-12-12 01:41:27

我能够将 tikz 图片从 org 导出到 html(实际上,
Reveal.js 使用 org 8.2.5h 中的 [org-reveal][1] ),并包含以下内容,

#+OPTIONS: tex:imagemagick
#+LaTeX_HEADER: \usepackage{tikz}

* Slide Title

\begin{tikzpicture}
    \draw [blue,fill] (0,0) rectangle (5,3);
    \draw [red,fill] (0,0) rectangle (3,2);
\end{tikzpicture}

希望对您有所帮助。如果没有 #+LaTeX_Header 行,LaTeX
进程不知道包含 tikz,因此构建失败。如果我尝试
tex:dvipng 而不是 tex:imagemagick,然后我得到一个图像,但是
错误地渲染了一个。

I was able to export a tikz picture from org to html (actually, to
reveal.js using [org-reveal][1] ) from org 8.2.5h with the following

#+OPTIONS: tex:imagemagick
#+LaTeX_HEADER: \usepackage{tikz}

* Slide Title

\begin{tikzpicture}
    \draw [blue,fill] (0,0) rectangle (5,3);
    \draw [red,fill] (0,0) rectangle (3,2);
\end{tikzpicture}

I hope that's helpful. Without the #+LaTeX_Header line, the LaTeX
process doesn't know to include tikz, and the build fails. If I try
tex:dvipng instead of tex:imagemagick, then I get an image, but an
incorrectly rendered one.

地狱即天堂 2024-12-12 01:41:27

使用最新版本的 Org-mode(还早于版本 9 --- 请参阅下面的编辑),您可以使用类似以下内容的内容,它可以导出到 LaTeX 以及 HTML。在后一种情况下,convert(来自 ImageMagick 工具包)用于将从 tikz 代码片段生成的 PDF 转换为大小为 100px x 100px 的 PNG 图像。

#+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:

此外,还可以添加标题,并将图片插入到 LaTeX 中的浮动 figure 环境中,方法是:

#+caption: A diamond.
#+attr_latex: :float t :width ""
#+results: diamond

注意 :width 属性设置为要擦除的空字符串Org-mode 的默认行为,导出到 LaTeX 时将图片的宽度设置为 0.9\textwidth。


根据此页面,也是可能的要以 SVG 而不是 PNG 格式导出图片,只需将 diamond.png 替换为 diamond.svg 并删除与 ImageMagick 相关的标头,如下所示:

#+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:

但请注意,此解决方案不支持数学宏在 tikz 代码中。 htlatex 应该支持基本的数学结构(通过取消注释上面的 2 条 tikz 和 lisp 行来尝试),但此功能显然需要一些返工,因为生成的 SVG 无效。


编辑

从版本 9 开始,上面的代码变成了(出于说明目的,参考了图):

#+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:

主要区别在于“COMMENT”变成了设置部分的“:noexport”标签(请参阅此答案)、by-backend 宏的代码以及“graphics " 结果的属性乳胶代码块。

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.

我很坚强 2024-12-12 01:41:27

dvipng 不处理 tikz。我在安装中通过将 dvipng 替换为以下 shell 脚本来解决这个问题:

 #! /bin/bash
 shift
 shift
 dvips $9
 gm convert -trim $9 ${9/dvi/png}

现在,我可以使用 Cx Cc Cl 预览 tikz 片段,或将其导出为 HTML。这不是一个理想的解决方案,但它对我有用。

dvipng does not handle tikz. I've worked around this on my installation by replacing dvipng with the following shell script:

 #! /bin/bash
 shift
 shift
 dvips $9
 gm convert -trim $9 ${9/dvi/png}

Now, I can preview a tikz snippet with C-x C-c C-l, or export it to HTML. Not an ideal solution, but it works for me.

南巷近海 2024-12-12 01:41:27

我无法使 tikzpicture 块正常工作,但不应使用 #+begin_latex#+end_latex ,正如您在 Org-Mode 文档的链接中所述,仅需要 \begin{...} LATEX_CODE end{...}

这个片段应该可以工作。

#+OPTIONS:      LaTeX:dvipng
* Test
  Blah

  \begin{equation}                          % arbitrary environments,
  x=\sqrt{b}                                % even tables, figures
  \end{equation}                            % etc

  If $a^2=b$ and \( b=2 \), then the solution must be either $
  a=+\sqrt{2} $ or \[ a=-\sqrt{2} \].

  Done !

I wasn't able to make tikzpicture blocks working but #+begin_latex and #+end_latex shouldn't be use, as said in your link to the Org-Mode documentation, only \begin{...} LATEX_CODE end{...} is required.

This snippet should work.

#+OPTIONS:      LaTeX:dvipng
* Test
  Blah

  \begin{equation}                          % arbitrary environments,
  x=\sqrt{b}                                % even tables, figures
  \end{equation}                            % etc

  If $a^2=b$ and \( b=2 \), then the solution must be either $
  a=+\sqrt{2} $ or \[ a=-\sqrt{2} \].

  Done !
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文