LaTeX forum ⇒ Text FormattingChapter Headings in LoF and LoT

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
BaCaRoZzo
Posts: 10
Joined: Sun Dec 18, 2011 7:53 pm

Chapter Headings in LoF and LoT

Postby BaCaRoZzo » Sun Jan 15, 2012 2:13 pm

Hi all,

I've searched the forum and the Internet for clues about this point but, as far as I've searched, I didn't resolve my problem.

I want to had the chapter titles, along with their numbers, in the LoF and LoT. I've found the two commands to do this. Unfortunately none of them suits my needs. If I choose to add, after the chapter declaration, the command
  1. \addcontentsline{lof}{chapter}{\protect\numberline{\arabic{chapter}} {Introduction}}

The chapter is correctly set BUT also the page number is displayed and hyperlinked (hyperref).
If I use the following one, instead,
  1. \addtocontents{lof}{\protect\numberline{\thechapter}{Introduction}}

the chapter is not inserted in bold text (as previously) and no hyperlinked page number is shown BUT a line break is generated. In other words, the chapter number is set on a row and the chapter title is printed in the subsequent line.

Basically what I want is to insert the title in bold with the chapter number and without the corresponding page number. What can be wrong with the second command? Can I use the first command forcing the hiding of the page number?

As a second point. I would like to typeset this automatically. E.g. I would like that the command calls the chapter addition to the list of figures/tables. Or something like that. Is it possible?

Thanks in advance for your kind answers.
Best,
F.

Tags:

5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Postby 5gon12eder » Mon Jan 16, 2012 5:59 pm

The first thing is very simple. Just replace

  1. \addcontentsline{lof}{chapter}{\protect\numberline{\arabic{chapter}} {Introduction}}


with

  1. \addcontentsline{lof}{chapter}{\protect\numberline{\thechapter\quad{}Introduction}}


