How to make a chapter field in a heading blank, when there is no chapter at that level?

I have put two chapter fields in a page header, level 1 and 2. It works when there is a level 2 chapter or a page. But when there is only a chapter 1 level, the level 1 chapter title is repeated in the level 2 field.
How do I make the level 2 field “blank” in the header when there is no level 2 chapter?

This is the implemented behaviour. But you must thoroughly understand how it works before implementing an imperfect workaround.

You are wrong assuming Writer scans a page to collect the current outline state. It only looks at the first paragraph of the page. The outline nesting of every paragraph is constantly monitored. Consequently, Writer knows the closest preceding Heading n and the n value is the current nesting level. As you noticed, if you request a chapter parameter for level m for m>n, all fields for levels n+1 to m will return level n attributes.

An imperfect workaround is to systematically add a “fake” sub-chapter heading corresponding to the level-n field you want to silence in the header. These fake headings will be empty paragraphs. Of course, if you style them Heading n, they will create a large empty space following your chapter heading.

  • Recipe
    Create new paragraph styles Fake Level n you will attach to the adequate outline level and whose vertical spacing will be zero, font effects set to Hidden. Of course, don’t forget to enable viewing hidden data so that you have a visual clue on screen.

Empty headings don’t show up in TOC. You are then safe here.

Where the workaround is imperfect is on the first page of a chapter. On this page, whatever you do (except to lay out your chapter heading centred on a page by itself with a dedicated page style), the first paragraph will be a Heading 1 and any Heading n for n>1 doesn’t exist. This can be mitigated by reconfiguring your page style so that the header is different on the first page. You won’t insert a field for level(s) different than 1.

The workaround is also imperfect if your header contains more than 2 level-n fields: there is only one first paragraph in a page and this paragraph is necessarily attached to a single level.

Your question elaborates on a “solution” you arrived at. It is better to describe the expected resulted from a reader’s point of view. We can then suggest better ideas. I have the feeling that your question is not how to retrieve reliably outline information but how to build dynamically a header according to some scheme you didn’t describe. A solution based on automatically chained page styles may be what you are looking for.

PS: when asking here, always mention OS name, LO version, save format and your skill level with styles. The workaround I briefly sketched above will work only for .odt files. More targeted advice can be given when you provide this missing information.

1 Like

Wow, thanks for the thorough solution! My plan for the header is as follows:
left page: Page#, Book Title, Level1-Chapter
right page: Level2-Chapter, Leve3-Chapter, Page#
I’m putting a 3-cell table in the header to align the fields.

My style skill is probably above average, but not at the expert level that your answer showed. :slight_smile: Your answer here is probably enough to get me going, but if you have other suggestions, I’d be happy to learn more.

OS: MX Linux 21.3, Libreoffice 7.0.4.2 00(Build:2)
Saving as odt file, 9"x6" for a trade book. The end format will probably be a PDF.

There is no need for this. Built-in paragraph styles Header, Header Left and Header Right all have tab stops defined at center (with center alignment) and right (with right alignment). Thus, If you have 3 elements in your header, just press Tab to send them at their respective locations. However, since you’re using a non-standard sheet size, you probably need to adjust the tab stops. Just do it in Header. The other styles inherit from it.

Don’t use tables when your text is not semantically tabular “by nature”. Remember that inserting “objects” (tables, frames, sections, graphical objects, …) complicates the internal structure of your document and may cause stability and compatibility issues. So, when not strictly necessary, avoid them and use a simpler structure respecting a more traditional flow model.

Since you want outline data spanning 3 levels, my workaround won’t work. There could be an alternate solution based on variables and field insertions but it is not user-friendly and extremely tedious to apply (seriously complicating creation of headings).

I agree, using a table is overkill for the heading area. I tried to get TABs to work, but the custom size and getting the left/right pages to work was not easy for me. I’ll try using TABs again later. I’ll probably drop using a Level 3 chapter so I can use the workaround that you suggested.

Thanks again!