3

I'd insert an equation within a figure as shown in the following image. enter image description here

I'd like also to visualize the number that identifies the equation.

Is it possible to obtain what I've written?

2
  • The figure is formatted immediately and stored in a savebox. The equation number will be set then, although the \label (\protected@write) will be delayed until the page where the figure shows up. Jul 5, 2021 at 23:34
  • This is virtually the same as Drawing on an image with TikZ.
    – Werner
    Jul 5, 2021 at 23:41

1 Answer 1

4

If you save the equation in a boxed minipage \sbox2, you can \stackinset it into the figure.

\documentclass{article}
\usepackage{stackengine,graphicx}
\begin{document}
\begin{equation}
E=mc^1
\end{equation}
\begin{figure}[ht]
\sbox2{\begin{minipage}{100pt}
\begin{equation}
E=mc^2
\end{equation}
\end{minipage}}
\stackinset{r}{20pt}{t}{20pt}{\copy2}
  {\includegraphics[width=\textwidth]{example-image}}
\caption{My caption}
\end{figure}
\begin{equation}
E=mc^3
\end{equation}
\end{document}

enter image description here


UPDATE

At the urging of Don in comments below, I have updated the stackengine package so that one may bypass the creation of the intermediate box2. Instead, one may place the content directly into the \stackinset without adverse effects on the equation counter. It should propagate in a few days, I hope. It is v4.1 2021-07-15

\documentclass{article}
\usepackage{stackengine}[2021-07-15]
\usepackage{graphicx}
\begin{document}
\begin{equation}
E=mc^1
\end{equation}
\begin{figure}[ht]
\stackinset{r}{20pt}{t}{20pt}
{\begin{minipage}{100pt}\begin{equation}
 E=mc^2
 \end{equation}\end{minipage}}
{\includegraphics[width=\textwidth]{example-image}}
\caption{My caption}
\end{figure}
\begin{equation}
E=mc^3
\end{equation}
\end{document}
4
  • Hmm, it appears that \stackinset typesets its contents 5 times? I thought the whole \sbox\copy smelled a bit odd when I saw it, but I thought to try it modified before commenting—is there some compelling reason to keep re-interpretting the argument of \stackinset? I haven't had a chance to look at the code yet.
    – Don Hosek
    Jul 6, 2021 at 3:23
  • Ah, looked at the code. It looks like, at the very least, you should have your code save the contents of #5 and #6 in boxes rather than macros. It's a lot faster for TeX to do a \copy\somebox than to re-parse the tokens from #5 and #6 repeatedly. It's kind of expensive to look in graphics files to get the dimensions, not to mention side effects if there are numbered structures in one of the arguments. If you like, I can give you a PR with my suggested changes.
    – Don Hosek
    Jul 6, 2021 at 3:33
  • @DonHosek Thank you for the suggestion, and perhaps I should effect that change. However, I do seem to recall looking into that sort of thing at one point and someone of repute noted to me that if you load the same \includegraphics multiple times, it only goes through the process the first time, and re-uses information for subsequent calls. Nonetheless, your argument applies even to non-graphical load (such as \begin{equation})s, so I should consider it for that reason alone. Thanks for the reminder! Jul 6, 2021 at 8:55
  • 1
    @DonHosek Thanks again for your comment that spurred me on to update the package. I have just submitted an update (from v4.01 to v4.1) of stackengine. Not only does it only evaluate the arguments but once, both for efficiency and so that counters action in the argument doesn't get out of sync, but I also improved the ability to perform nesting (which was sadly deficient for stacks involving field separators, rather than explicit arguments). It should distribute in a few days, I suppose. Jul 15, 2021 at 14:39

Not the answer you're looking for? Browse other questions tagged .