i.e., put everything into one \numberline. (I've also added a \quad space and wrote \thechapter which is generally the preferred way since it allows a user to modify the style more flexible.)

If you want to do things more automatically, it gets a little tricky. I don't think that appending some code to the definition of \chapter would do what one expects. For example, it'd add entries

0 Contents
0 List of Figures
0 List of Tables


etc. to the list of figures which is probably not what you want.

If you only want chapters to appear that actually contain some figures, I think it is safer to let \caption do the work. In the following suggestion, using Philipp Lehman's etoolbox package, I define the counter chapter@last that stores the number of the last chapter added to the LoF. If it matches the current chapter number, \caption won't do anything special, otherwise it adds a line with the current chapter name. This name is stored in the macro \thechaptername which is defined in the redefinition of \chaptermark.

  1. \usepackage{etoolbox}
  2.  
  3. \makeatletter
  4.  
  5. \newcommand{\thechaptername}{}
  6. \newcounter{chapter@last}
  7.  
  8. \renewcommand{\chaptermark}[1]
  9. {%
  10. \markboth{#1}{}%
  11. \renewcommand{\thechaptername}{#1}%
  12. }
  13.  
  14. \pretocmd{\caption}
  15. {%
  16. \ifnumequal%
  17. {\value{chapter}}%
  18. {\value{chapter@last}}%
  19. {}% already added
  20. {%
  21. \addtocontents{lof}%
  22. {\protect\numberline{\bfseries\thechapter\quad\thechaptername}}%
  23. \setcounter{chapter@last}{\value{chapter}}%
  24. }%
  25. }
  26. {}
  27. {}% failed, could put some error message here...
  28.  
  29. \makeatother


Best
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).

BaCaRoZzo
Posts: 10
Joined: Sun Dec 18, 2011 7:53 pm

Postby BaCaRoZzo » Tue Jan 17, 2012 1:04 pm

Hi 5gon12eder,
thanks for your kind reply.

As soon as I submitted my question I realized that adding code to \chapter would make no sense. Indeed it is not so obvious that every chapter has at least a figure.

That said, I read your suggestion and implemented your code: it worked like a charm. The used package is really powerful, never heard about it! Good point. :D

Understood your code I have added it to my style file. The command about the header have been moved to the fancy header part. The "core" part of your solution has been changed to a command, which is called at a certain point. A variable CHAPTERS is evaluated so it can be chosen if the chapters are added or not to the LoFT, via such variable.

Here's the code:
  1. \newcommand{\chapters}{
  2. \ifbool{CHAPTERS}{
  3. \newcounter{chapterF@last}
  4. \newcounter{chapterT@last}
  5.  
  6. \pretocmd{\figure}
  7. {%
  8. \ifnumequal%
  9. {\value{chapter}}%
  10. {\value{chapterF@last}}%
  11. {}% already added
  12. {%
  13. \addtocontents{lof}%
  14. {\vspace{5pt}}%
  15. \addtocontents{lof}%
  16. {\protect\numberline{\bfseries\thechapter\quad\thechaptername}}%
  17. \setcounter{chapterF@last}{\value{chapter}}%
  18. }%
  19. }
  20. {}
  21. {}% failed, could put some error message here...
  22. %
  23. %
  24. \pretocmd{\table}
  25. {%
  26. \ifnumequal%
  27. {\value{chapter}}%
  28. {\value{chapterT@last}}%
  29. {}% already added
  30. {%
  31. \addtocontents{lot}%
  32. {\vspace{5pt}}%
  33. \addtocontents{lot}%
  34. {\protect\numberline{\bfseries\thechapter\quad\thechaptername}}%
  35. \setcounter{chapterT@last}{\value{chapter}}%
  36. }%
  37. }
  38. {}
  39. {}% failed, could put some error message here...
  40. }
  41. {}
  42. }


As you can see I check the \figure and \table commands so that I can add chapter to both LoF and LoT. It works and I'm ok with it.

Now the question part.
1) Do you think there is a stronger and safer way to implement it or is it a valid solution? I mean, Did I do the wrong choice in checking \figure and \table? I actually cannot see other solutions.
2) Partially related to the first question. If I put your(mine) code somewhere in the class file, without the variable check, it works ok. If I wrap the code inside an if statement (like the one in the code above) it does not work either if the variable is true or false. I've solved the problem by wrapping the if stamentent in the command \chapters as you can see above. Hence, the question: why the if statement works only inside a command environment? What was wrong with that?

Thanks again.
Bests,
F.

BaCaRoZzo
Posts: 10
Joined: Sun Dec 18, 2011 7:53 pm

Postby BaCaRoZzo » Thu Jan 19, 2012 6:33 pm

Ok,

I've got the answer to the second STUPID question.
Sorry for that.
I didn't realized that the code is executed BEFORE the evaluated variable is set in the main file. Again, sorry for that.

My first question still remains.
Thanks,
F.

5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Postby 5gon12eder » Sun Jan 22, 2012 12:04 am

Glad to see it worked for you and -- even better -- you were able to understand and extend the suggested solution.

I think it's fairly acceptable but if you wish, you could make it a little more portable. For example, you could make it a little package and share the code among multiple documents. Also, you needn't duplicate the code for each floating environment if you write a more generic command that does the hooking. Finally, note the \AtBeginEnvironment command from the etoolbox package.

