emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* latex block tikz to svg
@ 2022-01-17 14:14 Edouard Debry
  2022-01-17 16:01 ` Juan Manuel Macías
  0 siblings, 1 reply; 4+ messages in thread
From: Edouard Debry @ 2022-01-17 14:14 UTC (permalink / raw)
  To: emacs-orgmode


Hello,

I experience some difficulties to export latex src block
consisting of tikz code to svg.

Here is for example the simple code I try to run with `C-c C-c` :

<==================================================>
#+HEADER: :file test1.png
#+HEADER: :imagemagick yes
#+HEADER: :exports results
#+HEADER: :results output silent graphics file
#+HEADER: :headers '("\\usepackage{tikz}")
#+HEADER: :fit yes :imoutoptions -geometry 400 :iminoptions -density 600
#+begin_src latex
\begin{tikzpicture}             %
\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=1mm];
\end{tikzpicture}
#+end_src
<==================================================>

First of all, for this to work I have to change  `org-latex-pdf-process`
to ("latexmk.exe -pdf -f %f -cd %o"), which unfortunately breaks the
latex to pdf export of org files (C-c C-e l o/p).

Then, it works when image file format is png. However, a close look of
the pdflatex output shows this error :

<====================================================================>
=== TeX engine is 'pdfTeX'
Latexmk: All targets (latex-JEShjO.pdf) are up-to-date
Rule 'pdflatex': File changes, etc:
   Changed files, or newly in use since previous run(s):
      'd:/xxxx/Documents/utils/texlive/2021/texmf-dist/tex/latex/tools/.tex'
------------
Run number 1 of rule 'pdflatex'
------------
------------
Running 'pdflatex -shell-escape -synctex=1 -interaction=nonstopmode  -recorder  "d:\xxxxxx\Documents\utils\texlive\2021\texmf-dist\tex\latex\tools\.tex"'
------------
Latexmk: applying rule 'pdflatex'...
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2021/W32TeX) (preloaded format=pdflatex)
 \write18 enabled.
entering extended mode
(d:/xxxxxx/Documents/utils/texlive/2021/texmf-dist/tex/latex/tools/.tex
LaTeX2e <2021-11-15> patch level 1
L3 programming layer <2022-01-12> File ignored)
! Emergency stop.
<*> ...exlive/2021/texmf-dist/tex/latex/tools/.tex
                                                  
!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on .log.
Failure to make '.pdf'
Collected error summary (may duplicate other messages):
  pdflatex: Command for 'pdflatex' gave return code 1
      Refer to '.log' for details
Latexmk: Undoing directory change

------------
Latexmk: Some operations failed, for the following tex file(s)
  
Latexmk: Examining '.log'
=== TeX engine is 'pdfTeX'
Latexmk: Errors, in force_mode: so I tried finishing targets
...xxxx\Documents\utils\texlive\2021\bin\win32\runscript.tlu:915: command failed with exit code 12:
perl.exe d:\xxxxxx\Documents\utils\texlive\2021\texmf-dist\scripts\latexmk\latexmk.pl -pdf -f "c:/Users/xxxxxx/AppData/Local/Temp/babel-B1S4gp/latex-JEShjO.tex" -cd "c:/Users/xxxxxx/AppData/Local/Temp/babel-B1S4gp/"
<=========================================================================================>

I wonder why it tries every time to compile a ".tex" file although the
temporary previous pdf file was correctly generated.


Finally, if I change "test1.png" to "test1.svg", such a svg file is
generated but with garbage in it.

I would like to find a way to generate svg images from latex src blocks
(using tikz) which works and is compatible with default orgmode settings
for latex export (at least does not break it)

Did you experience such issues ? do you have some workings settings and
examples ? I googled several times "org latex block tikz svg", but it is
difficult to guess how relevant are the elements found, some of them
seems quiet outdated. Hence my question on this mailing list

Regards

My configuration :
- windows 11
- emacs 29.0.50 (compiled master from a few days)
- orgmode (9.5.2)
- mingw64 : imagemagick
- texlive 2021 (installed from yesterday)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: latex block tikz to svg
  2022-01-17 14:14 latex block tikz to svg Edouard Debry
@ 2022-01-17 16:01 ` Juan Manuel Macías
  2022-01-25 23:24   ` Edouard Debry
  2022-04-18 13:23   ` Edouard Debry
  0 siblings, 2 replies; 4+ messages in thread
