emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Creating animated gif from latex src blocks
@ 2022-06-17 13:21 DEBRY.Edouard
  2022-06-17 13:43 ` Fraga, Eric
  0 siblings, 1 reply; 7+ messages in thread
From: DEBRY.Edouard @ 2022-06-17 13:21 UTC (permalink / raw)
  To: Emacs-orgmode


Hello,

I would like to create an animated gif for a latex src block.

Here to fix ideas such a block :

<====================================================================================>
#+header: :file test4.gif
#+header: :exports results
#+header: :results output silent graphics file
#+header: :imagemagick yes :iminoptions -density 600 -delay 8 -loop 0 -background white -alpha remove
#+header: :fit yes :noweb yes :headers '("\\usepackage{tikz}")
#+begin_src latex
\foreach \angle in {0,10,...,360}
{
  \begin{tikzpicture}
    % fill circle and plot
    \fill[blue!50] (-1,0) arc (0:\angle:1) -- (-2,0) -- cycle;
    \fill[blue!50] plot[smooth,domain=0:\angle] (pi/180*\x,{sin(\x)}) |- (0,0);
    % draw connection
    \draw (-2,0) +(\angle:1) circle (2pt) -- (pi/180*\angle,{sin(\angle)}) circle (2pt);
    % draw axes an ticks
    \draw (-3.5,0) -- (7,0);
    \foreach \deg in {90, 180, 270, 360}
      \draw (pi/180*\deg,2pt) -- (pi/180*\deg,-2pt) node[below] {$\deg^\circ$};
    \draw (0,-1.2) -- (0,1.2);
    \foreach \y in {-1,-0.5,0.5,1}
      \draw (2pt,\y) -- (-2pt,\y) node[left] {$\y$};
    % draw plot and circle outline
    \draw plot[smooth,domain=0:360] (pi/180*\x,{sin(\x)});
    \draw (-2,0) circle (1);
  \end{tikzpicture}
}
#+end_src
<====================================================================================>

grabbed here :
https://tex.stackexchange.com/questions/291627/draw-an-animated-gif-of-trigonometry-function

When hitting [C-c C-c], it does produce a gif, but which cannot be
animated.

I downloaded another animated gif on internet which works properly
within emacs, so the issue comes most probably from the latex to pdf
generation.

I noticed that the latex header used is

<==============================================================>
\documentclass{article}
\usepackage[usenames]{color}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\pagestyle{empty}             % do not remove
% The settings below are copied from fullpage.sty
\setlength{\textwidth}{\paperwidth}
\addtolength{\textwidth}{-3cm}
\setlength{\oddsidemargin}{1.5cm}
\addtolength{\oddsidemargin}{-2.54cm}
\setlength{\evensidemargin}{\oddsidemargin}
\setlength{\textheight}{\paperheight}
\addtolength{\textheight}{-\headheight}
\addtolength{\textheight}{-\headsep}
\addtolength{\textheight}{-\footskip}
\addtolength{\textheight}{-3cm}
\setlength{\topmargin}{1.5cm}
\addtolength{\topmargin}{-2.54cm}
\usepackage[active, tightpage]{preview}
<==============================================================>

which is different from the latex header used in tex.stackexchange page
:
<========================================>
\documentclass[tikz]{standalone}
\usepackage{tikz}
<========================================>

I replaced the default header provided by emacs with this very one and
it worked. Is it possible to modify the latex header depending on the
export image format ?

Regards

___________________________________________________________________________________________________________________________________

This email and any attachments are confidential to the intended recipient and may also be privileged.
If you are not the intended recipient please delete it from your system and notify the sender. 
You should not copy it or use it for any purpose nor disclose or distribute its contents to any other person.
 

Ce courriel et ses pieces-jointes sont envoyes de maniere confidentielle et doivent etre traites avec attention.
Si vous n'etes pas le destinataire, merci de le detruire et d'en informer son auteur. 
Vous ne devez pas copier, utiliser, reveler ou diffuser son contenu a quiconque.



^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: Creating animated gif from latex src blocks
@ 2023-02-09 10:32 Edouard Debry
  0 siblings, 0 replies; 7+ messages in thread
From: Edouard Debry @ 2023-02-09 10:32 UTC (permalink / raw)
  To: emacs-orgmode


Hi,

In reply to an old thread initiated, here is how I get animated pictures
from latex src blocks.

<=========================== GIF image ===============================>

#+NAME: before-make-gif
#+begin_src elisp :lexical no :results none
(setq org-format-latex-header "\\documentclass[tikz]{standalone}")
(setq org-latex-default-packages-alist nil)
(setq org-latex-packages-alist nil)
#+end_src

#+NAME: after-make-gif
#+begin_src elisp :lexical no :results none
(custom-reevaluate-setting 'org-format-latex-header)
(custom-reevaluate-setting 'org-latex-packages-alist)
(custom-reevaluate-setting 'org-latex-default-packages-alist)
#+end_src

#+NAME: make-gif
#+header: :file test1.gif
#+header: :exports results
#+header: :results output silent graphics file
#+header: :imagemagick yes
#+header: :iminoptions -density 600 -delay 8 -loop 0 -background white -alpha remove
#+header: :headers '("\\usepackage{tikz}" "")
#+begin_src latex
\foreach \angle in {0,10,...,360} %
{
  \begin{tikzpicture}
    % fill circle and plot
    \fill[blue!50] (-1,0) arc (0:\angle:1) -- (-2,0) -- cycle;
    \fill[blue!50] plot[smooth,domain=0:\angle] (pi/180*\x,{sin(\x)}) |- (0,0);
    % draw connection
    \draw (-2,0) +(\angle:1) circle (2pt) -- (pi/180*\angle,{sin(\angle)}) circle (2pt);
    % draw axes an ticks
    \draw (-3.5,0) -- (7,0);
    \foreach \deg in {90, 180, 270, 360}
      \draw (pi/180*\deg,2pt) -- (pi/180*\deg,-2pt) node[below] {$\deg^\circ$};
    \draw (0,-1.2) -- (0,1.2);
    \foreach \y in {-1,-0.5,0.5,1}
      \draw (2pt,\y) -- (-2pt,\y) node[left] {$\y$};
    % draw plot and circle outline
    \draw plot[smooth,domain=0:360] (pi/180*\x,{sin(\x)});
    \draw (-2,0) circle (1);
  \end{tikzpicture}
}
#+end_src

#+ATTR_ORG: :width 1500 :height 600
[[file:test1.gif]]

<=========================== GIF image ===============================>



<=========================== SVG image ===============================>

#+NAME: before-make-svg
#+begin_src elisp :lexical no :results none
(setq org-format-latex-header "\\documentclass[dvisvgm]{standalone}")
(setq org-latex-default-packages-alist nil)
(setq org-latex-packages-alist nil)
#+end_src

#+NAME: after-make-svg
#+begin_src elisp :lexical no :results none
(custom-reevaluate-setting 'org-format-latex-header)
(custom-reevaluate-setting 'org-latex-packages-alist)
(custom-reevaluate-setting 'org-latex-default-packages-alist)
#+end_src

#+NAME: make-svg
#+header: :file test1.svg
#+header: :exports results
#+header: :results output silent graphics file
#+header: :headers '("\\usepackage{animate}" "\\usepackage{tikz}" "\\pgfmathsetmacro{\\pendulumswing}{40}" "\\pgfmathsetmacro{\\pendulumlength}{5}")
#+begin_src latex
\begin{animateinline}[controls, palindrome]{45}
  \multiframe{45}{rt=0+4}{%
    \begin{tikzpicture}[line width=1pt]
      \draw[dashed] (0:0) -- (90:{-\pendulumlength}) coordinate (o);
      \draw[dashed] ({90-\pendulumswing}:{-\pendulumlength}) coordinate (a)
      arc[start angle={90-\pendulumswing}, end angle={90+\pendulumswing}, radius={-\pendulumlength}] coordinate (b);
      \draw[dashed, red] (a) -- (a |- o) coordinate (c) node[below] {$-x_m$};
      \draw[dashed, red] (b) -- (b |- o) coordinate (d) node[below] {$x_m$};
      \draw[-stealth, red] ([xshift=-1cm]c) -- ([xshift=1cm]d);

      % variable \rt goes from 0 to 180
      % cos(\rt) returns a value between -1 and 1 following a (co)sine curve
      \pgfmathsetmacro{\pendulumangle}{cos(\rt)*\pendulumswing}
      \draw (0:0) -- ({90+\pendulumangle}:{-\pendulumlength})
      node[circle, fill=blue, text=white] {$\mathbf{m}$};
    \end{tikzpicture}%
  }%
\end{animateinline}
#+end_src

#+ATTR_ORG: :width 1500 :height 600
[[file:test1.svg]]

<=========================== SVG image ===============================>

The GIF generation first goes through generating a PDF file and then, converts it
afterwards to gif with ImageMagick. It only uses "tikz" latex package.

The SVG generation uses "tikz" and "animate" latex packages. This latter is initiated
with "dvisvgm" and therefore, the src block is configured to produce a svg file
("dvisvgm" again under the hood).

The GIF image shows up correctly animated in emacs, either as a plain file or as a link in
orgmode (org-inline-animate).

The SVG file is statically well displayed in emacs, but not animated. As far as I understand
the "animate" package uses javascript under the hood and indeed, the SVG file is
correctly animated in (e.g.) the chromium browser.

That is to note that for correct image generation, I had to tweak
'org-format-latex-header, , 'org-latex-packages-alist and
'org-latex-default-packages-alist orgmode variables. Setting nil for two latters is probably
a bit too harsh, but I did not have enough time to figure out which package was causing
problems.

So, as you may have guessed, before evaluating the latex block, I evaluate the first
elisp block and after, the second elisp block's evaluation reverts orgmode variables to
their previous settings.

This is a bit annoying, I wonder if there is a way to automatically evaluate both
elisp blocks when running the latex block, one before and one after. I know that some
blocks can be called within other code blocks but obviously, with the same language.

Perhaps the "#+CALL:" syntax can call several code blocks ?

#+CALL : before-gif() make-gif() after-gif()

Let me know if you find a lighter way to generate animated images from src code blocks.

Hope this helps

Regards

Edouard Debry


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

end of thread, other threads:[~2023-02-09 10:33 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-17 13:21 Creating animated gif from latex src blocks DEBRY.Edouard
2022-06-17 13:43 ` Fraga, Eric
2022-06-17 22:26   ` Edouard Debry
2022-06-17 23:04     ` Ihor Radchenko
2022-06-18  9:06     ` Fraga, Eric
2022-06-19 14:03       ` Edouard Debry
  -- strict thread matches above, loose matches on Subject: below --
2023-02-09 10:32 Edouard Debry

Code repositories for project(s) associated with this public 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).