Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org-mode html export

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.

like image 786
Hamza Yerlikaya Avatar asked Sep 19 '11 07:09

Hamza Yerlikaya


4 Answers

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.

like image 73
Gareth Avatar answered Nov 20 '22 13:11

Gareth


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.

like image 32
nberth Avatar answered Nov 20 '22 13:11

nberth


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.

like image 21
Mike Gauland Avatar answered Nov 20 '22 13:11

Mike Gauland


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 !
like image 45
Guillaume Papin Avatar answered Nov 20 '22 14:11

Guillaume Papin