TOC text alignment

jcb
Posts: 5
Joined: Sat Mar 02, 2019 3:57 am

TOC text alignment

Post by jcb » Fri Mar 06, 2020 5:25 am

The text for my TOC for Parts and Chapters align well, but the author and book information are justified. My attempts to fix this via CSS have proved unsuccessful.

I'd also like to have the page numbers right aligned in the Chapters, as they are in the Parts.

And Lastly, the title of the TOC (Contents) gets put on it's own page. How can I force it to be on the same page as the TOC?

Below is a snippet of my CSS related to the TOC:

Code: Select all

@page toc {
  @bottom-right-corner {
    content: counter(page, lower-roman);
  }
}

ul.toc {
  page: toc;
}

ul.toc li {
    list-style-type:none;    
    text-align: left !important;
}


ul.toc a {
    link: attr(href);
    text-decoration:none;
    text-align: left !important;
}

ul.toc a:after{
    color:black;
    content: leader(" ") target-counter(attr(href), page);
    link: attr(href);
}

li.book-section {
    font-weight:bold;
}

li.toc-author-item
{

    color: black;
    text-align: left !important;    
}

span.author {
    font-weight:normal;
    text-align: left !important;
}

span.toc-summary {
    font-weight:normal;
    text-align: left !important;
}

@page { 
  size: us-letter;
}

@page {
   @bottom-left {
        content: "...Books - Introduction to ...";
        font-size:80%;
   }
}

@page {
    @bottom-right {
      content: counter(page);
      font-size:80%;
    }
}

@page {
  margin: 1in;
}

@page :left {
  margin-right: 1in;
}

@page :right {
  margin-left: 1in;
}

@page two-columns-page {
    column-count: 2;
    column-gap: 1in;
}

@page two-columns-page {
  margin: 1in;
}

@page two-columns-page:left {
  margin-right: 1in;
}

@page two-columns-page:right {
  margin-left: 1in;
}
XML I'm using to generate the PDF:

Code: Select all

<body>
      <div class="toc">
         <h1>Contents</h1>
         <ul class="toc">
            <li class="book-section">
               <a class="ref-section" href="part-1">
                  <span class="toc-label" title="I">Part I.</span> This is Part I</a>
               <ul class="book-section">
                  <li class="toc-item">
                     <a class="ref-chap"
                        id="s9781544357140-toc.i3"
                        href="#s9781506395449.i804">Chapter 1.  This is Chapter 1</a>
                     <span class="author-list">
                        <br/>
                        <span class="author">Steve Kippman</span>
                     </span>
                     <br/>
                     <span class="toc-summary">From <i>This Book, Second Edition</i>
                     </span>
                  </li>
               </ul>
            </li>
            <li class="book-section">
               <a class="ref-section" href="part-2">
                  <span class="toc-label" title="II">Part II.</span> This is Part II</a>
               <ul class="book-section">
                  <li class="toc-item">
                     <a class="ref-chap"
                        id="s9781544357140-toc.i6"
                        href="#s9781506362205.i2009">Chapter 2.  This is Chapter 2</a>
                     <span class="author-list">
                        <br/>
                        <span class="author">Chris Bar</span>
                        <br/>
                        <span class="author">George C. Right</span>
                     </span>
                     <br/>
                     <span class="toc-summary">From <i>Another Book, Eighth Edition</i>
                     </span>
                  </li>
               </ul>
            </li>
            <li class="book-section">
               <a class="ref-section" href="part-3">
                  <span class="toc-label" title="III">Part III.</span> This is Part III</a>
               <ul class="book-section">
                  <li class="toc-item">
                     <a class="ref-chap"
                        id="s9781544357140-toc.i7"
                        href="#s9781506362205.i2663">Chapter 3. This Chapter 3</a>
                     <span class="author-list">
                        <br/>
                        <span class="author">Chris Bar</span>
                        <br/>
                        <span class="author">George C. Right</span>
                     </span>
                     <br/>
                     <span class="toc-summary">From <i>Another Book, Eighth Edition</i>
                     </span>
                  </li>
               </ul>
            </li>
         </ul>
      </div>
