Conditional text is empty when exporting a document to EPUB

I have a large document that contains many conditional-text fields.

I need to export the document as EPUB.

Exporting as PDF works perfectly, but when exporting as EPUB, all of the conditional-text fields are empty!

I export using the menu File > Export As > Export as EPUB.

I don’t know how to add an attachment to this post, so here are links to samples:

How do I fix this so that the conditional text is exported to the EPUB, please?

EDIT: I have submitted a bug report, and amended the two previous links to point there.

Initially I thought that the omitted boxed variable declaration and initialisation was causing the problem, but that is not.
It may be a bug in the output filter. I could see what is inside the ePub file because it is a mixture of metadata and zip-compressed contents. Extracting contents requires more than a handful of seconds, so I didn’t attempt it.
Filter behaviour depends probably on ePub capabilities. I could find info about the reflow capability of ePub readers. But I don’t know if this extends to dynamic contents like your conditional fields. If it doesn’t, it is output filter responsibility to interpret the conditional field and emit the correct alternative. You should submit a bug report so that developers could give you an explanation.

Thanks for your answer, @ajlittoz

I don’t need the EPUB to be dynamic; it’s OK to export the displayed values as fixed text. That’s what exporting to PDF does, and I was expecting EPUB to do the same. Perhaps I should amend my OP to explicitly say this.

I didn’t think of looking inside the EPUB file, so I have done so. The EPUB contains exactly what you see; there is nothing between the square brackets apart from some redundant formatting information, presumably caused by the presence (in the original ODT document) of the conditional text.

Here is the complete XML file (I’ve added some line-feeds for readability):

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="../styles/stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body class="body0" xmlns:epub="http://www.idpf.org/2007/ops">
<p class="para0">
<span class="span0">This is a sample paragraph that contains conditional text.
The bit inside the square brackets here [</span><span class="span0">] should read
“</span><span class="span0">boxed in</span><span class="span0">”, and indeed it does
so in LibreOffice. But, once exported to EPUB, it is empty.</span>
</p>
<p class="para0">
<span class="span0">The image below shows the conditional text, which is found from
the menu Insert &gt; Cross-reference &gt; Functions &gt; Conditional text.</span>
</p>
<p class="para0">
<img alt="OEBPS/images/image0001.png" class="frame0" src="../images/image0001.png"/>
 </p>
</body>
</html>

I shall submit a bug report and then post back here.

Then it looks to me definitely as a bug in the export filter because there is absolutely nothing between the brackets.
Anyway, think about adding a definition for boxed before its use. I discovered that you can’t add it just like that. It looks like Writer sees there is a use for it and forbids creation of the same variable.

think about adding a definition for boxed before its use.

Sorry, I’m unsure what you mean. I did indeed define the variable boxed before I used it, if that’s what you mean. You can see it in the sample ODT document that I linked to.

I reloaded the document once again to make sure. I confirm there is no Set variable field in it. This can be asserted both with View>Field Shadings and the Navigator Fields section.

  • To see the variable inside the ODT document, go to Insert > Field > More Fields > Variables > Type > User Field. You’ll see “boxed” there; it is set to 1.

  • To see the field rather than the interpreted text, go to View > Field Names. You’ll see that the field between the square brackets reads:
    Conditional text boxed == 1 : boxed in : set free

  • To see the conditional-text field, highlight the text between the square brackets and double-click. You’ll see the same box as in the image.

EDIT: I see the field in the Navigator. I don’t know why you don’t?

I have submitted a bug report.

One ugly workaround in the mean time could be to save the document as Flat XML fodt. Open in a text editor, then search the opening and closing tags for conditional text <text:conditional-text …> and </text:conditional-text> and replace with an empty string. This leaves the text (as it was when saving the file) behind. Open back in LO from where you can export to epub.

This would obviously more easy if Writer had a function to convert fields to text. The official way to do so now is to select the text of the field, cut and paste back as unformatted text. Handy for your sample document, but not if there are tens or hundred fields to change.

You could even do the replacement in Writer after opening the fodt file as plain text (add a .txt. extension). With “Regular expressions” checked:

Search: <text:conditional.*?>(.*?)</text:conditional.*?>
Replace: $1

“Replace all” and you are done. Save back as plain text.

1 Like

That workaround is a fantastic temporary solution, thank you!

I shall use it in the meantime.

Am I correct that the only differences between .fodt and .odt are that .fodt is a single file and it doesn’t use compression? (I know that .odt is a file containing compressed files.)

I am afraid this is not the intended usage of this field because, though created, it is not present anywhere in the document. How do you change its value?

I see the conditional text field but not your user entry field. It appears only after I insert it. So how do you force a different version of the text when you have no “control” field in the document but only “using” fields?


Usually in such a document, you have a bunch of invisible (hidden) paragraphs at the beginning with text explaining the role of the variables and a variable field.
Built-in help explicitly mentions insertion of a Set variable field before using a condition.

Set variable and user entry fields are seemingly absolutely equivalent. They both create a variable and both are edited with a double-click.

The procedure is given in built-in help: copy the field and paste it back as Unformatted Text. It will show the alternative selected by the variable value.

No, these files are not equivalent. I have seen that for OLE objects some information is not preserved, and the objects are incorrectly restored when opening such file.

Note that I also wrote:

The official way to do so now is to select the text of the field, cut and paste back as unformatted text. Handy for your sample document, but not if there are tens or hundred fields to change.

I am afraid this is not the intended usage of this field …

Uh, yes, it is! It’s documented in the official help.

To answer your questions:

How do you change its value?

  1. Insert > Field > More Fields (on my system, the shortcut is Ctrl+F2, but I don’t know if I changed that) > Variables > Type > User Field > Select > select the variable.
  2. At the bottom of the dialogue, you can see the name and the value. Change the value and press the green tick.
  3. Close the dialogue.
  4. Tools > Update > Update All (the default shortcut is F9).

I hope that this answers all of your concerns.

The procedure is given in built-in help: copy the field and paste it back as Unformatted Text . It will show the alternative selected by the variable value.

@Vanadium already mentioned that. Unfortunately, it isn’t useful for my document, which has around 300 such fields; it would take an unacceptably long time, and I’d have to repeat the process each time I changed the variable.

The solution that @Vanadium gave is quick, so it’s an acceptable workaround.

Thanks, that is important information. I have made a note to be careful not to overwrite the original when following your procedure!

@PaddyLandau: the fact that you didn’t insert a user entry or set variable field from scratch is IMHO a logic mistake. Having these “control” fields grouped at start of text will draw attention on the “dynamic” nature of your document.
If you put into unprepared hands, users won’t guess they need to modify it by inserting a control field. In addition, I bet not many users know the trick to make these controls fields not visible in the final copy.
So all in all, your document should be ready for showing variants. The only needed procedure step should be the double-click on the variable.

I don’t know why you keep insisting that I didn’t set it up from scratch. There’s no other way to do it. You have to set it up from scratch. I literally followed the official instructions to do what I did, albeit that this was first set up over 10 years ago.

It works very well. Why would you think that it’s wrong to do this? I have two different target audiences. I change the variable for one audience, just the once, and press F9, and the entire document updates in under a second. I export my PDF or EPUB. Then I change the variable for the second scenario, again just once, press F9, and export my second PDF or EPUB.

Now I have the two versions that I need, one for one audience and one for the other audience.

Only if you have the ODT copy. But that’s not the copy for distribution. The resulting PDF or EPUB is the one for distribution, as I say, for two different target audiences.

Correct. That’s why I wouldn’t do that.

Update: I have found that EPUB export leaves other fields within the document are left empty, e.g., cross references (e.g. heading text), document information.

I think that I have no choice but to find a solution that doesn’t involve EPUB. That could be difficult, unfortunately, but I’ll figure it out.

These are issues with the current implementation of epub export. On the other hand, there are cases where exporting a field result makes no sense, for example a cross reference that refers to a page number.

Also here, a same trick could be applied (“text:bookmark-ref...…</text:bookmark-ref>”). Alternatively, you could try to convert fields to text before exporting to epub. Unfortunately, LO does not allow to do this automatically (the official way is to “cut the field and paste it back as unformatted text”, so one by one…).

Code that should do this is fiven here: Find field, replace by plain text, keep formating - #2 by ajlittoz

Thank you, @Vanadium . This is getting too complicated, so I shall probably have to stick with PDF.

Intuitively, I would have thought that a cross reference should work, if the page number itself, because clicking it would allow you to jump to that point. Oh well.