Multiple TOCs in the same document

Everything related to our flagship word processor.
Post Reply
pttrn
Posts: 3
Joined: 2021-10-21 06:31:53

Multiple TOCs in the same document

Post by pttrn »

Hi Everyone,
I am new to Nisus Writer Pro having mainly used Microsoft Word before.

Could anyone tell me how to insert multiple TOCs in the same document? I am not talking about creating a TOC for the headings, one for the tables, and one for the authorities. Instead I would like to create multiple TOC for the same set of headings. Namely, I would like to have a general TOC for all the headings (up to level 4) for the entire document, including the chapters, and a TOC (up to level 8) at the start of each chapter with the headings in that specific chapter. I know it is possible in MS Word, but I cannot find a way to do this in Nisus.

Thanks for your help.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Multiple TOCs in the same document

Post by phspaelti »

Nisus allows you to create different TOC styles and insert as many TOCs as you want.
The way you control what a TOC inserts is using an attribute on the headings that you want to include.

In your case the basic procedure would work like this:
  1. Create a TOC style "Main TOC" and set styles to be applied as desired.
  2. Create a TOC style "Chapter TOC" and set styles
  3. In your heading styles 1-4 set the attributes to include in relevant levels of your Main TOC. Make sure other headings are not included
  4. In the headings below the chapter level set them to be included in the relevant levels of your Chapter TOC
Unfortunately this doesn't quite work. If you use the same styles in all your chapters the Chapter TOCs will include all the sub-headers from all the chapters, not just the current chapter. It looks like one would probably need to create a Chapter TOC style for each chapter, and then separate heading styles for each chapter as well, with each set of chapter headings to be included only in its chapter TOC (and the Main chapter when relevant). If you have more than two or three chapters, this looks like it would get quite messy.

At that point I would suggest the alternative to create macros than turn the relevant "include in TOC" styles on and off as necessary (i.e., right before you want to insert the TOC), or just create a macro that creates the TOC itself. Nisus cross-references work well for such purposes.

