That is correct, what we would be essentially doing here is searching and replacing images on the fly whenever you compile. That is what using the text tag for images lets us do. So just as one document can say “Bob” or “Chuck” depending upon your search and replacement, there is no need to duplicate the document and have a “Chuck” or “Bob” version of it.
Sure, since you can search for the image tag as text and replace it with nothing, this would work. You would need some way of identifying these special cases so that when you compile to print they would not be handled.
image_replacers.zip (115 KB)
I have attached a sample project which demonstrates all of this. In the left split you’ll find the sample book test containing three image placeholders. These are using the custom placeholder that I proposed, rather than Scrivener’s native image syntax. This way you can disregard the compile implementation details completely while writing. It’s just a figure at this point, or maybe one that doesn’t print.
In the right split, you’ll find the graphics. Note how each graphic has “-rgb” or “-bw” appended to the name. The compiler will be adding that for you, so all you have to type in the editor is the main part of the image name, as shown in the editor.
Okay, now pop open the Compile panel and have a look at the Replacements compile option pane. I’ve left it in B&W mode. Try it now, just click “Compile” and examine the print preview. You should see images “One” and “Three”, but “Two” will be omitted entirely as requested by the ‘noprint’ variant typed into the editor.
If you wish, use the “Format As” compile drop-down to select “E-book (RGB)” from the “Project Formats” section. Compile that and you’ll see three colour images. Both the !fig and !fig_noprint commands will output in e-book mode.
All right, here is how it works in Replacements (looking at RGB first): the first line is as I’ve already described in the prior post. Scrivener searches for “!fig(” and when it finds it, it starts storing text into the “$@” until it reaches “)”. It then replaces everything it just encountered, placing whatever was stored into “$@” where requested.
The second line is basically identical to the first, save that it is looking for the ‘noprint’ version of the command. Both output identically when compiled, in other words.
Switch over to the Print (B & W) preset. The first line is very much the same—the only difference is the With column, which appends ‘-bw’ instead of ‘-rgb’.
The second command is a bit trickier. We can’t use the handy ‘$@’ code because that needs a With value, but we still need to match a variable amount of information between the parentheses. We need a wildcard, basically. RegEx mode gives us wildcards, so I’ve switched that on. Here is the contents of the Replace column:
Since parentheses are significant in RegEx, we put a \ in front of them to tell the search engine we definitely want ‘(’ and ‘)’ to a part of what we are looking for. The ‘.+?’ stuff in the middle is our wildcard. The \n on the end stands for a carriage return.
Since we are removing the figure completely, we don’t want an empty line left where it was. Including the carriage return removes that now empty line.
Naturally, this will only work if a carriage return immediately follows the image—in other words it assumes normal centre aligned images on their own lines.
So yeah, it’s a bit complicated to set up (though you may be able to just export these two presets I created and import them into your project, or copy and paste the replacements between the two project’s compile panes)—but note how easy it is to switch outputs now. You can just flip the mode with the “Format As” menu and forget about it. Meanwhile it is also pretty easy to type the figures into the editor while you are writing.