4

Shortly put, the links in table of contents do not work the way I'd like. For the second subsection, Subsection B, all subsubsection links lead instead to the subsubsections in the first subsection, Subsection A.

Picture of table of contents

So, for instance, the link for "subsubsection x" leads instead to "subsubsection b".

Here is a small copyable example to replicate the issue.

\documentclass{article}

\usepackage[pdftex]{graphicx}
\usepackage[colorlinks=true, linkcolor=blue]{hyperref}

\begin{document}

\tableofcontents
\subsection*{Subsection A}
    \addcontentsline{toc}{subsection}{Subsection A}
    \setcounter{subsubsection}{0}
    \subsubsection{subsubsection a}
        \newpage
    \subsubsection{subsubsection b}
        \newpage
    \subsubsection{subsubsection c}
        \newpage
    \subsubsection{subsubsection d}
        \newpage

\subsection*{Subsection B}
    \addcontentsline{toc}{subsection}{Subsection B}
    \setcounter{subsubsection}{0}
    \subsubsection{subsubsection a}
        \newpage
    \subsubsection{subsubsection x}
        \newpage
    \subsubsection{subsubsection y}

\end{document}

The problem appears to be the simultaneous usage of subsection* (i.e the starred version) and setcounter{subsubsection}{0} twice. If your solution to this problem does not use these commands, it is perfectly fine. However, it must still be true that

  • "Subsection A" and "Subsection B" appear without numbers in the document;
  • "Subsection A" and "Subsection B" show up in the table of contents;
  • the numbering for subsubsections starts again from 0.0.1 in both subsections.

The file was compiled multiple consecutive times with pdflatex.

How do I fix this problem?

3
  • A MWE, please. Never aeen any such mess with packages geared towards larger works, except when doing a major reorganization leaving stale intermediate files around. Clean up everything, and try again (will need to run latex several times).
    – vonbrand
    Feb 5 '20 at 0:19
  • You might also be able to achieve this effect using titlesec. Article class uses \@startsection for \section, \subsection, etc. which makes it a pain to modify. Feb 5 '20 at 4:53
  • @vonbrand Isn't what I included already an MWE? It is also stated that 'The file was compiled multiple consecutive times with pdflatex.' Nevertheless, thank you for the comment. Feb 5 '20 at 11:47
2

You can use option hypertexnames=false for package hyperref:

\documentclass{article}
\usepackage{graphicx}
\usepackage[colorlinks=true, linkcolor=blue
  ,hypertexnames=false% <- added
]{hyperref}

\begin{document}
\tableofcontents

\subsection*{Subsection A}
\addcontentsline{toc}{subsection}{Subsection A}
\setcounter{subsubsection}{0}
\subsubsection{subsubsection a}\newpage
\subsubsection{subsubsection b}\newpage
\subsubsection{subsubsection c}\newpage
\subsubsection{subsubsection d}\newpage

\subsection*{Subsection B}
\addcontentsline{toc}{subsection}{Subsection B}
\setcounter{subsubsection}{0}
\subsubsection{subsubsection a}\newpage
\subsubsection{subsubsection x}\newpage
\subsubsection{subsubsection y}
\end{document}

Additional remark: If you load package titlesec, then you have to use \subsection*{\phantomsection Subsection X} (where X = A or X = B) instead of simply \subsection*{Subsection X} to set an anchor for the links manually.

