exporting/extracting styled text

I created a style which I’ve applied to certain text strings in my book.

I would like to produce a listing of all (and only) the text that has been tagged with that particular style.

Is that possible? If so, how?

A challenge! But in fact, this is a great example of the sort of ad hoc thing the new Compile + Styles system can do.

When Compiling, you can set text of a particular style to be deleted. Now, in your case, you want the inverse of this, but you can do that by creating a style in Compile that is set to be deleted, and have Compile apply that to all other text except the text that has been assigned styles in your project. The general method is:

  1. Set up Compile to apply a style to all other text.

  2. Set that style to be deleted.

  3. Add some sort of spacing prefix/suffix around other styled text to ensure it doesn’t run into one another after the deletion of everything else. (Only necessary if you are using character styles - not necessary if all of your styled text uses paragraph styles, which will appear on their own paragraphs anyway.)

So, in Compile:

  1. Create a new Compile Format.

  2. In the “Styles” area, create a new Paragraph style called e.g. “Deleted Text” and tick the “Delete text of this style” box.

  3. In “Section Layouts”, delete all Section Layouts except for “Text Section”.

  4. For the “Text Section” Section Layout, tick “Override text and notes formatting”.

  5. Apply the “Deleted Text” style to the preview text in the box above the “Override…” checkbox.

  6. Save the Format (giving it a name such as “Styled Text Only”).

  7. Click on “Assign Section Layouts…” and assign the “Text Section” layout to all Section Types in the project.

  8. Compile.

The compiled document will delete all text except for text you didn’t apply a style to (which will have been assigned the “Deleted Text” style during Compile).

If you’re only using paragraph styles, that’s all you’ll need to do, because each piece of styled text appears in its own paragraph. If you have character styles as well, though, the text of those will likely run into each other after you’ve deleted surrounding text. To fix that up, you will want to edit the “Styles” in your Compile Format and add all of the styles in your project, then add a space or newline suffix.

All the best,

Addendum: I misread and it seems that you only want one particular style included, and all others removed. In that case, you will have to add other styles you use in your project to the “Styles” area of the Compile format and tick “Delete text of this style” for those, too, so that all are deleted except the one you want to keep.

All the best,

Haven’t been able to get this to work yet, but I’ll keep trying.

Most of my chapter text in the editor appears as “No Style” when I click within it, except for the particular text strings to which I’ve applied the style I need to list out.

So what I’m not sure how to do is add that “No Style” to the Styles list, so that I can indicate “Delete text of this style”. “No Style” doesn’t appear as an option. (I tried creating a style named “No Style” and indicated that style text should be deleted, but that didn’t work, and I might guess why.)

In the Section Layouts, I have only the Text Section, with Notes and Text “ticked”. I ticked the “Override text …formatting” in the pane below, selected all the text within there, and tagged it with the “Deleted Text” style and saved it. But that’s not doing the trick.

Did you make sure that the “Text Section” Section Layout was assigned to the necessary Section Types in your project using Assign Section Layouts?

The instructions I provided above should work fine as long as you followed all the steps. The “Deleted Text” style defined in Compile gets applied to all text with “No Style” assigned in the project: you assign “Deleted Text” to the text and notes in the “Text Section” section layout, and then you assign the “Text Section” layout to the necessary Section Types in your project. This will apply the “Deleted Text” style to all text with “No Style” and thus delete it.

I’ve attached a sample project containing a “Delete ‘No Styles’ Text” Compile Format that was created following the instructions I provided. If you compile it, you will see that all text except for styled text is deleted.
StylesDeletion.zip (71.2 KB)

Thanks for the test project. I can see that works, but the difference I noticed was your text was tagged with paragraph styles.

I created a Character Style (called Date), which I’ve applied to date strings (e.g., Nov. 11, 1956) which occur within No Style text.

What would I need to do with this Date style in the compile phase?

And how do I specify a “newline” in the suffix box?

For this you’ll need to add your Date style to the “Styles” list in Compile, then, in the right text field under the Prefix/Suffix setting for that style, hold down Option and hit return. (On macOS, if you hold down Option, you can enter return or tab into a text field that would otherwise end editing when you hit these characters.)

You’ll also want the “Deleted Text” style to be a character style in this case (rather than a paragraph style), because otherwise the “Deleted Text” paragraph style will be added to all text that has no paragraph style, including text that has a paragraph style but no character style, so it will end up deleting your dates if they don’t have a paragraph style associated with them.

You will probably also want to replace any double return characters using “Replacements” to ensure that you don’t end up with unwanted blank lines between dates. A good way of doing this is to use a RegEx search looking or “[\n]+” (multiple newlines) and replacing it with a return character (hold down Option and hit return in the “With” field).

I have attached another sample project showing all of this. This has a “Date” style that is applied to some dates in the project text. When you compile using the “Dates Only” format these dates are output as a list and everything else that doesn’t have a character style associated is deleted.

Note: if you are using any other character styles in your text, you will need to add them to the “Styles” list in the Compile Format too, and set them to be deleted.

“You’ll also want the “Deleted Text” style to be a character style in this case (rather than a paragraph style), because otherwise the “Deleted Text” paragraph style will be added to all text that has no paragraph style, including text that has a paragraph style but no character style, so it will end up deleting your dates if they don’t have a paragraph style associated with them.”

I didn’t find the attachment mentioned in your lastest response, but your explanation makes sense and matches the result that I was seeing, that my character-styled text was being deleted as well.

Thanks a lot. (I’ve only had Scrivener less than a month, and have a lot to learn, but it’s already helped tremendously.)

Huh, strange - I don’t know why it didn’t show up, although I did have a page reload issue so that might be it. Hopefully it works this time. The things to note in the “Dates Only” Compile Format are:

  1. “Deleted Text” is now a character style.
  2. The “Date” style has been added to the Compile “Styles” list. I’ve changed its formatting slightly (removed the bold and colour of the style as it appears in the main text) and, although you cannot see it, there is a return character in the “Suffix” box (the right text field under “Prefix/suffix:”).
  3. The “Deleted Text” character style has been applied to the text and notes in the “Text Section” Section Layout.
  4. Under “Replacements”, I have a RegEx replacement set up to replace “[\n]+” (multiple return characters) with a single return character (again, you cannot see the return character in the “With” field, but it is there).

No problem, and I’m glad Scrivener is proving useful. (This was quite a fun challenge because it’s not the sort of thing that’s ever come up before or a common thing to need to do, so it’s been a good test of some of Scrivener’s new Compile features.)

All the best,
StylesDeletion2.zip (66.6 KB)