Conditional Text within a single file

Summary: Is there any way I can mark sections of text that are conditionally included when compiling? This would allow the same source file(s) to be compiled with different parameters and produce different outputs depending on what’s selected for compiling. [I am using the Windows version]

Details: I would like to have multiple versions of a book – one that is an “adult” version, one that is a toned-down adult version, another more appropriate for a younger audience, and so on. The specific use case in mind will be to allow for differences in small portions of the dialog between some of the characters, but the chapters, scenes, and overall flow will be largely identical in all these different variations.

I know I can have multiple versions of files in the binder for this and use the compiler to select which files are appropriate for which version. But replicating an entire scene/file simply to change a few dozen words is very cumbersome – and trying to keep all the other parts of the files in question synchronized will be error-prone.

Having separate binder items that contain only the dialog variations is also possible, but that will only introduce another configuration mess; the number of files I’d have to manage will explode, and many files will only have a single word in them (or be empty).

My ideal solution would be to apply tags to specific portions of text in a single file and have compile options to selectively include items with specific tag(s), along with all un-tagged content. The editor would also display text tagged in this fashion with a user-selected style.

Any thoughts how I might accomplish this? My apologies if there is a feature already to do this, but I didn’t recognize it as I read the manual (or wasn’t smart enough to figure it out from what’s already available).

1 Like

My ideal solution would be to apply tags to specific portions of text in a single file and have compile options to selectively include items with specific tag(s), along with all un-tagged content. The editor would also display text tagged in this fashion with a user-selected style.

That’s precisely how you would do it: the Styles feature in Scrivener is capable of optionally deleting the text assigned to that style, as a specific setting for that style in the compile settings. So then the basic idea is to have three copies of your compile Format, each with one or two of these special styles set to be deleted.

This is how the Scrivener user manual itself handles numerous bits of text that only pertain to the Mac or Windows version of the software. When I compile using the Mac settings all of the text styled as “Windows-only” is deleted, and vice versa.

The attached sample project (82.9 KB) demonstrates a simple setup using this concept:

  • If you examine the two styles in use, you will see they don’t save font settings, and use dynamic highlighting to show themselves. They thus don’t really do much of anything, in the sense that we’d generally think of how styles are used.
  • The compile formats all change the formatting to 12pt Courier double-spaced, and as you can see when compiling the “NSFW Edition” format to RTF, the fact that we’ve styled some of the text has no visual impact on the output.
  • Edit the NSFW format and note it doesn’t actually do anything special with styles. All sentences are visible with it.
  • Now select the “Adult Edition” format and compile to that. The middle sentence, marked in pink in the editor, is removed from the output. Edit the compile format and examine the Styles pane. In here you will find the “NSFW” style listed, with the Delete text of this style setting enabled.
  • Lastly you could try compiling and examining the “General Edition” format—but at this point there should be no surprises as to what it does and how it does so.

And of course you may also sometimes want to filter out whole sections. I’ve posted some thoughts on how that can be done. For a three-way edition like you describe, you would need to use the Collection approach that is discussed, there, as the compile filter logic is simple exclude/include. Thus the more complex decision (this item should be in A but not B or C) needs to be delegated to search collections.

6 Likes

@AmberV ,

I can’t thank you enough for pointing this out and providing that sample project. I see how you’ve done it and it’s very straight-forward. I guess I’m just not experienced enough with Scrivener to have figured it out for myself. This is exactly what I wanted to do.

Thanks, again!!

1 Like

I wonder if you can use paragraph styles to the same effect? I tried to make a paragraph style and set that to “Delete text of this style” but the paragraph itself was still present in the compile output. Maybe I did something wrong? (I tried some days ago and I deleted all my styles I used for it meanwhile.)

In theory I would like to have character styles I can use to conditional suppress text as well as paragraph styles. My text was a mix of different strings within paragraphs but also whole paragraphs which should be conditionally suppressed independently.

For the time being I switched to whole folders/documents being different and the custom metadata/collections approach you explained here.

Yes, I think perhaps your test was not set up right, because it will make no difference if you use this setting on a paragraph style specifically, or a character style. I have tested both.

Though it is also worth noting that character styles can enclose any number of paragraphs, so there is a question of whether or not it is efficient to have different kinds of styles that do the exact same thing essentially. I only use character styles for deletions in the user manual projects, even though sometimes they will block large enough areas of text to span a page or two.

Thanks!

I just checked the character style scenario:

When I select the text of a complete paragraph and style that with just a character style which is set to be deleted during compile, I get an empty paragraph in the compiled file. (Windows, RTF)

Is that as expected? This would be an edge case then that could only be tackled with a paragraph style.

Ah I see, that is a complication I’ve never had to worry about since I use the much simpler Markdown to write with. There is no such thing as an “empty paragraph” for me, such typing errors (or of machine generation) are automatically cleaned.

The other unfortunate thing is that the Windows version has a bug in that character styles do not stick to what you apply them to if the edges of the selection include carriage returns. Normally that would be your answer, if it worked right: if you wanted to delete a paragraph you would select its newline as well, not just the visible text, and so the same would go for a “selection” that is a style meant to perform that same task.

So I guess in this case, with the more complex word processing workflow that requires fully accurate input, and a bug that is very difficult to fix, yes you might need a workaround like two separate styles for that.

1 Like

You could try setting up replacements in your compile format.
It might or might not work, depending on whether they happen pre or post styles’ handling, but try replacing return-return with return.

If that works, you’ll then need to set replacements for multiple consecutive returns.
In this order:
Five returns with return.
Four returns with return.
And so on. Decreasing.

Of couse, for that to work, it’d be imperative that you make proper use of space before/after for your paragraphs.

(I haven’t tested any of this.)

. . . . . . . . .
I can also think of a solution where, instead of using styles, you’d use a Regex formula (again, in your compile format’s replacements), tagging (prefixing) your target paragraphs before hand, in the editor, with (a) special character(s).

1 Like

Thank you both @AmberV and @Vincent_Vincent! I guess I will stick with the different styles for paragraphs and characters for the time being.

That Windows version bug seems to have a lot of consequences, I struggled with probably related problems with Alex in Support where list items changed their font settings depending on the font settings of preceding paragraphs which were deleted during compile. Weird and nasty. :slight_smile:

Indeed this root issue has a number of different implications. It is unfortunate that it is difficult to fix, because in a sense it really isn’t a bug so much as how the text engine addresses lines internally—how code must work with the text as a model of it. That’s a pretty big thing to be changing or working around.

1 Like