So it looks like there is no easy solution. :(
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Multiple TOCs in the same document

Post by phspaelti »

Here is—in very limited form—what such a macro might look like.
This macro works with just two levels of heading: Heading 1 are the chapter headings, and Heading 2 are the section headings.
It visits each chapter heading and turns on the include on the headings within the chapter. Then it inserts the TOC and removes the include.

It might make more sense to create a macro which just does one chapter, and then use it when appropriate on the current chapter.

Anyhow here is the code:

Code: Select all

$doc = Document.active
if $doc.tocStyleNames.containsValue("Chapter TOC")
  if $doc.activeTocStyleName != "Chapter TOC"
    Table of Contents:Chapter TOC
  end
else
  exit "Chapter TOC has not been set up"
end

$chapterHeadingSels = $doc.text.findAll $doc.styleWithName "Heading 1"
$heading2Style = $doc.styleWithName "Heading 2"
$end = $doc.text.length
foreach $sel in reversed $chapterHeadingSels
  $chapterRange = Range.newWithLocationAndBound $sel.bound, $end
  $chapterStart = TextSelection.new $sel.text, Range.new($sel.location, 0)
  $sectionHeadingSels = $doc.text.find $heading2Style, "a", $chapterRange
  $doc.setSelection $sectionHeadingSels
  Include in TOC:Level 1
  $doc.setSelection $chapterStart
  Insert TOC
  $sectionHeadingSels = $doc.text.findAll $heading2Style
  $doc.setSelection $sectionHeadingSels
  Remove from TOC
  $end = $sel.location
end
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Multiple TOCs in the same document

Post by phspaelti »

Following my own suggestion, I have now tried to create a more worked out macro. This macro inserts a TOC for the chapter after the immediately selected chapter heading. The TOC will include all sub-headings that are marked with a "Heading" style.
Attachments
Make Single Chapter TOC.nwm
(10.03 KiB) Downloaded 254 times
philip
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Multiple TOCs in the same document

Post by martin »

phspaelti wrote: 2021-10-22 10:40:45 Following my own suggestion, I have now tried to create a more worked out macro. This macro inserts a TOC for the chapter after the immediately selected chapter heading. The TOC will include all sub-headings that are marked with a "Heading" style.
Wow, that's very neat! It was clever to toggle "Remove From TOC" to ensure only the desired paragraphs were included. Thanks Philip :)
pttrn wrote: 2021-10-21 06:47:53 I would like to create multiple TOC for the same set of headings. Namely, I would like to have a general TOC for all the headings (up to level 4) for the entire document, including the chapters, and a TOC (up to level 8 ) at the start of each chapter with the headings in that specific chapter. I know it is possible in MS Word, but I cannot find a way to do this in Nisus.
That's a good idea for a new feature/enhancement. I can file the idea as something we might consider for the future.

I am curious: how do you accomplish the task in Microsoft Word? I know Word will let you choose which paragraphs to include in a generated TOC based on the paragraph style or TOC level, but I don't see a way to restrict it to a single chapter or chunk of text. How do you configure the TOC so it's limited to text from a single chapter?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Multiple TOCs in the same document

Post by phspaelti »

martin wrote: 2021-10-22 13:52:12 Wow, that's very neat! It was clever to toggle "Remove From TOC" to ensure only the desired paragraphs were included. Thanks Philip :)
Thanks. Yes, it's interesting that "Remove from TOC" even overrides any Includes specified as part of a style definition, which in this case is welcome behavior on the part of Nisus. Good job, Nisus engineers! :D
philip
pttrn
Posts: 3
Joined: 2021-10-21 06:31:53

Re: Multiple TOCs in the same document

Post by pttrn »

Hi,

thanks to all of you for the replies, particularly to phspaelti for his perseverance to solve somebody's else problem. :D

I am still trying to understand how the macro works and to run it on a sample document as I am not good at scripting and I am new to Nisus. I have created a skeleton sample document with two chapters and few headings and subheadings under each chapter. I have also created a TOC style called Chapter TOC and then I have assigned the headings and subheadings to it. However, when I run the macro I get a message saying "The current selection is not a chapter heading." What have I missed?

To Martin: in MS Word, you can restrict the entries in a Chapter TOC to only the headings and subheadings in that specific chapter by selecting (1) everything in a specific chapter, (2) set a bookmark for that chapter/selection and naming the bookmark uniquely, and then (3) entering "/b" as a field option when you create the chapter TOC. As you have unique bookmarks for each chapter (e.g., "BkmCh1" "BkmCh2", "BkmCh3", etc.) you can get a TOC for each chapter/bookmark in your document. You can find a much clearer explanation of the procedure at this website: "https://www.bluepecantraining.com/portf ... n-in-word/", or by googling "how to make multiple table of contents in word".

It would certainly be great to see a feature to do this in a future version of Nisus. I am looking forward to ditch for good MS Word as it is way to cumbersome.

Cheers

Roberto
pttrn
Posts: 3
Joined: 2021-10-21 06:31:53

Re: Multiple TOCs in the same document

Post by pttrn »

Actually, I have managed to understand how the macro works and it works nicely. Thank you so much for the help.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Multiple TOCs in the same document

Post by martin »

pttrn wrote: 2021-10-24 04:26:51 I am still trying to understand how the macro works ...
However, when I run the macro I get a message saying "The current selection is not a chapter heading." What have I missed?
I'm glad to hear that you figured this out. I'm attaching a revised version of Philip's macro that may clarify the error message, for anyone else that might stumble on this point.

The new macro has one other improvement: it no longer requires that your document have a "Chapter TOC" style. The macro will now ask you which TOC style to use. Or if your document has only a single TOC style, the macro will default to using that single style.

I also changed the macro's name to start with TOC. Over the years (and many Nisus macros) I've learned that macros whose names start with generic verbs like "make", "create", "transform", etc easily get lost. Hopefully Philip doesn't mind :)
To Martin: in MS Word, you can restrict the entries in a Chapter TOC to only the headings and subheadings in that specific chapter by selecting (1) everything in a specific chapter, (2) set a bookmark for that chapter/selection and naming the bookmark uniquely, and then (3) entering "/b" as a field option when you create the chapter TOC.
Thank you for that lesson Roberto! I didn't realize the TOC field had a /b switch to target a specific document bookmark. It's a bit ugly, but I can see why Word does it like that.
Attachments
TOC for Single Chapter.nwm.zip
(3.6 KiB) Downloaded 261 times
Post Reply