Bei wikipedia werden oft die svg-Codes zu gezeichneten Bildern angegeben: Kann ich diesen Code irgendwie direkt mit TikZ verarbeiten? Öffne in Overleaf
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg xmlns="http://www.w3.org/2000/svg" width="1000" height="600" viewBox="0 0 5 3"> <desc>Flag of Germany</desc> <rect id="black_stripe" width="5" height="3" y="0" x="0" fill="#000"/> <rect id="red_stripe" width="5" height="2" y="1" x="0" fill="#D00"/> <rect id="gold_stripe" width="5" height="1" y="2" x="0" fill="#FFCE00"/> </svg> Öffne in Overleaf
\documentclass{article} \usepackage{tikz} \usepgflibrary{svg.path} \begin{document} IST: \begin{verbatim} <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg xmlns="http://www.w3.org/2000/svg" width="1000" height="600" viewBox="0 0 5 3"> <desc>Flag of Germany</desc> <rect id="black_stripe" width="5" height="3" y="0" x="0" fill="#000"/> <rect id="red_stripe" width="5" height="2" y="1" x="0" fill="#D00"/> <rect id="gold_stripe" width="5" height="1" y="2" x="0" fill="#FFCE00"/> </svg> \end{verbatim} SOLL: ??? \begin{pgfpicture} \pgfpathsvg{ M 0 0 l 20 0 0 20 -20 0 q 10 0 10 10 } \pgfusepath{stroke} \end{pgfpicture} \end{document} gefragt 14 Nov '18, 18:08 cis |
Zunächst installiert man sich den SVG Optimizer: Öffne in Overleaf
npm install svgo Dann führt man ihn aus mit der SVG-Datei als Eingabe (überschreibt die Eingabe): Öffne in Overleaf
nodejs /path/to/svgo-module/bin/svgo flag.svg Das resultierende SVG hat dann die Pfade, die TikZ versteht: Öffne in Overleaf
<svg xmlns="http://www.w3.org/2000/svg" width="1000" height="600" viewBox="0 0 5 3"><path d="M0 0h5v3H0z"/><path fill="#D00" d="M0 1h5v2H0z"/><path fill="#FFCE00" d="M0 2h5v1H0z"/></svg> MWE: Öffne in Overleaf
\documentclass{article} \usepackage{tikz} \usepgflibrary{svg.path} \definecolor{rot}{HTML}{DD0000} \definecolor{gold}{HTML}{FFCE00} \begin{document} \begin{pgfpicture} \pgftransformscale{-1} % Warum auch immer? \pgfpathsvg{M0 0h5v3H0z} \pgfusepath{fill} \pgfsetcolor{rot} \pgfpathsvg{M0 1h5v2H0z} \pgfusepath{fill} \pgfsetcolor{gold} \pgfpathsvg{M0 2h5v1H0z} \pgfusepath{fill} \end{pgfpicture} \end{document} beantwortet 15 Nov '18, 03:20 Henri Aja gut. Aber die Übersetzung in TikZ muss ich selbt vornehmen, wie es aussieht. Das geht höchstens noch bei solchen einfachen Beispielen.
(15 Nov '18, 11:07)
cis
|
Alternativ kann man mit der FFI von LuaTeX ≥ 1.0.3 oder LuaJITTeX und pplib das SVG direkt mit Cairo und RSVG2 rendern, siehe meine Antwort auf TeX.SX https://tex.stackexchange.com/a/408014
MWE: Öffne in Overleaf
\documentclass{article} \usepackage{luacode} \begin{luacode} local ffi = require("ffi") ffi.cdef[[ // Types typedef void FILE; typedef void GError; typedef void RsvgHandle; typedef void cairo_surface_t; typedef void cairo_t; typedef int(*cairo_write_func_t)(void *, const unsigned char *, unsigned int); typedef struct { int width; int height; double em; double ex; } RsvgDimensionData; // Conversion functions RsvgHandle * rsvg_handle_new_from_file(const char *, GError **); void rsvg_set_default_dpi_x_y(double, double); int rsvg_handle_get_dimensions(RsvgHandle *, RsvgDimensionData *); cairo_surface_t * cairo_pdf_surface_create_for_stream(cairo_write_func_t, FILE *, double, double); cairo_t * cairo_create(cairo_surface_t *); void cairo_scale(cairo_t *, double, double); int rsvg_handle_render_cairo(RsvgHandle *, cairo_t *); // Cleanup functions void cairo_destroy(cairo_t *); void cairo_surface_destroy(cairo_surface_t *); void g_object_unref(void *); ]] local lcairo = ffi.load("cairo") local lrsvg = ffi.load("rsvg-2") function svg_to_pdf(params, filename) -- Open input file local err = ffi.new("GError**", nil) local rsvg = lrsvg.rsvg_handle_new_from_file(filename, err) -- Get size of image local dimensions = ffi.new("RsvgDimensionData[1]") lrsvg.rsvg_handle_get_dimensions(rsvg, dimensions) local width = params.scale * dimensions[0].width local height = params.scale * dimensions[0].height -- Open cairo PDF canvas of same size and associate callback function local output = "" function rsvg_cairo_write_func(_, data, length) output = output .. ffi.string(data, length) return 0 -- CAIRO_STATUS_SUCCESS end local surface = lcairo.cairo_pdf_surface_create_for_stream( rsvg_cairo_write_func, nil, width, height); -- Render SVG on the canvas local cr = lcairo.cairo_create(surface) lcairo.cairo_scale(cr, params.scale, params.scale); lrsvg.rsvg_handle_render_cairo(rsvg, cr) -- Clean up if (cr ~= nil) then lcairo.cairo_destroy(cr) end if (surface ~= nil) then lcairo.cairo_surface_destroy(surface) end if (rsvg ~= nil) then lrsvg.g_object_unref(rsvg) end if (err ~= nil) then lrsvg.g_object_unref(err) end return output end \end{luacode} \newcommand\includesvg[2][scale=1]{% \leavevmode \luaexec{ local stream = svg_to_pdf({#1},[[#2]]) local identifier if status.luatex_version < 108 then _, identifier = epdf.openMemStream(stream,\#stream,[[#2]]) else identifier = pdfe.new(stream,\#stream,[[#2]]) end node.write(img.node { filename = identifier }) }% } \begin{document} \includesvg[scale=.3]{flag.svg} \end{document} beantwortet 15 Nov '18, 03:30 Henri |