From: Juan Manuel Macías @ 2022-01-17 16:01 UTC (permalink / raw)
  To: Edouard Debry; +Cc: orgmode

Hi Edouard,

Edouard Debry writes:

> I would like to find a way to generate svg images from latex src blocks
> (using tikz) which works and is compatible with default orgmode settings
> for latex export (at least does not break it)
>
> Did you experience such issues ? do you have some workings settings and
> examples ? I googled several times "org latex block tikz svg", but it is
> difficult to guess how relevant are the elements found, some of them
> seems quiet outdated. Hence my question on this mailing list

I've done some quick tests with your example block. I don't know if I'm
wrong, but I think the problem is on line 27 of `org-babel-execute:latex':

((string= "svg" extension)

I don't know if this should be considered an Org bug, but it's clear
that if the svg extension is detected in :file, the ':imagemagick yes'
option is ignored, and a type of preamble is generated that fails in
pgfsysdriver when compiling the temp tex document:

\documentclass[preview]{standalone}
\def\pgfsysdriver{pgfsys-tex4ht.def}

If I replace the above line with this conditional:

((and (string= "svg" extension) (not imagemagick))

then the imagemagick option is taken into account: it creates correctly
the pdf and then converts it to svg with 'convert' imagemagick program.
I did have to remove this line though:

#+HEADER: :imoutoptions -geometry 400 :iminoptions -density 600

otherwise, the conversion produced a dark image.

Best regards,

Juan Manuel 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: latex block tikz to svg
  2022-01-17 16:01 ` Juan Manuel Macías
@ 2022-01-25 23:24   ` Edouard Debry
  2022-04-18 13:23   ` Edouard Debry
  1 sibling, 0 replies; 4+ messages in thread
From: Edouard Debry @ 2022-01-25 23:24 UTC (permalink / raw)
  To: Juan Manuel Macías; +Cc: orgmode


Hi Juan,

Thanks for your answeer.

Indeed, the generated tex files are different whether you use the png or
svg extension.

It seems that for svg, a distinct build system is used from the usual
one for other image types (png, jpg).

This build system seems based on tex4ht, given the latex file preamble.

I tried to compile manually this temporary latex file (the one for svg)
with "pdflatex" which fails. Then, I tried with "htlatex" and guess what
... it works ! A svg file is created, which unfortunately does not
contain everything, but this was only manual compiling, probably some
missing options.

I think at first there may be a bug in my own configuration of the
variable "org-latex-pdf-process" which should be passed the correct
build system.

Regards

Juan Manuel Macías <maciaschain@posteo.net> writes:

> Hi Edouard,
>
> Edouard Debry writes:
>
>> I would like to find a way to generate svg images from latex src blocks
>> (using tikz) which works and is compatible with default orgmode settings
>> for latex export (at least does not break it)
>>
>> Did you experience such issues ? do you have some workings settings and
>> examples ? I googled several times "org latex block tikz svg", but it is
>> difficult to guess how relevant are the elements found, some of them
>> seems quiet outdated. Hence my question on this mailing list
>
> I've done some quick tests with your example block. I don't know if I'm
> wrong, but I think the problem is on line 27 of `org-babel-execute:latex':
>
> ((string= "svg" extension)
>
> I don't know if this should be considered an Org bug, but it's clear
> that if the svg extension is detected in :file, the ':imagemagick yes'
> option is ignored, and a type of preamble is generated that fails in
> pgfsysdriver when compiling the temp tex document:
>
> \documentclass[preview]{standalone}
> \def\pgfsysdriver{pgfsys-tex4ht.def}
>
> If I replace the above line with this conditional:
>
> ((and (string= "svg" extension) (not imagemagick))
>
> then the imagemagick option is taken into account: it creates correctly
> the pdf and then converts it to svg with 'convert' imagemagick program.
> I did have to remove this line though:
>
> #+HEADER: :imoutoptions -geometry 400 :iminoptions -density 600
>
> otherwise, the conversion produced a dark image.
>
> Best regards,
>
> Juan Manuel 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: latex block tikz to svg
  2022-01-17 16:01 ` Juan Manuel Macías
  2022-01-25 23:24   ` Edouard Debry
@ 2022-04-18 13:23   ` Edouard Debry
  1 sibling, 0 replies; 4+ messages in thread
