3

Is there a possibility to place text on a svg vector image without using Inkscape?

For me it is much more convenient to change the text quickly using \overpic and \put than opening Inkscape and generating the pdf_tex file every time.

Or is that just the only way to do it? Thanks!

ro-bb
  • 63
  • 1
    See https://tex.stackexchange.com/questions/171483/mathematical-formulas-on-a-graph-not-made-by-tex The answer there uses \stackinset of the stackengine package. – Steven B. Segletes Sep 10 '19 at 11:47
  • Brilliant! This works for me: \stackinset{l}{20pt}{b}{20pt}{test}{% \includesvg[width=0.15\linewidth, svgpath=images/]{test-svg} } The only thing is that relative placements (relative to the image size) are not possible as far as I understood the documentation. So I have to adjust the text every time I change the scaling. Do you wanna add your anser as an Answer @Steven – ro-bb Sep 10 '19 at 12:29
  • Where can I find an example svg image to use in my MWE? – Steven B. Segletes Sep 10 '19 at 12:41
  • I created one on draw.io Do you need an actual download link? It should work with an arbitrary SVG file – ro-bb Sep 10 '19 at 12:47
  • Well, I haven't used inkscape, and I can't find anything on the web that identifies as an svg. – Steven B. Segletes Sep 10 '19 at 12:48
  • http://draw.io is an online sketch tool that let's you easily export SVG files :) – ro-bb Sep 10 '19 at 13:03
  • You don't have to open Inkscape and generate the .pdf_tex file, the svg package can do that for you. Though you would of course have to edit the .svg file (or the generated .pdf_tex file) in order to change the text. (I don't really see how having to find the correct coordinates by hand and having to change them every time the scaling of the image is changed is less hassle than just placing the text in the .svg file, which has to be created (i.e. edited) at some point, anyway.) – schtandard Sep 10 '19 at 13:21
  • For me it's that Inkscape is super slow and too overloaded for simple sketches I have to do. That's why I'm using http://draw.io and I'm quite happy with the workflow. I used JPG images before with overpic, that wasn't much slower than writing a normal paragraph. With Steven's solution I have the relative placement as well, so everything is perfect :) And nobody want to generate the .pdf_tex files manually. – ro-bb Sep 10 '19 at 13:33

3 Answers3

3

Here, I use \stackinset to achieve the goal (stackengine package)

I provide \scaledinset in response to the OP's comment. Thus, rather than #2 and #4 being physical x and y distances, they represent a fraction of the overall width and height of the graphical object.

I also use a standard graphic, rather than an svg, for this demonstration.

\documentclass[standard]{letter}
\usepackage{svg}
\usepackage[usestackEOL]{stackengine}
\usepackage{graphicx}
\newcommand\scaledinset[6]{%
  \setbox0=\hbox{#6}%
  \stackinset{#1}{#2\wd0}{#3}{#4\ht0}{#5}{#6}
}
\begin{document}
\scaledinset{l}{.5}{b}{.7}{test}{\includegraphics[width=0.15\linewidth]{example-image}}

\scaledinset{l}{.5}{b}{.7}{test}{\includegraphics[width=0.45\linewidth]{example-image}}
\end{document}

enter image description here

  • I had to add a % after \stackinset{#1}{#2\wd0}{#3}{#4\ht0}{#5}{#6} to not mess up the vertical alignment, as I explain in my answer: https://tex.stackexchange.com/a/683398/128042 – Jakob Apr 20 '23 at 14:11
  • 1
    @Jakob A % indeed will prevent a space from being added by an end-of-line in source code. – Steven B. Segletes Apr 20 '23 at 16:22
1

My answer is essentially the same as the answer from Steven B. Segletes, but I've added a % after \stackinset{#1}{#2\wd0}{#3}{#4\ht0}{#5}{#6}. Without this % the defined command \scaledinset always inserts a new line after the image, which can be very annoying.

\documentclass[standard]{letter}
\usepackage{svg}
\usepackage[usestackEOL]{stackengine}
\usepackage{graphicx}
\newcommand\scaledinset[6]{%
  \setbox0=\hbox{#6}%
  \stackinset{#1}{#2\wd0}{#3}{#4\ht0}{#5}{#6}%
}
\begin{document}
\scaledinset{l}{.5}{b}{.7}{test}{\includegraphics[width=0.15\linewidth]{example-image}}

\scaledinset{l}{.5}{b}{.7}{test}{\includegraphics[width=0.45\linewidth]{example-image}} \end{document}

With the code from Steven B. Segletes one loses alignment, while one can keep the alignment by adding the % as shown in the following example:

\documentclass[standard]{report}
\usepackage{subcaption}
\usepackage{svg}
\usepackage[usestackEOL]{stackengine}
\usepackage{graphicx}
\newcommand\scaledinset[6]{%
  \setbox0=\hbox{#6}%
  \stackinset{#1}{#2\wd0}{#3}{#4\ht0}{#5}{#6}%
}
\newcommand\scaledinsetOld[6]{%
  \setbox0=\hbox{#6}%
  \stackinset{#1}{#2\wd0}{#3}{#4\ht0}{#5}{#6}
}
\begin{document}
\begin{figure}
     \centering
     \begin{subfigure}[b]{0.3\textwidth}
         \centering
         \includegraphics[width=\textwidth]{example-image}
     \end{subfigure}
     \hfill
     \begin{subfigure}[b]{0.3\textwidth}
         \centering
         \scaledinset{l}{.3}{b}{.7}{test new code}{\includegraphics[width=\textwidth]{example-image}}
     \end{subfigure}
     \hfill
     \begin{subfigure}[b]{0.3\textwidth}
         \centering
         \scaledinsetOld{l}{.3}{b}{.7}{test old code}{\includegraphics[width=\textwidth]{example-image}}
     \end{subfigure}
\end{figure}
\end{document}

enter image description here

Jakob
  • 993
0

There is a way to use overpic as well - add the SVG figure, and after the SVG include but before the caption part, add another overpic figure with a transparent line. Then you can add text to this overpic figure as you wish...

E.g.,

\begin{figure}[h]
\includesvg[inkscapelatex=false,width=\linewidth]{figures/xxx.svg}
\begin{overpic}[width=\linewidth,height=0.001in]{figures/blank.png}
\put(20, 20) {Blah blah!}
\end{overpic}
\caption{\label{fig}Blah blah blah!}
\end{figure}
Optmly
  • 1