Insert an Image as marginfigure

It would help a lot to know what kind of technology you use to generate LaTeX code out of Scrivener, since that isn’t something it does natively. But from the description you provided, it sounds like you use perhaps a form of Markdown? If you are using MultiMarkdown or Pandoc, then you need to “escape” your raw LaTeX code, otherwise they will do it for you, assuming you aim to be writing a book about LaTeX, where you would want to be able to talk about these codes.

A while ago I described how I make call-out boxes with LaTeX and MMD. There are some practical examples given a few posts down.

Otherwise you have the right idea I think, though if you are using Pandoc, I’d consider designing an output filter for yourself that adjusts how it generates LaTeX code at the source. I’m not sure if your approach will result in a figure inside of a figure environment or not.

That’s up to the system you are using to generate LaTeX code. With MMD (and I believe any CommonMark compatible parser) that would be:

[code]Caption of the image

It’s worth noting Scrivener uses this notation by default, though lacking an input for “label”, it uses the graphic’s filename as the ID:

[code]![Caption of the image][name_of_file]

[name_of_file] name_of_file.jpg[/code]