Automatically update the Table of Contents

Hi all:

I have a Writer document with a Table of Contents. After adding or editing sections, I keep forgetting to manually update it (right-click on it, choose “Update Index”).

So the next time around I get confused because some new or changed section is not on the Table of Contents. Until I remember the annoyance again.

Is there any way to update the “Table of Contents” automatically? If not on the fly, hopefully every few minutes. Or at least when saving the document.

Thanks in advance,
rdiez

There’s a feature request to auto-update the table of contents on print or save: https://bugs.documentfoundation.org/show_bug.cgi?id=44448

Permanent TOC update would cause many operational problems, not to speak of performance.

You already know the right-click + Update index procedure. You also have Tools>Update>Update All (which you can associate to a keyboard shortcut if you use it a lot). But this latter command will scroll you to the end of your document (because it causes a full scan).

What kind of operational problems would those be? A permanently out-of-date Table of Contents may be actually the biggest “operational problem” for most users.

How about an option to automatically update when saving? Most computers should be fast enough that you wouldn’t mind.

Microsoft Word has an option to automatically update such tables before printing. I hope LibreOffice Writer can do that too. Otherwise, it is no fun to print documents (or generate PDFs) with an outdated Table of Contents.

The worst type of operational problem is a potential infinite loop while computing the new layout of pages caused by an expansion/contraction ambiguity of field replacement. This can’t occur on manual update because you are not simultaneously modifying your text.

I don’t know if automatic update on save is a universal wish. Considering the present behaviour, I am now relying on a workflow where I expect the TOC not to be modified when I backup or save so that I can measure the progress in my work. Updating the TOC is part of the explicit breakpoints I periodically make.

So, again because of the workflo I developed with time, such an automatic update should be controlled by an option in Tools>Options, the same kind as auto-backup are controlled, i.e. under explicit user request.

The AskLO site is manned by users like you. If you want to submit a feature request, do it on the Bugzilla server where developers can be reached. If you do so, reference your request in this question under the form tdf#<bug number>. This creates a link to the bug report.

I do not really understand your workflow, that is, how you can rely on the Table of Contents not being automatically updated in order to measure progress. For me, the lack of automatic or semiautomatic update is a significant flaw.

Nevertheless, I also wanted to thank you for the other accurate information you provided so quickly.

It is the application’s workflow that matters here. The content needs to be frozen for a moment, the entire document needs to be paginated and finally the TOC itself may change the pagination as it grows or shrinks. If at the same time the user pastes or deletes some text, things may break easily or loop endlessly. If the application freezes a thousand pages with many headings and subheadings while you are editing just because the pagination may have changed, you would complain about this program not being usable at all.

On the other hand, I find it counter-intuitive that an automatic Table of Contents has to always be updated manually. The way it is at the moment, it may forever stay out of sync with the document, and the user does not get a warning or visible hint. That is not OK.

I am not asking for an immediate update after every keystroke. But there has to be a middle ground.

Some editors have an option to automatically save the current document at regular intervals. This way, if you forget to press the “Save” button often, a reasonably up-to-date version can be recovered afterwards. Saving to disk, especially over a network connection, may mean there is a delay every now and then. But it is probably a good tradeoff.

Automatically updating the Table of Contents should offer at least a compromise solution. Auto-updating before printing is probably mandatory. Auto-updating when saving would probably be acceptable by most users. Auto-updating every 30 minutes, with the hint “Updating Table of Contents…”, will probably be acceptable by most users.

Relying exclusively on manual updates reminds me of the WordPerfect years.

I wonder how those other editors work, where many people can update the same document at the same time on different places. I heard some are even web-based, so using JavaScript.

LibreOffice is a typical desktop application of the 90ies. The Open Document Format is the crucial progress. Everything else is conservative.

If you’re prepared to use a Macro solution, you could try this solution. The idea is to create a Sub that will be called each time the document is opened StartUp(). It will call a Function called DocModifiedToday() to see if you’ve modified the document today (obviously). This way you won’t be nagged every time you open your document. If you haven’t modified the document, then StartUp() calls UpdateTOC() to – you guessed it.

Here’s the code:

Sub UpdateTOC()

Dim oIndexes, oIndex As Object

  oIndexes = ThisComponent.getDocumentIndexes()
  For i = 0 To oIndexes.getCount() - 1
    oIndex = oIndexes.getByIndex(i)
    If oIndex.supportsService("com.sun.star.text.ContentIndex") Then
      oIndex.update()
      Exit For
    End If
  Next

End Sub



Function DocModifiedToday() As Boolean

Dim ModDate As Object
Dim LastMod As Date

   ModDate = ThisComponent.DocumentProperties.ModificationDate
   LastMod = DateSerial(ModDate.Year, ModDate.Month, ModDate.Day)
   DocModifiedToday = (LastMod = DateValue(Now()))

End Function



Sub StartUp()

  ' Check to see if Doc has aleady been modified TODAY. If so, we skip TOC update
  If DocModifiedToday() = True Then Exit Sub

  If (Day(Now) = 1) AND (Month(Now) > 1) Then ' Do on the 1st of each month but not January
     Answer = MsgBox("Update the TOC?", 32 + 4, "Start Up") ' Ask the user if he wants to update the TOC
     If Answer = 6 Then UpdateTOC()
  End If

End Sub

Other options you can go with are:

If(WeekDay(Now) = 1) Then ’ Every Monday (Or SUN = 0 to SAT = 6)

Or you could simply remove the If … Else and just do it each day:

  Answer = MsgBox("Update the TOC?", 32 + 4, "Start Up") ' Ask the user if he wants to update the TOC
  If Answer = 6 Then UpdateTOC()

The DocModifiedToday() Function is just anti-nag coding. Once you’ve actually edited your document in some way, even just re-saving it, you won’t be asked again that day whether you want to update the TOC. (That would be nagging.)

To make it all work, you need to then set StartUp() to be called when the document is opened:

Tools → Customize → Events
Set Open Document to call your StartUp() Sub.

Hope this helps.

1 Like

That is an interesting work-around, thanks for the info.