org-mode html export
Asked Answered
V

4

8

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.

V1 answered 19/9, 2011 at 7:53 Comment(3)
I wonder what do you expect from html exporter.Ronnironnica
@kindahero, I certainly don't expect it to make me a cup of coffee but I do expect it to render the graph with dvipng and embed it into the html.V1
Then i guess you should use babel orgmode.org/worg/org-contrib/babel/languages/ob-doc-LaTeX.htmlRonnironnica
O
2

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 !
Outpatient answered 20/9, 2011 at 19:47 Comment(0)
F
7

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.

Fronniah answered 8/4, 2014 at 14:57 Comment(1)
that's exactly what I have been looking for the whole day (also to get it to work in org-reveal btw)!Marcelina
C
7

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.

Clump answered 27/6, 2014 at 13:25 Comment(2)
Hello, could you tell what is latex+ exaclty?Ingleside
#+PROPERTY: header-args:latex+ <args> appends <args> to the header arguments named header-args:latex. This is somehow "guessable" in the docs (though I agree this could be improved).Clump
G
3

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.

Guitar answered 1/7, 2012 at 21:22 Comment(1)
Any luck with this, or are you still using the same workaround? I can't make this work.Neoterize
O
2

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 !
Outpatient answered 20/9, 2011 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.