redetho logo

Documentation about EMACS' Graphics Support

SVG Support

SVG Logo

"Out of the box" Support

Since EMACS version ‑tbf‑, EMACS can display Scalable Vector Graphics (SVG) files out of the box. Using C-C C-C, you can switch between showing the graphics and showing the underlying SVG file. For the latter ‑ as SVG is pure XML ‑ the nXML mode is used. nXML mode allows you to edit the SVG file as you normally would edit any XML file.

Below, I documented three options to create and edit SVG files using EMACS:

Issues

As I write this, I use EMACS version 25.1.1 under MS Windows 10 Pro 21H2. I have observed the following issues, when viewing SVG content in EMACS:

Create and Manipulate SVG Files Using ELISP and svg.el

The ELISP package svg.el is due to Lars Magne Ingebrigtsen and Felix Klee. It is part of EMACS as from version 26.1.

The main purpose of svg.el is to programmatically create and manipulate SVG files using EMACS LISP.

In year 2014, Lars published the video https://www.youtube.com/watch?v=FWQB_9QcGI0. It show EMACS LISP code and below SVG file content, that changes in real-time (or may be after a key stroke). See also https://emacs.stackexchange.com/questions/46305/svg-in-realtime

Regards the LISP functions, that svg.el provides, see https://www.gnu.org/software/emacs/manual/html_node/elisp/SVG-Images.html

Example

The following grey circle SVG graphics was created using ELISP:

‑ Text before image is in a separate paragraph ‑

testSVG.el.svg ‑ Text immediately after image is in the same paragraph that contains the SVG ‑

The ELISP source code had been embedded in an org-mode document like so:

‑ Text before image is in a separate paragraph ‑
#+BEGIN_SRC emacs-lisp :exports results :file testSVG.el.svg 
    (let ((svg (svg-create 400 400 :stroke-width 10 :stroke "green")))
      (svg-circle svg 200 200 100 :fill-color "gray" )
      (with-temp-buffer
        (svg-print svg)
        (buffer-string)))
#+END_SRC
#+RESULTS:
[[file:testSVG.el.svg]]
‑ Text immediately after image is in the same paragraph that contains the SVG ‑

Hence the SVG graphics can be changed by changing the ELISP source code. Note the :exports results setting: it suppresses the export of the ELISP source code when exporting to HTML code. The default is to show the ELISP code in the export.

Draw SVG Shapes Using canvas-mode

The purpose of canvas-mode is to create and edit SVG in EMACS by using the mouse and draw simple shapes on a canvas.

Anand Tamariya published under https://gitlab.com/atamariya/emacs/-/blob/dev/lisp/svg.el an extended version of svg.al, that adds canvas-mode. Anand proposed in https://lists.gnu.org/archive/html/emacs-devel/2021-09/msg00798.html to have canvas-mode added to the built-in svg.el.

I had tested Anand's version from 2022-04-30 and found the following issues:

Usage of canvas-mode

Store svg.el at a location which is in the EMACS load path. I chose xyz/emacs-25.1\share\emacs\site-lisp. xyz is the directory where all EMACS files are stored.

Then in your init file do

(require 'svg)

The follwing section is a copy from http://lifeofpenguin.blogspot.com/2021/08/scribble-notes-in-gnu-emacs.html

If you want to edit an existing SVG file, run C-u M-x canvas-mode.

Draw SVG Shapes Using sketch-mode

sketch-mode was created by D.L. Nicolai. sketch-mode.el is available at https://github.com/dalanicolai/sketch-mode

sketch-mode also uses svg.el and has a similar purpose as canvas-mode. I have not yet tested sketch-mode


Last change: 2022-05-22
© 2002-2022 Dr. Thomas Redelberger redethogmx.de