2

I'm trying to write my own class and while I'm didn't use hyperref package following code work properly.

\newcommand\chaptindent{\advance\leftskip 1.0em \hskip -\leftskip}
\newcommand\appenindent{\advance\leftskip 6.0em \hskip -\leftskip}

\def\@chapter[#1]#2{
  \ifnum \c@secnumdepth > \m@ne
    \if@mainmatter                 
       \refstepcounter{chapter}%
       \addcontentsline{toc}{chapter}%
           {\chaptindent\protect\numberline{\thechapter}\MakeUppercase{#1}}%
    \else
       \refstepcounter{chapter}
       \addcontentsline{toc}{chapter}%
           {\appenindent\nobreak\@chapapp\space\protect\numberline{
                \thechapter}\space{#1}}
    \fi
  \else
    \addcontentsline{toc}{chapter}{\MakeUppercase{#1}}%
  \fi
    \chaptermark{#1}%
    \addtocontents{lof}{\protect\addvspace{10\p@}}%
    \addtocontents{lot}{\protect\addvspace{10\p@}}%
    \@makechapterhead{#2}%
    \@afterheading}

enter image description here

After I had include hyperref package the appendix ToC lines moved over left border. enter image description here

Removed \chaptindent and \appenindent from \@chapter redifinition and got ToC without any standoff enter image description here

Try to fix this issue with \def\l@chapter{\@dottedtocline{1}{0.0em}{1.0em}} command but there are the same indent for chapter and appendix lines. enter image description here

How can I get Properly ToC appendix aligned with using hyperref package?

7
  • This is difficult to tell. At which point do you load the hyperref package? It does redefinitions of the structure level macros
    – user31729
    Commented Dec 1, 2017 at 21:35
  • At the beginning of class. Where all others packages were included with \RequirePackage command.
    – Konstantin
    Commented Dec 1, 2017 at 21:41
  • 1
    Try to load it at the end of the class with \AtEndOfClass or better, do not load it automatically
    – user31729
    Commented Dec 1, 2017 at 21:43
  • 1
    'Didn't work' is no useful description.
    – user31729
    Commented Dec 1, 2017 at 21:49
  • 2
    You shouldn't do the formatting in \addcontentsline (hyperref changes it). Beside this you have quite a number of spurious spaces in your code. Commented Dec 1, 2017 at 22:49

1 Answer 1

2

I have solved this problem. So there is solution which may be useful for someone. Appendices are the same chapters and, so for showing them in table of contents uses same command as for chapters and it's \l@chapter. This command is formed while \contentsline{chapter}{... items handling.For differentiate chapters and appendices needs to form different \contentsline items in .toc file, as follow \contentsline{chapter}{... and \contentsline{appendix}{.... For handling \contentsline{appendix}{... it have been needed to implement \l@appendix command. After this I got opportunity to separate handling for chapters and appendices whereas table of contents is forming.

\def\@chapter[#1]#2{\ifnum\c@secnumdepth>\m@ne%
                    \if@mainmatter%                 
                      \refstepcounter{chapter}%
                      \addcontentsline{toc}{chapter}%
    {\protect\numberline\thechapter\texorpdfstring{\MakeUppercase{#1}}{#1}}%
                    \else%
                      \refstepcounter{chapter}%
                      \phantomsection
                      \addcontentsline{toc}{appendix}%
    {\texorpdfstring{\@chapapp\space\protect\numberline\thechapter\space#1}
     {\@chapapp\space\thechapter\space#1}}%
                    \fi%
                    \else%
                      \addcontentsline{toc}{chapter}{#1}%
                    \fi%
                      \chaptermark{#1}%
                      \addtocontents{lof}{\protect\addvspace{10\p@}}%
                      \addtocontents{lot}{\protect\addvspace{10\p@}}%
                      \@makechapterhead{#2}%
                      \@afterheading}

So this code generate following .toc file content

...
\contentsline {chapter}{\numberline 2\MakeUppercase {\IeC ...
...
\contentsline {appendix}{{\Ukrainian \CYRD ...

which is handled by two commands

\renewcommand\l@chapter[2]{%
   \ifnum \c@tocdepth >\m@ne
    \vskip \z@ \@plus.2\p@
    {\leftskip 0.0em\relax \rightskip \@tocrmarg plus 15em \parfillskip -
  \rightskip%mod <plus 15em> for avoid hyph. in TOC
     \parindent 0.0em\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima 1.0em\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#1}\nobreak
     \leaders\hbox{$\m@th
      \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
         mu$}\hfill
      \nobreak
     \hb@xt@\@pnumwidth{\hfil \normalcolor #2}%   \hfil\normalfont   |||
    \par}%
   \fi}

\newcommand\l@appendix[2]{%
  \ifnum \c@tocdepth >\m@ne
    \vskip \z@ \@plus.2\p@
    {\leftskip 0.0em\relax \rightskip \@tocrmarg plus 15em \parfillskip -
 \rightskip%mod <plus 15em> for avoid hyph. in TOC
     \parindent 0.0em\relax \hangindent 4.3em\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima 1.5em\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#1}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
       mu$}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil \normalcolor #2}%   \hfil\normalfont   |||
     \par}%
  \fi}

\phantomsection used for hyperref links works correct. enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .