https://bugs.documentfoundation.org/show_bug.cgi?id=174532

            Bug ID: 174532
           Summary: Auto Numbered Paragraphs
           Product: LibreOffice
           Version: 26.8.3.1 release
          Hardware: x86-64 (AMD64)
                OS: Windows (All)
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: medium
         Component: LibreOffice
          Assignee: [email protected]
          Reporter: [email protected]

Technical Report
Implementing Dynamic Hierarchical Paragraph Numbering in LibreOffice Writer
26.8.3
Technical assessment and proposed implementation

1. Executive Summary
LibreOffice Writer 26.8.3 provides automatic heading numbering through the
Heading Numbering facility and independent paragraph numbering through List
Styles.
Although both systems provide automatic numbering, they do not natively provide
a direct mechanism for ordinary paragraphs to inherit the numbering hierarchy
of the most recently preceding heading.
This creates a limitation when preparing large, structured documents where both
headings and ordinary paragraphs require continuous hierarchical numbering.
This report proposes a solution to overcome that limitation while retaining the
existing LibreOffice Writer heading structure.
The proposed solution is a Dynamic Hierarchical Paragraph Numbering System
(DHPNS).
Its purpose is to provide automatic, context-sensitive numbering for ordinary
paragraphs, with numbering that adjusts throughout the document when headings
or paragraphs are inserted, deleted, moved or reorganised.

2. Existing LibreOffice Numbering Architecture
2.1 Heading Numbering
LibreOffice uses paragraph styles assigned to outline levels to generate
heading numbers.
The standard configuration is:
Outline level   Paragraph style Example
1       Heading 1       1
2       Heading 2       1.1
3       Heading 3       1.1.1
4       Heading 4       1.1.1.1
Heading numbering is managed through:
Tools → Heading Numbering
LibreOffice supports up to ten outline levels. The outline level, rather than
simply the paragraph style's name, determines whether a paragraph is treated as
a heading. 

2.2 Ordinary Paragraph Numbering
Ordinary paragraphs can be numbered using List Styles and paragraph styles.
These provide:
    • Automatic sequential numbering.
    • Multiple list levels.
    • Numbering formats and indentation.
    • Restarting and continuation controls.
    • Automatic updates when list items are inserted or deleted.
However, List Styles operate independently of the document's heading outline
hierarchy. 

2.3 The Limitation
The fundamental problem is that Writer does not provide a native setting to
dynamically associate an ordinary paragraph with the current heading and derive
its number from that heading.
For example, assigning an ordinary paragraph to a subordinate numbering level
can result in it inheriting the deepest configured heading level rather than
the level immediately below the heading that governs that paragraph.
This becomes particularly problematic in documents containing Heading 1,
Heading 2, Heading 3 and Heading 4.

3. Proposed Solution: Dynamic Hierarchical Paragraph Numbering System
I recommend developing a LibreOffice Basic macro, supported by custom paragraph
styles, to provide the missing functionality.
The system would maintain two related but distinct functions:
    1. Heading numbering would remain under the control of LibreOffice's native
Heading Numbering facility.
    2. Ordinary paragraph numbering would be calculated dynamically using the
outline structure of the document.
The macro would examine the document's paragraphs in their actual document
order, identify headings and their outline levels, and calculate the
appropriate numbering for each ordinary paragraph.

3.1 Proposed Paragraph Styles
The following paragraph styles would be established:
Paragraph style Function
Heading 1       Main heading
Heading 2       Second-level heading
Heading 3       Third-level heading
Heading 4       Fourth-level heading
Body Numbered   Automatically numbered ordinary paragraph
Body Continuation       Unnumbered continuation paragraph
Body Unnumbered Ordinary unnumbered paragraph
Ordinary paragraph styles would have their outline level set to None.
This is important because they must not interfere with the heading outline
hierarchy.

3.2 Numbering Calculation
The proposed macro would:
    1. Read each paragraph in document order.
    2. Identify whether it is a heading or an ordinary paragraph.
    3. Determine the current heading hierarchy.
    4. Maintain paragraph counters at the appropriate subordinate level.
    5. Generate the correct paragraph number.
    6. Reset subordinate counters when a new heading changes the applicable
context.
    7. Update all affected paragraph numbers.
The numbering would be calculated from the document structure rather than from
manually maintained number sequences.

4. Critical Numbering Design Decision
There is one important issue that must be resolved before implementation.
Heading numbers and paragraph numbers must not produce duplicate identifiers.
For example, consider:
    • Heading 1: 1
    • Ordinary paragraph: 1.1
    • Heading 2: 1.1
Both the paragraph and the heading would have the same number.
A proper implementation must avoid this.
I recommend considering two numbering conventions.
Option A — Shared hierarchical numbering
Headings and ordinary paragraphs share the same subordinate numbering sequence.
1 Introduction — Heading 1
1.1 First ordinary paragraph
1.2 Second ordinary paragraph
1.3 Background — Heading 2
1.3.1 First ordinary paragraph
1.3.2 Second ordinary paragraph
1.3.3 Scope — Heading 3
1.3.3.1 First ordinary paragraph
1.3.3.2 Second ordinary paragraph
Advantages:
    • Every paragraph has a unique hierarchical number.
    • Heading and paragraph numbers follow one consistent structure.
    • All numbering can be recalculated from document order.
