3

I wanted TOC not to have section numbers displayed. So instead of using the star version of sections, found that simply adding \setcounter{secnumdepth}{0} does that.

But then I found that once package titlesec was added, links inside the pdf no longer work.

Here is MWE, which generates 10 sections and some subsection. Clicking on links in the PDF does nothing. Once titlesec is removed, all links work fine.

\documentclass[12pt]{article}    
\usepackage{hyperref}
\usepackage{titlesec}

\usepackage{lipsum}
\usepackage{luacode}    

\begin{luacode*}%this so I do not have to type 10 sections by hand
function generateFewSections(n)
  local i

  for i = 1, n do
    s="\\section{section " .. i .. "}\n \\lipsum[1]\n "
      .. "\\subsection{subsection 1}\n \\lipsum[1]\n "
      .. "\\subsection{subsection 2}\n \\lipsum[1]\n "

    tex.print(s)    
  end
end
  
\end{luacode*}
\newcommand\generateFewSections[1]{\directlua{generateFewSections(#1) }}%

\begin{document}    
\setcounter{secnumdepth}{0}
\setcounter{tocdepth}{1}
\tableofcontents    
\generateFewSections{10}
\end{document}

Compiled on Linux using lualatex C.tex this shows pdf links do not work. i.e. clicking on link does not jump to the section in the pdf.

enter image description here

Removing titlesec links work OK now.

This only happens when adding \setcounter{secnumdepth}{0}. Removing this, then now titlesec does not cause any issue.

If this by design or is there a bug here? Can I use \setcounter{secnumdepth}{0} and also use titlesec?

TL 2025, updated about 2-3 weeks ago.

1 Answer 1

5

Just load titlesec before hyperref, which is a standard thing to try.

\documentclass[12pt]{article}
\usepackage{titlesec}
\usepackage{hyperref}

\usepackage{lipsum}

\ExplSyntaxOn
\newcommand\generateFewSections[1]
 {
  \int_step_inline:nn {#1}
   {
    \section{Section~##1} \lipsum[1]
    \subsection{Subsection~1-##1} \lipsum[1]
    \subsection{Subsection~2-##1} \lipsum[1]
   }
 }
\ExplSyntaxOff

\setcounter{secnumdepth}{0}
\setcounter{tocdepth}{1}

\begin{document}

\tableofcontents

\generateFewSections{10}

\end{document}

(I replaced your faulty Lua function with working code.)

output

The picture shows the preview of the link target.

1
  • I understand hyperref better be last package. But my setup was working OK as is, and the problem only showed when I added \setcounter{secnumdepth}{0}. Without adding this, there was no problem before, even if hyperref was before titlesec. But I now made sure hyperref is the very last package loaded. Commented Jan 15 at 19:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.