Caption of image

User avatar
krysttof
Posts: 25
Joined: Fri Jan 24, 2014 9:24 am
Platform: Mac

Thu Jan 30, 2014 9:27 am Post

Hello !
Is it possible to automatically insert the file name of an image as a caption ?
Thank's for your help !
Eternel french beginner

User avatar
AmberV
Posts: 24641
Joined: Sun Jun 18, 2006 4:30 am
Platform: Mac + Linux
Location: Ourense, Galiza
Contact:

Thu Jan 30, 2014 8:32 pm Post

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:

Code: Select all

<$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:

Code: Select all

<$img:$@>
Fig. <$n:figure>. $@


When compiled, this will result in:

Image
Fig. 2. Salt Mine

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. http://en.wikipedia.org/wiki/File:Slanic_Salt_Mine.jpg
.:.
Ioa Petra'ka
“Whole sight, or all the rest is desolation.” —John Fowles

User avatar
krysttof
Posts: 25
Joined: Fri Jan 24, 2014 9:24 am
Platform: Mac

Fri Jan 31, 2014 10:43 am Post

This is just huge!
That's great !
Thank's a lot
Eternel french beginner

fr
fredster
Posts: 48
Joined: Thu Apr 11, 2013 6:25 am
Platform: Mac

Fri Jan 31, 2014 8:40 pm Post

wow, that's neat!

Ca
Cajun
Posts: 12
Joined: Tue Oct 20, 2015 1:32 am
Platform: Windows

Sat May 21, 2016 6:17 pm Post

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.

User avatar
AmberV
Posts: 24641
Joined: Sun Jun 18, 2006 4:30 am
Platform: Mac + Linux
Location: Ourense, Galiza
Contact:

Tue May 24, 2016 8:39 am Post

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 <Name of Figure>. 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.
.:.
Ioa Petra'ka
“Whole sight, or all the rest is desolation.” —John Fowles

ip
ipanini
Posts: 23
Joined: Fri Aug 08, 2014 8:38 am
Platform: Mac + Windows
Location: Belgium

Wed Nov 30, 2016 7:51 pm Post

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.

1. 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)


2. 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..
Attachments
Picture-4.png
Picture-4.png (23.83 KiB) Viewed 5066 times

User avatar
AmberV
Posts: 24641
Joined: Sun Jun 18, 2006 4:30 am
Platform: Mac + Linux
Location: Ourense, Galiza
Contact:

Fri Dec 02, 2016 1:30 am Post

(apparently yes, because they already are in the preset replacements)

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.

Or do I need to include the path in the replacement?

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:

Code: Select all

<$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.
.:.
Ioa Petra'ka
“Whole sight, or all the rest is desolation.” —John Fowles

ip
ipanini
Posts: 23
Joined: Fri Aug 08, 2014 8:38 am
Platform: Mac + Windows
Location: Belgium

Mon Dec 05, 2016 9:08 pm Post

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

Code: Select all

<$img:$@>
Fig. <$n:figure>. $@


I'm compiling for print, then I do save as PDF to preview.
Picture-5.png
Picture-5.png (43.92 KiB) Viewed 5029 times


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:

Code: Select all

<$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?

User avatar
AmberV
Posts: 24641
Joined: Sun Jun 18, 2006 4:30 am
Platform: Mac + Linux
Location: Ourense, Galiza
Contact:

Tue Dec 06, 2016 12:32 pm Post

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 (<stuff>). 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…

Can I attach my project for some helping soul to have a look at what’s wrong?

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.
.:.
Ioa Petra'ka
“Whole sight, or all the rest is desolation.” —John Fowles

tr
tressolini
Posts: 2
Joined: Tue Aug 28, 2018 4:59 pm
Platform: Mac

Tue Aug 28, 2018 5:07 pm Post

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.

User avatar
KB
Site Admin
Posts: 20920
Joined: Tue Jun 13, 2006 11:23 pm
Platform: Mac
Location: Truro, Cornwall
Contact:

Tue Aug 28, 2018 5:32 pm Post

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
"You can't waltz in here, use my toaster, and start spouting universal truths without qualification."

User avatar
AmberV
Posts: 24641
Joined: Sun Jun 18, 2006 4:30 am
Platform: Mac + Linux
Location: Ourense, Galiza
Contact:

Tue Aug 28, 2018 5:37 pm Post

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:

  1. Locate the project in Finder and right-click on it.
  2. Compress the project to create a backup alongside it in Finder.
  3. Right-click on the project again, and select “Show Package Contents”.
  4. 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 “<Replacements>” element. Find the replacement containing the problematic pattern, and remove the entire “<Replacement>” (singular) element. Save the file and reload the project.
.:.
Ioa Petra'ka
“Whole sight, or all the rest is desolation.” —John Fowles

tr
tressolini
Posts: 2
Joined: Tue Aug 28, 2018 4:59 pm
Platform: Mac

Wed Aug 29, 2018 8:37 am Post

AmberV wrote:To repair the project in question:

  1. Locate the project in Finder and right-click on it.
  2. Compress the project to create a backup alongside it in Finder.
  3. Right-click on the project again, and select “Show Package Contents”.
  4. 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 “<Replacements>” element. Find the replacement containing the problematic pattern, and remove the entire “<Replacement>” (singular) element. Save the file and reload the project.


This was just brilliant--an easy fix that solved my problem entirely. Thanks!