# Previewing an SVG generated by METAPOST

With v1.8, METAPOST allows one to create an SVG --- unfortunately, there aren't any TeX / code editors which have SVG previewers as far as I can tell.

Found:

but it seems to be broken in Windows 10 --- one has to switch to its window and press F5 --- is there an updated version of this program, or some other easy way to preview an SVG?

Cobbled together this file:

outputtemplate := "%j-%c.svg";
prologues := 3;
outputformat := "svg";
beginfig(1);
z0 = (0,0); z1 = (60,40);
z2 = (40,90); z3 = (10,70);
z4 = (30,45);

draw z0..z1..z2..z3--z4--cycle
endfig;
end


which makes an SVG.

• Are you looking for something that automatically updates the preview when the file changes? Otherwise you could just open your file in a web browser. – siracusa May 9 '19 at 19:08
• I tried Google Chrome, and it wouldn't reload the SVG from the local filesystem automatically --- also, file-monitor at least has controls for zooming in/out. – WillAdams May 9 '19 at 19:10
• This question seems to be off-topic because it is not about METAPOST but about SVG viewer features - maybe it is a better fit at softwarerecs.stackexchange.com ? – Marijn May 9 '19 at 20:56
• But as a suggestion you might try a browser extension for auto-refreshing tabs, to reload your svg every couple of seconds. Or use the Opera browser, which has this functionality built-in. – Marijn May 9 '19 at 21:02

The following html file will preview an SVG in a browser window, refreshing once per second. I've hardcoded it to use test.svg in the same folder, but in real use I'd programmatically generate the html, perhaps by using sed -i 's|test.svg|\$REALFILE| svg_preview.html' where svg_preview.html is the file below. You could of course (over-)write the html file at compilation time.

<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<meta http-equiv="refresh" content="1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SVG preview Chris H</title>
<img src="test.svg">
<body>
</body>
</html>


I tested this by creating my svg in Inkscape, and it updates when I save, as expected.

A simple webpage can't watch a local file for changes - perhaps that could be done in javascript but I've gone for a simple approach.

• This requires the browser to render the svg and whilst inkscape can render most variants browsers may not notoriously my windows explorer and edge fail on many svgs that are viewable in inkscape. What was IMHO better was to iframe an embedded application like Adobe SVG handler However MS moved away from such behaviours as a security risk. anyway +1 for a simplified MWE Minimal Working Entry – user170109 May 10 '19 at 13:47
• @KJO that's interesting to hear. I use firefox (95%+) and that seems to render all the SVGs I come across with no obvious issues. My limited experience of Chrome is the same, but in neither case do I claim there are no subtle issues. – Chris H May 10 '19 at 15:20
• ~The simple test file in my answer WILL render in Internet explorer or Edge but more complicated examples may on occasion not render then I need to open in Inkscape OR now I can use my standalone.tex shortcut – user170109 May 10 '19 at 15:26
• ... (after testing) Firefox uses the page margins as its bounding box, but otherwise handles some rather nasty SVGs at least as well as inkscape, and quicker in the case of 20MB simulation meshes (inkscape even on a fast machine takes a few minutes to open them). FF and Inkscape use different font subsitutions for a font that's missing here, and inkscape wins slightly – Chris H May 10 '19 at 15:27
• Agreed your examples are stable enough SVG structures in both MS browsers the ones I previously had issues with were from packages such as QTgrace – user170109 May 10 '19 at 15:43

The linked viewer is nicely lightweight an if refreshing on compile was working (I confirm it did not for me) would be similar to TeX PDF viewer functionality, so Inspired by Chris H's answer You have such ability in most TeX editor configurations (Those that already work with SVG in the background, using Inkscape or whatever)

The rudimentary elements for a "viewer" simply add the outputname from metapost as the input name for an extra pdflatex pass (the same as you would for makeindex etc) (below I show how this could be run as an external command which could be added to the editor compile commands same as any other) see

test.svg (from question https://tex.stackexchange.com/a/474637/170109)

<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100">
<style>
path {
fill: none;
stroke: black;
stroke-width: 3;
}
</style>
<path d="M52.25,14c0.25,2.28-0.52,3.59-1.8,5.62c-5.76,9.14-17.9,27-39.2,39.88" />
<path d="M54.5,19.25c6.73,7.3,24.09,24.81,32.95,31.91c2.73,2.18,5.61,3.8,9.05,4.59" />
<path d="M37.36,50.16c1.64,0.34,4.04,0.36,4.98,0.25c6.79-0.79,14.29-1.91,19.66-2.4c1.56-0.14,3.25-0.39,4.66,0" />
<path d="M23,65.98c2.12,0.52,4.25,0.64,7.01,0.3c13.77-1.71,30.99-3.66,46.35-3.74c3.04-0.02,4.87,0.14,6.4,0.29" />
<path d="M47.16,66.38c0.62,1.65-0.03,2.93-0.92,4.28c-5.17,7.8-8.02,11.38-14.99,18.84c-2.11,2.25-1.5,4.18,2,3.75c7.35-0.91,28.19-5.83,40.16-7.95" />
<path d="M66.62,77.39c4.52,3.23,11,12.73,13.06,18.82" />
</svg>


MWE

% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode
%%% BEGIN MWE Example
%\documentclass[12pt,a4paper]{memoir} % for a long document
%\documentclass[12pt,a4paper]{article} % for a short document
\documentclass{standalone} % for a one off
% Don't forget to read the manual: svg.pdf for options
\usepackage{svg}

\begin{document}
\includesvg{test.svg} %change the name as required
\end{document}


Or for the adventurous run as TeX direct from the command line and let the default handler render as a viewer

pdflatex -shell-escape \documentclass{standalone} \usepackage{svg} \begin{document} \includesvg{test.svg} \end{document} & standalone.pdf


That one liner can be improved by scripting (windows bat cmd or lnk) the input filename to be a system parameter eg %1 and used as a DragON-drop (Tm:-) shortcut.

• I'm trying to work up a system where I create a PDF using lualatex, and SVGs with MPOST.exe directly --- I just want the option of examining/verifying the SVGs easily. – WillAdams May 11 '19 at 10:06

Asking on the METAPOST list suggested:

Geeqie

which unfortunately doesn't work on Windows.

But searching for an alternative to that led to:

https://nomacs.org/

which seems to work quite well.

• nomacs on my machine can't open the samples on this page and says it needs to update but downloading update hangs tried alternative newer download that also does not work on this machine. I would suggest if going the non tex route try Scribus, Gimp or many similar However the TeX related converters are inkscape & imagemagik ignore GS own svg support e.g. mupdf (its still poor) – user170109 May 10 '19 at 19:42
• Inkscape doesn't work well --- refreshes things alarmingly, and won't open maximized. Since nomacs seems to be working on my machine, I'm going to stick with it for the time being. – WillAdams May 11 '19 at 10:04