8
  • This seems to work nicely, great! (I'm not currently at home, so I compiled instead with Overleaf's online pdfLaTeX). Sadly, I cannot use this solution with my real document. That document also has \usepackage{titlesec} which breaks this solution (at least with Overleaf's pdfLaTeX). With titlesec included, the hyperlinks for subsubsections all work, but hyperlinks for subsections A and B do not work. Any ideas? Feb 5 '20 at 11:46
  • 2
    With package titlesec you have to add \phantomsection before \addcontentsline to set an anchor for the link manually.
    – esdd
    Feb 5 '20 at 12:04
  • I'm not entirely sure where you meant to add \phantomsection in your comment, however adding \phantomsection a line before both \subsection*'s fixes the problem, indeed. What does \phantomsection do here exactly? Feb 5 '20 at 12:35
  • 1
    From the hyperref documentation, Section 5.27 \phantomsection: »Set an anchor at this location. It is often used in conjunction with \addcontentsline for sectionlike things (index, bibliography, preface). \addcontentsline refers to the latest previous location where an anchor is set.«
    – esdd
    Feb 5 '20 at 15:52
  • 1
    Then you have to ensure that \phantomsection puts the anchor on the same page as the \subsection*. Note there could be an automatic page break before \subsection*. Maybe \subsection*{\phantomsection ...} would be better?
    – esdd
    Feb 7 '20 at 20:21
1

I'd define a special command for this and use standard functions for resetting the subsubsection counter when \subdivision is issued.

\documentclass{article}

\usepackage{graphicx}
\usepackage[colorlinks=true, linkcolor=blue]{hyperref}

\newcounter{subdivision}
\counterwithin*{subsubsection}{subdivision}
\renewcommand{\thesubsubsection}{\arabic{subsubsection}}

\newcommand{\subdivision}[1]{%
  \refstepcounter{subdivision}%
  \subsection*{#1}%
  \addcontentsline{toc}{subsection}{#1}%
}

\begin{document}

\tableofcontents
\subdivision{Subsection A}

\subsubsection{subsubsection a}
        \newpage
\subsubsection{subsubsection b}
        \newpage
\subsubsection{subsubsection c}
        \newpage
\subsubsection{subsubsection d}
        \newpage

\subdivision{Subsection B}

\subsubsection{subsubsection a}
        \newpage
\subsubsection{subsubsection x}
        \newpage
\subsubsection{subsubsection y}

\end{document}

enter image description here

You can check that the links point to the right places.

5
  • Great answer, thank you (and apologies for delay). It does indeed work as advertised. Two questions if I may. (1) What exactly is the function of \renewcommand{\theHsubsubsection}{\thesubdivision.\arabic{subsubsection}}? Without it, I seem to achieve the same output and no errors. Is it for some referencing purpose with \ref? (2) Secondly, what is the purpose of the per cent symbol % in newcommand? Usually, I use this in text editor for comments. Is is some sort of line break, or signalling to TeX to run the three commands in a consecutive fashion? Feb 6 '20 at 21:01
  • @LinearChristmas Without the \theHsubsection redefinition, you'd get duplicate anchors and links would not refer to the correct spot. % at the end of the line masks off the otherwise generated space.
    – egreg
    Feb 6 '20 at 21:10
  • "...you'd get duplicate anchors and links would not refer to the correct spot" Do the links in the TOC fall under your usage of 'links' in this sentence? If so, I did remove that line before posting my first comment, and everything worked fine :). For the per cent symbol usage in this case, could you please provide a reference where this is discussed (I am too inexperienced to see any difference in the PDF, and since this is a tangent to the question, I think I cannot expect you to keep explaining it to me). Feb 6 '20 at 21:21
  • 1
    @LinearChristmas Oh, well! I just discovered that hyperref knows by itself how to redefine \theHsubsection. ;-) For % look at tex.stackexchange.com/q/7453/4427, but also tex.stackexchange.com/q/34844/4427
    – egreg
    Feb 6 '20 at 21:33
  • Not a problem! Might've even been a fairly recent update? Anyhow, I'm going to go back on my previous comment a bit, and ask this question anyway. A yes or no answer will suffice :). Will something bad happen (under the hood) in this particular instance if the %'s are removed? (Will check out those two references hopefully during this weekend, no insurance if I can fully understand them, however). Feb 6 '20 at 22:04
1

All you need is \phantomsection from hyperref.

\documentclass{article}

\usepackage[pdftex]{graphicx}
\usepackage[colorlinks=true, linkcolor=blue]{hyperref}

\begin{document}

\tableofcontents

\phantomsection
\subsection*{Subsection A}
    \addcontentsline{toc}{subsection}{Subsection A}
    \setcounter{subsubsection}{0}
    \subsubsection{subsubsection a}
        \newpage
    \subsubsection{subsubsection b}
        \newpage
    \subsubsection{subsubsection c}
        \newpage
    \subsubsection{subsubsection d}
        \newpage

\phantomsection
\subsection*{Subsection B}
    \addcontentsline{toc}{subsection}{Subsection B}
    \setcounter{subsubsection}{0}
    \subsubsection{subsubsection a}
        \newpage
    \subsubsection{subsubsection x}
        \newpage
    \subsubsection{subsubsection y}

\end{document}

It occurred to me that \thesubsection is still used in \thesubsubsection so you need to redefine at least one of these.

\documentclass{article}

\usepackage[pdftex]{graphicx}
\usepackage[colorlinks=true, linkcolor=blue]{hyperref}

\renewcommand{\thesubsection}{\Alph{subsection}}
\renewcommand{\thesubsubsection}{\arabic{subsubsection}}

\begin{document}

\tableofcontents

\refstepcounter{subsection}%
\addcontentsline{toc}{subsection}{Subsection A}%
\subsection*{Subsection A}

    \subsubsection{subsubsection a}
        \newpage
    \subsubsection{subsubsection b}
        \newpage
    \subsubsection{subsubsection c}
        \newpage
    \subsubsection{subsubsection d}
        \newpage

\refstepcounter{subsection}%
\addcontentsline{toc}{subsection}{Subsection B}%
\subsection*{Subsection B}

    \subsubsection{subsubsection a}
        \newpage
    \subsubsection{subsubsection x}
        \newpage
    \subsubsection{subsubsection y}

\end{document}
7
  • I'm currently not at home, so I cannot compile from command line. However, with Overleaf's online pdfLaTeX compiler this does not seem to work. Under "Subsection B", all subsubsection links still lead to incorrect places. Feb 5 '20 at 11:41
  • Did you run it twice? Feb 6 '20 at 4:18
  • Overleaf's online compiler does not require multiple compilations as far as I know (but I did run it three times anyway). I have now also checked it via command line, and indeed the first proposed solution does not work. The second solution does fix the links but the numbers do not, strictly speaking, restart from 0.0.1. Instead, the format is A.#no and B.#no. This really isn't a big issue, I am just being pedantic. (So, you may choose to fix it or not, it's up to you. The format does not have to be exactly 0.0.1 and 0.0.1, just as long as both have the same start). Thank you for your answer. Feb 6 '20 at 20:14
  • The second solution also works when \usepackage{titlesec} is added, this is a plus. Feb 6 '20 at 20:18
  • 0.0.1 means section 0, subsection 0, subsubsecton 1. Since you were using subsecton A. I assumed you wanted to number it that way. Anyway, those are all handled by \thesection, \thesubsecton and \thesubsubsection. Feb 6 '20 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.