In addition to @RGB-es’s answer, I point out that your requirement is quite complex, i.e. chapter and subsection title might be long enough to cause text to flow to next line, misaligning subsequent elements on the wrong tab stop.
In such a case, I opt for a table so that I can control exactly what goes where. See example below.
This is the “code” in the table
resulting in
where all elements come from field insertion (from File
>Properties
, except chapter title and number from the most recent Heading 1). Only fixed strings like “Project” or “Language” are manually written. Even the book title is a “property” so that it is identical on the title page and in the header. Note: page numbers are inserted in the footer; it is only a matter of personal preference.
Chapter and subsection
Field values to be inserted in the header are computed when the page is created. At this time, only the first paragraph is available. Of course, content of previous pages is also known. This context is used to determine field values.
You have no problem with Heading 1 because you usually cause a page break so that it is the first paragraph on the page. Either the first paragraph is a Heading 1 and you get the new chapter title, or it is another paragraph and you get the current chapter value (the most recently set Heading 1 if it exists, see below).
The only difficulty arises if you precede your Heading 1 with some introductory text or quotation: this will cause the chapter title to be lagging one page.
Situation is more complex with Heading n, n>1. General algorithm is the same: context at first line is used to compute field values. However, you may not have yet a Heading 2 (you are at the very early beginning of the chapter). In this case, LO Writer has chosen to return the closest containing Heading n (e.g. you request Heading 3 but it does not exist, then Writer attempts to return the containing Heading 2 if possible, otherwise Heading 1, by default nothing).
This is why you get twice the chapter title when there is no subsection yet. The general algorithm also returns the known Heading 2 at the beginning of the page (reverting to Heading 1 if there is none yet), eventhough you have a Heading 2 change inside the page.
Field insertion in the footer shows the expected results because there is no need for forward looking in the page, though we get there the last section titles (not the first in the page). But it is quite unusual (and disturbing) to display headings at the bottom of the page.