XML technologies / DocBook / Generating FO (PDF) / Displaying tables of contents / Run-in tables of contents in chapters

(Excerpt from "DocBook-XML: Media-neutral and platform-independent publishing" by Thomas Schraitle)

Run-in tables of contents are used when many sections need to be listed in a small amount of space. This type of table is set with ragged alignment; hyphenation should be avoided. Proceed as follows:

  1. Create a customization file as shown in Creating a customization file for XSL-FO.

  2. Insert the element <toc/> after each chapter title where you want a run-in table of contents.

  3. Add the following line to the parameter generate.toc:

<xsl:param name="generate.toc"> 
chapter toc,title 
... 
</xsl:param>

 

  1. Insert the following code to produce a run-in table of contents in a chapter:

<xsl:template name="component.toc"> 
    <xsl:param name="toc-context" select="."/>
    <xsl:param name="toc.title.p" select="true()"/> 
    <xsl:variable name="id">
        <xsl:call-template name="object.id"/> 
    </xsl:variable> 
    <xsl:variable name="nodes" select="d:section|d:sect1"/>
    <xsl:if test="$nodes">
        <fo:block id="toc...{$id}" xsl:use-attribute-sets="toc.margin.properties" line-height="1.75" text-align="left">
            <!-- <xsl:if test="$toc.title.p"><xsl:call-template name="table.of.contents.titlepage"/></xsl:if> --> 
            <xsl:apply-templates select="$nodes" mode="chaptoc">
                <xsl:with-param name="toc-context" select="$toc-context"/> 
            </xsl:apply-templates> 
        </fo:block> 
    </xsl:if> 
</xsl:template>

Line 2: Contains the current context.
Line 4: Creates an ID value obtained from the template object.id. This is later used in the element fo:block.
Line 7: Stores the required nodes.
Line 8: Checks whether the required nodes exist in the current context. If no node exists, no table of contents is inserted.
Line 9: Creates a block and inserts the ID value from line 4. Justified text is problematic for this type of table of contents, so left alignment is used. The property line-height slightly increases the line spacing for better readability.
Line 10: Commented out. If you want the words Table of contents before the entries, remove the comment delimiters.
Line 11: Formats the entry for the required node. The original uses the mode toc.

  1. Insert the following code to format all first-level sections:

<xsl:template match="d:chapter/d:sect1 | d:chapter/d:section" mode="chaptoc"> 
    <xsl:param name="toc-context" select="."/>
    <xsl:variable name="id">
        <xsl:call-template name="object.id"/> 
    </xsl:variable> 
    <fo:inline font-size="{$body.font.master}pt"> 
        <fo:inline hyphenate="false" space-end.minimum="0.25em" space-end.optimum="0.5em" space-end.maximum="0.5em"> 
            <fo:basic-link internal-destination="{$id}">
                <xsl:apply-templates select="." mode="titleabbrev.markup"/>
            </fo:basic-link> 
        </fo:inline> 
        <fo:inline space-end.conditionality="discard" space-end.optimum="1em" space-end.minimum="0pt" keep-with-previous.within-line="always"> 
            <fo:basic-link internal-destination="{$id}">
                <fo:page-number-citation ref-id="{$id}" font-weight="bold"/>
            </fo:basic-link> 
        </fo:inline> 
    </fo:inline> 
</xsl:template>

Line 2: Contains the current context.
Line 3: Creates an ID value obtained from the template object.id. This is later used in the element fo:block.
Line 7: Disables hyphenation and specifies the spacing before the page number.
Line 8: Creates an internal link for the title.
Line 9: Inserts the title, preferring titleabbrev instead of title. Long titles in particular can be shortened using titleabbrev.
Line 12: Prevents a line break between the title and page number and sets the spacing before the next entry. This should be greater than the spacing between the title and page number.
Line 13: Creates an internal link for the page number.
Line 14: Inserts the page number. Bold type makes the page number easier to find.

  1. Use your customization file for your document.

  2. The following figure shows what such a table of contents looks like:

    Appearance of a run-in table of contents

    Figure: Appearance of a run-in table of contents

  

back to top