From: Edouard Debry @ 2022-04-18 13:23 UTC (permalink / raw)
  To: Juan Manuel Macías; +Cc: orgmode


Hi,

I finally found a way to make the following example work

<===========================================================>
#+HEADER: :file test1.svg
#+HEADER: :exports results
#+HEADER: :results output silent graphics file
#+HEADER: :headers '("\\usepackage{tikz}")
#+begin_src latex
\begin{tikzpicture}
\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
<===========================================================>

In file ob-latex.el, I commented out the lines beginning with ((string=
"svg" extension) :
	 ;; ((string= "svg" extension)
	 ;;  (with-temp-file tex-file
	 ;;    (insert (concat (funcall org-babel-latex-preamble params)
	 ;;        	    (mapconcat #'identity headers "\n")
	 ;;        	    (funcall org-babel-latex-begin-env params)
	 ;;        	    body
	 ;;        	    (funcall org-babel-latex-end-env params))))
	 ;;  (let ((tmp-pdf (org-babel-latex-tex-to-pdf tex-file)))
         ;;    (let* ((log-buf (get-buffer-create "*Org Babel LaTeX Output*"))
         ;;           (err-msg "org babel latex failed")
         ;;           (img-out (org-compile-file
	 ;;                     tmp-pdf
         ;;                     (list org-babel-latex-pdf-svg-process)
         ;;                     extension err-msg log-buf)))
         ;;      (shell-command (format "mv %s %s" img-out out-file)))))

This part is responsible for the previous example to fail. What actually
breaks the svg production is the latex preamble. "pdflatex" fails itself
to produce a valid pdf file.


After that, I changed
((and (string= "html" extension)
	       (executable-find org-babel-latex-htlatex))

into

((and (or (string= "html" extension) (string= "svg" extension))
	       (executable-find org-babel-latex-htlatex))

Then, previous example, is compiled no more by "pdflatex" but by
"htlatex", this latter is able to produce svg directly.

I hope I do not introduce any side effects in doing so. There are several ways
to produce svg :
- pdflatex + inkscape (default with current org implementation)
- latex + dvisvgm
- pdflatex + pdf2svg
- htlatex (my choice above)

Ideally, all ways could be implemented and orgmode's user should choose
which way to use through a header parameter. As far as I could
understand, "htlatex" is the faster (direct) and cleaner way to produce
svg, but it may lack some advanced features, which is probably why, at the 
moment, the default way is through pdf generation.

Regards
  
Juan Manuel Macías <maciaschain@posteo.net> writes:

> Hi Edouard,
>
> Edouard Debry writes:
>
>> I would like to find a way to generate svg images from latex src blocks
>> (using tikz) which works and is compatible with default orgmode settings
>> for latex export (at least does not break it)
>>
>> Did you experience such issues ? do you have some workings settings and
>> examples ? I googled several times "org latex block tikz svg", but it is
>> difficult to guess how relevant are the elements found, some of them
>> seems quiet outdated. Hence my question on this mailing list
>
> I've done some quick tests with your example block. I don't know if I'm
> wrong, but I think the problem is on line 27 of `org-babel-execute:latex':
>
> ((string= "svg" extension)
>
> I don't know if this should be considered an Org bug, but it's clear
> that if the svg extension is detected in :file, the ':imagemagick yes'
> option is ignored, and a type of preamble is generated that fails in
> pgfsysdriver when compiling the temp tex document:
>
> \documentclass[preview]{standalone}
> \def\pgfsysdriver{pgfsys-tex4ht.def}
>
> If I replace the above line with this conditional:
>
> ((and (string= "svg" extension) (not imagemagick))
>
> then the imagemagick option is taken into account: it creates correctly
> the pdf and then converts it to svg with 'convert' imagemagick program.
> I did have to remove this line though:
>
> #+HEADER: :imoutoptions -geometry 400 :iminoptions -density 600
>
> otherwise, the conversion produced a dark image.
>
> Best regards,
>
> Juan Manuel 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-04-18 13:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-17 14:14 latex block tikz to svg Edouard Debry
2022-01-17 16:01 ` Juan Manuel Macías
2022-01-25 23:24   ` Edouard Debry
2022-04-18 13:23   ` Edouard Debry

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).