Disadvantage: Heading 2 numbering is affected by the number of ordinary
paragraphs appearing before it.
This differs from conventional heading numbering.
Option B — Independent heading and paragraph identifiers
Retain conventional heading numbering, but identify ordinary paragraphs
separately.
1 Introduction — Heading 1
1-P1 First ordinary paragraph
1-P2 Second ordinary paragraph
1.1 Background — Heading 2
1.1-P1 First ordinary paragraph
1.1-P2 Second ordinary paragraph
1.1.1 Scope — Heading 3
1.1.1-P1 First ordinary paragraph
Advantages:
    • Heading numbering remains completely conventional.
    • Paragraphs are associated with their governing heading.
    • No numbering collisions occur.
    • Paragraph references remain unambiguous.
Disadvantage: Paragraph numbers are not purely decimal.
My preference is Option B if maintaining conventional Heading 1–4 numbering is
important.

5. Automatic Updating Requirements
The proposed macro should provide the following functionality.
Operation       Expected behaviour
Insert heading  Recalculate affected paragraphs
Delete heading  Recalculate following paragraphs
Change heading level    Recalculate affected hierarchy
Insert paragraph        Renumber subsequent paragraphs
Delete paragraph        Renumber subsequent paragraphs
Move paragraph  Recalculate its new position
Move section    Recalculate the affected document
Save document   Ensure numbering is current
Reopen document Restore and verify numbering

The macro should use LibreOffice's UNO API to examine and manipulate paragraph
properties and document content. The UNO API exposes properties including
NumberingStyleName, NumberingLevel, NumberingRules and OutlineLevel. 

6. Macro Architecture
The proposed implementation would have four components.
Document structure
Headings and ordinary paragraphs
Document analyser
Identifies paragraph styles and heading levels
Numbering calculation engine
Maintains counters and hierarchical relationships
Numbering update engine
Updates paragraph identifiers throughout the document
The macro should not replace or manually rewrite the actual heading numbering.
It should leave the heading numbering mechanism intact.
The paragraph numbering engine should be independently responsible for ordinary
paragraph identifiers.

7. Updating Mechanism
For reliability, I recommend three levels of updating:
A. Manual refresh
Provide a toolbar button or menu command:
Tools → Update Paragraph Numbering
This would perform a complete recalculation.
B. Automatic document events
Configure the macro to recalculate when the document is opened and before it is
saved.
C. Editing events
Investigate an event listener that detects document modifications and schedules
a recalculation.
A technical consideration is that LibreOffice's standard document events do not
necessarily provide a simple, reliable callback for every individual text edit.
Therefore, the editing-event implementation would need testing. A full-document
refresh on demand and before saving should remain available as a fallback.
8. Data Integrity and Document Compatibility

The implementation should observe the following requirements:
    • Do not convert automatic heading numbers into ordinary text.
    • Do not manually type paragraph numbers into the document.
    • Do not change the paragraph's actual content when updating numbering.
    • Preserve paragraph formatting, indentation and spacing.
    • Preserve hyperlinks, bookmarks and cross-references.
    • Preserve the Table of Contents and document outline.
    • Support saving and reopening in the OpenDocument Text (.odt) format.
An important implementation decision is whether paragraph identifiers are
generated using native numbering properties or maintained as dynamic fields.
This should be established during prototyping.
I recommend native numbering properties wherever practical, because they offer
better integration with Writer's document structure.

9. Testing and Acceptance Criteria
Before deploying the solution in a large document, the following tests should
be performed.
Test    Expected result
Insert a Heading 2      All affected paragraph numbers update
Delete a Heading 2      Following paragraphs are reassigned
Insert 10 ordinary paragraphs   All ten receive correct numbers
Delete 5 ordinary paragraphs    Remaining paragraphs renumber
Change Heading 3 to Heading 4   Paragraph hierarchy updates
Move an entire section  Numbering follows the new structure
Insert Heading 4        Existing paragraph numbering remains correct
Save and reopen Numbering is retained correctly
Update Table of Contents        Heading entries remain correct
Testing should also include a large document with several thousand paragraphs
to assess performance.

10. Development Recommendation
I recommend developing this as a reusable LibreOffice Basic macro package.
The development should proceed in stages:
    1. Create the paragraph styles.
    2. Develop the document analyser.
    3. Develop the hierarchical numbering calculation engine.
    4. Implement the paragraph numbering update mechanism.
    5. Add document-open and document-save updating.
    6. Investigate automatic updating during editing.
    7. Test against large documents.
    8. Package the solution for reuse in other Writer documents.
The macro should also include error handling and a diagnostic report showing
paragraphs that cannot be assigned a valid numbering context.

11. Conclusion
LibreOffice Writer 26.8.3 provides a comprehensive heading numbering system and
a separate paragraph numbering system. However, neither system independently
provides the context-sensitive hierarchical paragraph numbering required for
the proposed document structure.
A custom numbering engine using LibreOffice Basic and the UNO API is a feasible
development approach, subject to prototyping and testing.
This would allow LibreOffice Writer to retain its existing heading numbering
functionality while providing an additional automated numbering capability for
ordinary paragraphs.
The proposed system would make it possible to maintain consistent hierarchical
paragraph references throughout large documents without manually adjusting
numbering after document changes.

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to