Quoth Michał Kruszewski:
I would like to have simple structure: custom cover page -> ToC ->
custom page -> text.
The easiest way to do all these things is to split the document in
multiple ones. Have one troff document for the cover page. There is
little reason for it to use ms. Have one document for the custom page
and one for the text. Have the text document use ms and generate the
ToC, perhaps by .tm. After processing the text document, you have a ToC
document.
Then comes the noteworthy bit: Concatenate the troff output of all those
separate documents and feed it all to the postprocessor at once, as in
troff -ms mainmatter.ms >mainmatter.trout 2>toc
{ troff frontmatter && troff toc && cat mainmatter.trout;
} | gropdf >all.pdf
That’s what I would do.
It is not a great thing to mix error output of troff with the ToC; you
might want to use fancy groff features to do the first step differently.