emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: svg file from tikz picture


From: Edouard Debry
Subject: Re: svg file from tikz picture
Date: Tue, 20 Sep 2022 22:53:34 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (windows-nt)

Hi, I had this very same question to produce svg from latex src blocks.

First, consider this example :
#+HEADER: :file test1.png
#+HEADER: :exports results
#+HEADER: :results output silent graphics file
#+HEADER: :fit yes :noweb yes :headers '("\\usepackage{tikz}" 
"\\usetikzlibrary{backgrounds}")
#+begin_src latex
\begin{tikzpicture}[background rectangle/.style={fill=olive!30}, show 
background rectangle]
\draw[->] (-3,0) -- (-2,0) arc[radius=0.5cm,start angle=-180,end angle=0] 
(-1,0) -- (1,0) arc[radius=0.5cm,start angle=180,end angle=0] (2,0) -- (3,0);
\filldraw (-1.5,0) circle[radius=1mm];
\filldraw (1.5,0) circle[radius=3mm];
\end{tikzpicture}
#+end_src

When I run it, it does create a test1.png but, it is in fact a svg file
!! This is because, as you noticed, the `org-create-formula-image`
relies on `org-preview-latex-default-process` which is set to 'dvisvgm.
If you set it to 'dvipng, it creates again a regular png file.

Then, I changed the line :
((and (string-suffix-p ".png" out-file) (not imagemagick))
into
((and (or (string-suffix-p ".png" out-file) (string-suffix-p ".svg" out-file)) 
(not imagemagick))

so that I can correctly create a svg file when I want to and, to create
a real png file (or jpg one), I add the following line to the header :
#+HEADER: :imagemagick yes :iminoptions -density 600

There is also another way to create a svg file with `htlatex` with :

         ((and (string= "svg" extension)
               (executable-find org-babel-latex-htlatex))
          ;; TODO: this is a very different way of generating the
          ;; frame latex document than in the pdf case.  Ideally, both
          ;; would be unified.  This would prevent bugs creeping in
          ;; such as the one fixed on Aug 16 2014 whereby :headers was
          ;; not included in the SVG/HTML case.
          (with-temp-file tex-file
            (insert (concat
                     "\\documentclass[preview]{standalone}
\\def\\pgfsysdriver{pgfsys-dvisvgm4ht.def}
"
                     (mapconcat (lambda (pkg)
                                  (concat "\\usepackage" pkg))
                                org-babel-latex-htlatex-packages
                                "\n")
                     (if headers
                         (concat "\n"
                                 (if (listp headers)
                                     (mapconcat #'identity headers "\n")
                                   headers) "\n")
                       "")
                     "\\begin{document}"
                     body
                     "\\end{document}")))
          (when (file-exists-p out-file) (delete-file out-file))
          (let ((default-directory (file-name-directory tex-file)))
            (shell-command (format "%s %s" org-babel-latex-htlatex tex-file)))
          (cond
           ((file-exists-p (concat (file-name-sans-extension tex-file) 
"0x.svg"))
            (if (string-suffix-p ".svg" out-file)
                (progn
                  (shell-command "pwd")
                  (shell-command (format "mv %s %s"
                                         (concat (file-name-sans-extension 
tex-file) "0x.svg")
                                         out-file)))
              (error "SVG file produced but HTML file requested")))
           ((file-exists-p (concat (file-name-sans-extension tex-file) ".html"))
            (if (string-suffix-p ".html" out-file)
                (shell-command "mv %s %s"
                               (concat (file-name-sans-extension tex-file)
                                       ".html")
                               out-file)
              (error "HTML file produced but SVG file requested")))))

you will notice that this is a copy-paste from the html generation with
two major differences :
- for svg generation, do not use the driver
\\def\\pgfsysdriver{pgfsys-tex4ht.def}
but rather
\\def\\pgfsysdriver{pgfsys-dvisvgm4ht.def}
which handles better fonts and probably many other things
- the svg file produced to not have the same format with this driver
  ("0x.svg")

That would be great to reimplement ob-latex.el, at least correct the
first curious behavior when you end up with a svg file although you
wanted a png one.

Hope this helps.

Regards

reza <reza@housseini.me> writes:

> By setting
>
>      (setq org-babel-latex-preamble (lambda (_) 
> "\\documentclass[preview]{standalone}\n")
>
> the tikz file svg generation does run fine. Obviously the part
>
>      \\def\\pgfsysdriver{pgfsys-tex4ht.def}
>
> inside `org-babel-latex-preamble` does not play well with the svg 
> generation.
>
>
> When having a look at the code inside ob-latex.el I also encountered a 
> few stuff which made me wondering:
>
> 1. png generation is done with the preview code inside org.el 
> (org-create-formula-image), there is also a perfectly fine svg preview 
> function but this does not get used for the svg extension which does the 
> svg conversion without any external tools like inkscape (see 
> https://github.com/bzg/org-mode/blob/main/lisp/ob-latex.el#L156 and 
> https://github.com/bzg/org-mode/blob/main/lisp/org.el#L3181)
>
> 2. there is a tikz extension switch which does insert the code verbatim, 
> which in my opinion does create a whole bunch of problems (backend 
> dependency issues). Not to mention that it also mimics behaviour which 
> is reserved for the header :results (see 
> https://github.com/bzg/org-mode/blob/main/lisp/ob-latex.el#L177).
>
> 3. there is a html extension switch with an unclear purpose to me (in 
> what scenario would you want to produce an html file?). It also has some 
> strange (and contradicting) checking if an svg or an html file got 
> produced. As far as I can tell this code never gets executed and is 
> therefore pointless (see 
> https://github.com/bzg/org-mode/blob/main/lisp/ob-latex.el#L181).
>
> 4. the whole pdf generation looks like duplicate code which is already 
> done in other parts of the code base (ox-latex.el and for the svg 
> extension) it ais also not using the variable org-babel-latex-begin-env 
> and org-babel-latex-end-env (see 
> https://github.com/bzg/org-mode/blob/main/lisp/ob-latex.el#L225).
>
> I don't want to criticize anyone, I just want to find answers for in my 
> opinion some strange decisions.
>
> My propositions for refactoring is:
>
> 1. use the svg preview code for svg generation (and therefore ditching 
> the whole imagemagick headers)
>
> 2. remove the whole tikz generation completely
>
> 3. remove the whole html generation completely
>
> 4. try to merge pdf generation with org.el and ox-latex.el or 
> incorporating it into he preview code and 
> org-preview-latex-process-alist (this is probably a whole project of it own)
>
> WDYT?
>
> Best,
> Reza



reply via email to

[Prev in Thread] Current Thread [Next in Thread]