I've attached the generated PDF as PNGs.
page-1.png
page-1.png (3.28 KiB) Viewed 237 times
page-2.png
page-2.png (19.12 KiB) Viewed 237 times

julien_lacour
Posts: 84
Joined: Wed Oct 16, 2019 3:47 pm

Re: TOC text alignment

Post by julien_lacour » Fri Mar 06, 2020 11:55 am

Hello,

To fix the first two point update the following selector:

Code: Select all

ul.toc a {
    display: block;
    link: attr(href);
    text-decoration:none;
}
The leader makes sense in a justified context, this is why the whole content was justified.
If you isolate the link in a block, the author and book information will be left aligned automatically (default behavior).
So you can also remove the text-align: left !important properties.

For the "Contents" title in TOC you need to update the ul.toc selector:

Code: Select all

.toc {
  page: toc;
}
You also need to define another named page (if you did not) after your TOC to set back the default behavior:

Code: Select all

@page content {
  /* Currently inherit from the default page.*/
}

.content {
  page: content;
}
Finally, your content needs a small update to make the CSS work:

Code: Select all

<body>
        <div>
            <h1 class="toc">Contents</h1><!-- Update the class -->
            <ul class="toc">
                <li class="book-section">
                    <a class="ref-section" href="part-1">
                        <span class="toc-label" title="I">Part I.</span> This is Part I</a>
                    <ul class="book-section">
                        <li class="toc-item">
                            <a class="ref-chap"
                                id="s9781544357140-toc.i3"
                                href="#s9781506395449.i804">Chapter 1.  This is Chapter 1</a>
                            <span class="author-list">
                            	<!-- <br/> is not necessary -->
                                <span class="author">Steve Kippman</span>
                            </span>
                            <br/>
                            <span class="toc-summary">From <i>This Book, Second Edition</i>
                            </span>
                        </li>
                    </ul>
                </li>
                <li class="book-section">
                    <a class="ref-section" href="part-2">
                        <span class="toc-label" title="II">Part II.</span> This is Part II</a>
                    <ul class="book-section">
                        <li class="toc-item">
                            <a class="ref-chap"
                                id="s9781544357140-toc.i6"
                                href="#s9781506362205.i2009">Chapter 2.  This is Chapter 2</a>
                            <span class="author-list">
                            	<!-- <br/> is not necessary -->
                                <span class="author">Chris Bar</span>
                                <br/>
                                <span class="author">George C. Right</span>
                            </span>
                            <br/>
                            <span class="toc-summary">From <i>Another Book, Eighth Edition</i>
                            </span>
                        </li>
                    </ul>
                </li>
                <li class="book-section">
                    <a class="ref-section" href="part-3">
                        <span class="toc-label" title="III">Part III.</span> This is Part III</a>
                    <ul class="book-section">
                        <li class="toc-item">
                            <a class="ref-chap"
                                id="s9781544357140-toc.i7"
                                href="#s9781506362205.i2663">Chapter 3. This Chapter 3</a>
                            <span class="author-list">
                            	<!-- <br/> is not necessary -->
                                <span class="author">Chris Bar</span>
                                <br/>
                                <span class="author">George C. Right</span>
                            </span>
                            <br/>
                            <span class="toc-summary">From <i>Another Book, Eighth Edition</i>
                            </span>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
        <div class="content"><!-- Get back to default page -->
        	...
	</div>
    </body>
Based on your remarks I opened an issue on our side to update our documentation and make it clearer.

Regards,
Julien

jcb
Posts: 5
Joined: Sat Mar 02, 2019 3:57 am

Re: TOC text alignment

Post by jcb » Fri Mar 06, 2020 11:50 pm

Thank you, Julien. I've carried out your suggestions and it's working perfectly now. Thanks for looking at updating the instructions.

Post Reply