Here is a suggestion for a very small package. Just name it chaptersinindex.sty and put it somewhere LaTeX can find it.

  1. \NeedsTeXFormat{LaTeX2e}
  2. \ProvidesPackage{chaptersinindex}[2012/01/21]
  3.  
  4. \RequirePackage{etoolbox}
  5.  
  6. \providecommand{\thechaptername}{}
  7. \renewcommand{\chaptermark}[1]
  8. {%
  9. \markboth{#1}{}%
  10. \renewcommand{\thechaptername}{#1}%
  11. }
  12.  
  13.  
  14. \newcommand%
  15. {\AddChaptersToIndex}[2]% #1: index name (e.g. `lof')
  16. % #2: environment to patch (e.g. `figure')
  17. {%
  18. \newcounter{chapter@last@#1}
  19. \AtBeginEnvironment
  20. {#2}
  21. {
  22. \ifnumequal%
  23. {\value{chapter}}%
  24. {\value{chapter@last@#1}}%
  25. {}% already added
  26. {%
  27. \addtocontents{#1}{\protect\numberline{\vspace{\BeforeChapterInIndex}}}%
  28. \addtocontents%
  29. {#1}%
  30. {\protect\numberline{\ChapterInIndex{\thechapter}{\thechaptername}}}%
  31. \setcounter{chapter@last@#1}{\value{chapter}}%
  32. \addtocontents{#1}{\protect\numberline{\vspace{\AfterChapterInIndex}}}%
  33. }
  34. }
  35. }
  36.  
  37. \newlength{\BeforeChapterInIndex}
  38. \setlength{\BeforeChapterInIndex}{0ex plus 1ex}
  39. \newlength{\AfterChapterInIndex}
  40. \setlength{\AfterChapterInIndex}{0ex}
  41.  
  42. \newcommand{\ChapterInIndex}[2]% #1: number, #2: title
  43. {{\bfseries#1\quad#2}}


Then in your document, the relevant code reduces to

  1. \usepackage{chaptersinindex}
  2.  
  3. \AddChaptersToIndex{lof}{figure}
  4. \AddChaptersToIndex{lot}{table}


If the user (you) wish, she can redefine the macro \ChapterInIndex that takes the chapter number as its first and the chapter name as its second argument and typesets them in the indices. Also, the dimensions \BeforeChapterInIndex and \AfterChapterInIndex can be changed by the user to adjust the spacing before and after the chapter entries. Note however that these dimensions add to the height of a single line of text. You could make the solution even more flexible by allowing the heading level (\chapter at the moment) to be specified as a package option so the package would also be useful to an article, say.

The posted solution is not so nice however in the way that the \renewcommand{\chaptermark}[1]... stuff is likely to be broken by a user who uses customized headings. Maybe you have a better idea for implementing that.

Happy TeXing!
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).

BaCaRoZzo
Posts: 10
Joined: Sun Dec 18, 2011 7:53 pm

Postby BaCaRoZzo » Sun Jan 22, 2012 2:24 pm

Hi again Egon and thanks for your time! Your suggestions are always inspiring. :)

I can just add that I TOTALLY agree with your point. I also was a little afraid for the \chaptermark breakable code. About the duplicated code, actually I didn't think about that! I could have been smarter...

I'm working on a thesis template for my PhD course and I would love to make it as usable as possible but also robust. Since only ME is using the template right now, I'm going to concentrate on my thesis: time is running out! :o

I partially implemented your code (the parametrization and the usage of the "AtBeginEnvironment" command). I will fully implement your suggestions as soon as the template will be released as the official template. Credits will also go to you! ;)

I got other two questions. As you have seen, I added a little spacing (cleverly transformed by you in commands "BeforeChapterInIndex" and "AfterChapterInIndex"). It is actually a workaround for not splitting a chapter title and its figures/tables. I try to explain better. Currently, rows are print to fill the page (obviously). If a chapter title is print at the end of a page and there's no more space, its figures/tables are printed on the next page. Which is quite ugly in my opinion. I'm not aware of TOC behaviour, but I would love to force chapter printing to the new page IF it is not possible to print AT LEAST a picture/table reference under it, in the current page. Is there a way to do that? Some already made code that can borrowed?

Another reason for adding space was that the distance between chapter in TOC and in LOFT seemed greater. Is there a specific variable defining the distance in TOC? In case that would be the default value for LOFT.

Thanks again for your time. My apologies for the bad English.
Bests,
F.

5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Postby 5gon12eder » Sun Jan 22, 2012 5:53 pm

You can tell LaTeX that a certain place would be a good place for a page break via \pagebreak[n] where n is a number between 1 and 4. The higher the better for breaking. Similairly, \nopagebreak[n] does quite the opposite. I think that should work for your purpose. Simply add it to the \vspace.
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 4 guests