Hello !
Is it possible to automatically insert the file name of an image as a caption ?
Thank’s for your help !
This could be accomplished with some creative use of Scrivener’s placeholder and replacement system. Images can be referred to with placeholders, such as in the following example, where the image is named “Libraries of the World” in the Binder:
<$img:Libraries of the World>
We can make things easier for ourselves with Compile Replacements. These are basically just search and replace directives that run during compile, without altering the original text. One thing they can do is “capture” variable text and then use that text in the replacement value. Here is an example that would ease typing in the above:
Replace: <$@>
With: <$img:$@>
The $@
is special, it means Scrivener should just start storing text as it scans to the right, until it finds the character following—in this case, >
. We can then use that same token to print whatever was stored in the replacement. <Salt Mines>
becomes <$img:Salt Mines>
when you compile, which in turn triggers Scrivener to look for a picture named “Salt Mines” in the Binder and insert the first one it finds by that name into the text.
Now all you have to do is extend that principle further to also print a caption. Using the above Replace string, we can alter the With string to:
<$img:$@>
Fig. <$n:figure>. $@
When compiled, this will result in:
Fig. 2. Salt Mines
If you don’t want them numbered, you can just put “$@” on the second line by itself (hint, you can just copy and paste into the With field, but if you want to type it in, hold down the Option key when pressing Return to insert a carriage return).
Some things of note:
- Images and their captions will take on whatever formatting you use on the placeholder in the editor. So if you want italicised captions, make sure to italicise (and probably centre-align) the placeholder.
- The numbering token as I provided it will count figure numbering separately from anything else. So if you are using the ‘n’ counter for chapter numbers or something, it will not conflict.
- For your custom replacement code, make sure to use something that will not otherwise be used in an image name, for the terminator after the
$@
code. For example if you for whatever reason use “>” in an image caption then you should choose something else to enclose the image name—or of course if this pattern is used elsewhere in your writing.
Image source Wikipedia. en.wikipedia.org/wiki/File:Slanic_Salt_Mine.jpg
This is just huge!
That’s great !
Thank’s a lot
wow, that’s neat!
I have followed the instructions and am not able to make this work.
Below each image:
<$@>
The output is:
<$img:$@> Figure 1. $@
<$img:$@> Figure 2. $@
etc.
I created a test project with text and four images. The counter works fine, but Scrivener is not pulling in the name of the image. The images are in a file folder called images, which resides in the Research folder.
Any help would be appreciated.
From how I’m reading this, it sounds as though you’ve typed <$@> in the text editor below each image, but with the described method the image wouldn’t even be in the text editor, and you would be typing in the file name of that image between the brackets, like . The compiler then takes that simple code and expands it into two separate codes, one that creates an image placeholder code (which the compiler then subsequently uses to insert the actual image) and the second that builds the caption line, using the name of the figure as the text.
With what you are doing, you are giving Scrivener no information to do that. You are typing “$@” between the brackets, which is a code that only works in Replacements. It ignores that sequence of characters if it exists within the replacement text, meaning you are asking for a file called “$@” and supplying “$@” as the visible caption that people read.
Hi,
I may be late to the ball…
I’m trying to use this in my project too.
I’m not sure if I fully understand.
-
Do I understand correctly that the ‘replace’ clause and the ‘with’ clause need to be entered in the compile > replacements section? (apparently yes, because they already are in the preset replacements)
-
Can this work with external images? Not residing in the binder?
The image name would then also hold the ‘PATH’.
Like this example in the manual: <$img:~/Dropbox/MyBookFolder/scene_break.png>
Or do I need to include the path in the replacement?
I just don’t get it yet…
Yes, the solution is a trick for how to use Replacements, but to be clear what is in the compile preset you posted isn’t really what was being discussed above. That doesn’t do anything with the image itself, it merely prints a serial number with a label tag so the number can be referred to elsewhere in a cross-reference. The concepts are very similar though. Look for X, replace it with Y—where presumably X is something easy to type in and Y is a longer code that you don’t want to repetitively type in or see all of the time.
That’s right, unless you want the full path of the image to be part of the visible caption itself that the reader sees below the image, that path needs to be part of the “code” they don’t see, or the Replacement.
You’ll probably want the filename extension to be hidden too. That means all of your figures need to be in a predictable location and probably all using the same extension (otherwise you’ll need multiple replacements to handle variations). For example, replace with:
<$img:~/Dropbox/MyBookFolder/$@.png>
Fig. <$n:figure>. $@
That said, I’m not sure if you want a visible caption on something like a scene break, maybe this full example isn’t the best to be following.
Ok, I’m not getting anywhere. Thought I understood to the larger extent, but apparently not since it is not working at all.
If this isn’t the best example to follow, is there a ‘good’ example available elsewhere?
I’ve created a small project to try this out.
Compile format = Custom,
Project Replacements > replace <$@> with
<$img:$@>
Fig. <$n:figure>. $@
I’m compiling for print, then I do save as PDF to preview.
What happens is:
I think meta-data present in the header setting is replaced with the project replace items.
And the image is not visible at all??
The document snippet that should serve to represent my image contains this text:
<$Img:/Volumes/data/Pictures/for scrivener TFT report/20160908/20160908_183115.1.jpg;h=600>
"CIP stroom zoekt uitweg via ontluchting"
“Between parenthesis is the caption that should be below the picture.”
Can I attach my project for some helping soul to have a look at what’s wrong?
Ah I see, I hadn’t anticipated that particular combination of uses in my earlier example. What you are running into is a clash between the built-in syntax used by Scrivener for its placeholders (<$stuff>) and the shorthand syntax in the example (). The replacement is finding the <$surname> tag and storing “$surname” for use in printing as the image file name and caption.
The solution is simple: use something other than angle brackets to denote figures. Anything will do, so long as it doesn’t clash with anything else. –Figure–, {Figure}, %Figure%, whatever you wish.
As to the example image placeholder code you posted, I’m not sure if it works with a capital “I” like that, it should be <$img… not <$Img…
Sure, just keep in mind it is a public forum so make sure all personal info is removed. To attach, zip the project as Scrivener projects are actually folders with many files in them.
Hi,
Now I might be late to the game, but when I entered the replacement placeholders in my Compile settings, the project just took forever to compile. Finally, I ended the compilation, but now nothing happens whenever I press Compile(!). New projects open the Compile window just fine–that is, until I enter the replacement placeholders in the new project again, in which case the same problem ensues… I’ve tried clean reinstalls, but unless someone here has a solution for me, I’m afraid I’ll have to copy all my old files into a new project.
So while I would love for this to work, I would caution others to try this setting in a new, empty project first, to make sure it works.
This sounds as though your project is now consistently triggering an internal error. Could you please zip it up and send it to us at Mac.support AT literatureandlatte.com for testing? I’d like to see what is causing the error and fix it.
Thanks and all the best,
Keith
Thanks, that looks like a fairly easy to trigger bug. In a blank test project with no changes made to it, other than to add the following replacement pattern to the Project Replacement tab, I get an exception or an indefinite hang when trying to compile. If instead I save the compile settings without compiling, and then later try to open the compile pane, I get the hang again.
Replace: <$@>
With: ($@)
To repair the project in question:
- Locate the project in Finder and right-click on it.
- Compress the project to create a backup alongside it in Finder.
- Right-click on the project again, and select “Show Package Contents”.
- Drill down into the Settings folder, and then do either of the following:
- Move the compile.xml file out of the folder. This will reset all of the compile settings for the project.
- If that is too destructive for you, and you don’t mind editing an XML file, then open the file in a plain-text editor (TextEdit will do, just make sure it stays in plain-text mode), and search for the “” element. Find the replacement containing the problematic pattern, and remove the entire “” (singular) element. Save the file and reload the project.
This was just brilliant–an easy fix that solved my problem entirely. Thanks!