bug-texinfo
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: toc multiline section titles misaligned in TeX


From: Gavin Smith
Subject: Re: toc multiline section titles misaligned in TeX
Date: Tue, 18 Jul 2023 15:43:51 +0100

On Mon, Jul 17, 2023 at 04:51:34PM -0600, Karl Berry wrote:
> Another (separate but sort of related) problem that has been in
> texinfo.tex from the beginning is that the toc does not align correctly
> when there is 10+ sections (at any level below chapter). I remember
> noticing this in 1985 or so but never getting around to fixing it :).
> 
> It didn't seem right to just always leave enough space for a two-digit
> section number, since few documents have so many sections. But if any
> sectional unit within a given chapter has >=10 items, I think all should
> be typeset with the two-digit spacing, else it would look weirdly wrong.
> 
> Because of the way toc's are formatted with texinfo.tex, with big
> divisions between chapters, I think it would look best to do the >=10
> check per chapter, but doing it for the whole document is also a viable
> option.

It's possible to do this by reading the toc file another time for the
sole purpose of getting the widths of the section numbers in each chapter.

I've got work in progress on this (at end this message) and will
probably finish it in the next two or three days.  My current work only
handles @chapter and @section headings only, but I don't see in principle
why the same approach couldn't be extended to all sectioning levels.  It
will require redefinitions for every level of sectioning from chapter
through to subsubsection.   Hopefully I am most of the way there and
it will not be excessively complicated to do.

There is a speed penalty to doing this, of course, but it may not be
significant.  I intend to make the code work even if \getsecnowidths
is not called so the process will be easy to bypass in future.

I think the output does not necessarily look ideal.  You can see in
the attached screen clip, in chapter 2, the spacing is very wide
from 2.1 through to 2.9.  I wonder if it would be sensible to try
to narrow this space, leading the spacing after 2.10 and following would
be narrower (say 0.5 em rather than 1 em).  This would be a tweak to
make once the functionality is finished.



diff --git a/doc/texinfo.tex b/doc/texinfo.tex
index 17219e3ec2..74ff61a230 100644
--- a/doc/texinfo.tex
+++ b/doc/texinfo.tex
@@ -6710,6 +6710,35 @@ might help (with 'rm \jobname.?? \jobname.??s')%
   \input \tocreadfilename
 }
 
+\newdimen\curchapmax
+
+% process toc file to find the maximum width of the section numbers for
+% each chapter
+\def\getsecnowidths{%
+  \begingroup
+  \setupdatafile
+  \activecatcodes
+  \secentryfonts
+  % Redefinitions
+  \def\numchapentry##1##2##3##4{%
+    \def\curchapname{secnowidth-##2}%
+    \curchapmax=0pt
+  }%
+  \def\numsecentry##1##2##3##4{%
+    \setbox0=\hbox{##2}%
+    \ifdim\wd0>\curchapmax
+      \curchapmax=\wd0
+      \expandafter\xdef\csname\curchapname\endcsname{\the\wd0}%
+    \fi
+  }%
+  % Discard any output by outputting to dummy vbox, in case the toc file
+  % contains macros that we have not redefined above.
+  \setbox\dummybox\vbox\bgroup
+    \input \tocreadfilename\relax
+  \egroup
+  \endgroup
+}
+
 \newskip\contentsrightmargin \contentsrightmargin=1in
 \newcount\savepageno
 \newcount\lastnegativepageno \lastnegativepageno = -1
@@ -6755,6 +6784,7 @@ might help (with 'rm \jobname.?? \jobname.??s')%
   \startcontents{\putwordTOC}%
     \openin 1 \tocreadfilename\space
     \ifeof 1 \else
+      \getsecnowidths
       \readtocfile
     \fi
     \vfill \eject
@@ -6858,7 +6888,15 @@ might help (with 'rm \jobname.?? \jobname.??s')%
 }
 
 % Chapters, in the main contents.
-\def\numchapentry#1#2#3#4{\dochapentry{#1}{#2}{#4}}
+\def\numchapentry#1#2#3#4{%
+  \expandafter\let\expandafter\savedsecnowidth \csname secnowidth-#2\endcsname
+  \ifx\savedsecnowidth\relax
+    \chapsecnowidth=0pt
+  \else
+    \chapsecnowidth=\savedsecnowidth
+  \fi
+  %
+  \dochapentry{#1}{#2}{#4}}
 
 % Chapters, in the short toc.
 % See comments in \dochapentry re vbox and related settings.
@@ -6899,8 +6937,8 @@ might help (with 'rm \jobname.?? \jobname.??s')%
 % Same as \defaultparindent.
 \newdimen\tocindent \tocindent = 15pt
 
-% Now for the actual typesetting. In all these, #1 is the text and #2 is the
-% page number.
+% Now for the actual typesetting. In all these, #1 is the text, #2 is
+% a section number if present, and #3 is the page number.
 %
 % If the toc has to be broken over pages, we want it to be at chapters
 % if at all possible; hence the \penalty.
@@ -6916,6 +6954,7 @@ might help (with 'rm \jobname.?? \jobname.??s')%
 }
 
 \def\dosecentry#1#2#3{\begingroup
+  \secnowidth=\chapsecnowidth
   \secentryfonts \leftskip=\tocindent
   \tocentry{#1}{#2}{#3}%
 \endgroup}
@@ -6930,6 +6969,14 @@ might help (with 'rm \jobname.?? \jobname.??s')%
   \tocentry{#1}{#2}{#3}%
 \endgroup}
 
+% Used for the maximum width of a section number so we can align
+% section titles.
+\newdimen\secnowidth
+\secnowidth=0pt
+
+\newdimen\chapsecnowidth
+\chapsecnowidth=0pt
+
 % \tocentry{TITLE}{SEC NO}{PAGE}
 %
 \def\tocentry#1#2#3{%
@@ -6939,7 +6986,13 @@ might help (with 'rm \jobname.?? \jobname.??s')%
   \else
     \setbox0=\hbox{#2\labelspace}%
     \entrycontskip=\wd0
-    \entry{#2\labelspace#1}{#3}%
+    \entry{%
+      \ifdim 0pt=\secnowidth
+        #2%
+      \else
+        \hbox to \secnowidth{#2\hfill}%
+      \fi
+      \labelspace#1}{#3}%
   \fi
 }
 








Attachment: clip.png
Description: PNG image


reply via email to

[Prev in Thread] Current Thread [Next in Thread]