Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How to link section titles back to toc? I want to use only hyperref and not any toc related packages (answer with titlesec can be found here)

I can do the hypertarget to toc sections (answer provided by cmhughes and shown in the MWE below). Thus, my question really is: where to put the hyperlink line?

\documentclass{report}
\usepackage{hyperref}

\let\oldcontentsline\contentsline%
\renewcommand\contentsline[4]{%
\hypertarget{toc#4}{}%
\oldcontentsline{#1}{#2}{#3}{#4}}

\begin{document}
\tableofcontents
\chapter{A Chapter to act as a Link}
\section{A Section to act as a Link}
\end{document}
share|improve this question
    
Like this? –  Symbol 1 Sep 1 at 17:07
1  
If he do not update the package in 24hours, should he donate something? –  Symbol 1 Sep 1 at 17:32
1  
You said below that you are developing your own class. Package hyperref should be loaded last (there are some exceptions). Think about how you are going to deal with that. This is a very important thing and if you don't take care of any possible solution, something is about to break. –  Johannes_B Sep 3 at 8:00

3 Answers 3

up vote 2 down vote accepted

Not sure if this is what you want

\documentclass{report}
\usepackage[colorlinks]{hyperref}

\let\oldcontentsline\contentsline%
\renewcommand\contentsline[4]{%
    \oldcontentsline{#1}{\smash{\raisebox{1em}{\hypertarget{toc#4}{}}}#2}{#3}{#4}}
\newcommand\mychapter[1]{%
    \chapter[#1]{\protect\hyperlink{tocchapter.\thechapter}{#1}}}
\newcommand\mysection[1]{%
    \section[#1]{\protect\hyperlink{tocsection.\thesection}{#1}}}
\newcommand\mysubsection[1]{%
    \subsection[#1]{\protect\hyperlink{tocsubsection.\thesubsection}{#1}}}

\begin{document}
    \tableofcontents
    \mychapter{A Chapter to act as a Link}
        \mysection{A Section to act as a Link}
            \mysubsection{A Subection to act as a Link}
            \mysubsection{A Subection to act as a Link}
        \mysection{A Section to act as a Link}
            \mysubsection{A Subection to act as a Link}
            \mysubsection{A Subection to act as a Link}
    \mychapter{A Chapter to act as a Link}
        \mysection{A Section to act as a Link}
            \mysubsection{A Subection to act as a Link}
            \mysubsection{A Subection to act as a Link}
        \mysection{A Section to act as a Link}
            \mysubsection{A Subection to act as a Link}
            \mysubsection{A Subection to act as a Link}
    \clearpage~
\end{document}
share|improve this answer
    
Sorry, not really. I am working on my own class file. Thus, the command that creates the link must be \section. –  Mappi Sep 2 at 13:36
1  
@Mappi I am a bit confused here. You say you are developing a class, which requires some experience. But above you wrote that you are a novice. Sounds pretty contradictory. –  Johannes_B Sep 3 at 7:53

Ideally, one would just use an \apptocmd command (from etoolbox package) or \xapptocmd (from xpatch package) in order to hook the appropiate hypertarget/hyperlink command, but unfortunately, this breaks due to the definition of the structuring commands (such as \part, \chapter etc.)

A generic 'slight' redefinition of those commands provides this, however. At first, a generic structuring command is used, at the begin of a document, a loop of the (seven) commands defines those commands and adds the link. Otherwise, those commands behave as their original definitions (therefore, \LaTeXStandardpart etc. has to be defined, via a loop and using \csletcs command).

\documentclass{book}

\usepackage{etoolbox}%
\usepackage[final,pdftex]{hyperref}%

\listgadd{\StructureCommandsList}{}%


\newcommand{\LetLaTeXStandardSectionCommand}[1]{%
  \csletcs{LaTeXStandard#1}{#1}%
}%

\makeatletter%

\apptocmd{\tableofcontents}{\phantomsection\hypertarget{document::toc}{}}{}{}%

\newcommand{\redefinestructurecommands}[1]{%
  \csgdef{unstarred#1@@noopt}##1{%
    \csuse{unstarred#1@@opt}[##1]{##1}%
  }%

  \csgdef{unstarred#1@@opt}[##1]##2{%
    \csuse{LaTeXStandard#1}[##1]{\hyperlink{document::toc}{##2}}%
  }%


  \csgdef{unstarred#1}{%
    \@ifnextchar[{%
      \@nameuse{unstarred#1@@opt}%
    }{%
      \@nameuse{unstarred#1@@noopt}%
    }%
  }%

  \csgdef{starred#1@@noopt}##1{%
    \csuse{LaTeXStandard#1}*{\hyperlink{document::toc}{##1}}%
  }%


  \csgdef{unstarred#1}{%
    \@ifnextchar[{%
      \csuse{unstarred#1@@opt}%
    }{%
      \csuse{unstarred#1@@noopt}%
    }%
  }%

  \csgdef{#1}{%
    \@ifstar{%
      \csuse{starred#1@@noopt}%
    }{%
      \csuse{unstarred#1}%
    }%
  }%
}%

\makeatother


\newcommand{\RedefineStructuringCommands}{%
   % If some of the structuring commands should not use linking back to toc, just remove them from this csv - list here (say for subparagraph))
  \forcsvlist{\listgadd{\StructureCommandsList}}{part,chapter,section,subsection,subsubsection,paragraph,subparagraph}%
  \forlistloop{\LetLaTeXStandardSectionCommand}{\StructureCommandsList}%
  \forlistloop{\redefinestructurecommands}{\StructureCommandsList}%
}%


\AtBeginDocument{%
  \RedefineStructuringCommands%
}%


\setcounter{secnumdepth}{5}% For demo only
\setcounter{tocdepth}{5}% For demo only

\begin{document}
\chapter*{Introduction}%

\tableofcontents%

\part{First part}%
\chapter{First chapter}%
\section{First Section}%
\section*{First starred section}%
\subsection{First subsection}%
\subsubsection{First subsubsection}%
\paragraph{First paragraph}
\subparagraph{First subparagraph}%

\end{document}

I omitted a screenshot, as it does not reveal really anything.

Note: The above redefinition command \redefinestructurecommand is part of an yet to be published package, it's usually longer and more sophisticated ;-)

share|improve this answer
1  
@Mappi - The only real package that's loaded in this answer is hyperref. (The commands of the etoolbox package are used only to patch existing macros.) In particular, titlesec is not loaded. I thought that not loading titlesec was the only real constraint. –  Mico Sep 1 at 17:54
1  
@Mappi: I think, elegance is a term somewhat arguable ;-) As Mico stated: You did not write, that only hyperref is allowed until some 40 minutes ago. I can readily delete my solution and use my code for other purposes ;-) –  Christian Hupfer Sep 1 at 18:27
1  
@Mappi - Your requirement that hyperref be the only additional package allowed is puzzling, the more so because you also want any solution to be "elegant". Note that Christian's use of the etoolbox package is (basically) confined to that package's high-level macros \apptocmd and \csgdef. For all intents and purposes, Christian's answer could be re-written using only the low-level TeX primitives that were used to construct the two high-level macros. But nothing would be gained -- certainly not elegance. The macros of the etoolbox package are well debugged -- why not use them? –  Mico Sep 2 at 13:17
    
@Mico: Thanks for your support. I can live without acceptance of my answer, if there are better ones. I will try to rewrite with more low-level approaches, for my own purposes. –  Christian Hupfer Sep 2 at 15:43
    
@Mappi: You could have readily asked about the solution before rejecting it, about etoolbox features etc. –  Christian Hupfer Sep 2 at 15:44

I think this was something I was after for. Thank you Symbol 1 for the idea!

\documentclass{report}
\usepackage{hyperref}

\let\oldcontentsline\contentsline%
\renewcommand\contentsline[4]{%
\hypertarget{toc#4}{}%
\oldcontentsline{#1}{#2}{#3}{#4}}
\let\oldsection\section
\renewcommand\section[1]{%
    \oldsection[#1]{\protect\hyperlink{tocsection.\thesection}{#1}}}

\begin{document}
\tableofcontents
\chapter{A Chapter to act as a Link}
\section{A Section to act as a Link}
\end{document}
share|improve this answer
    
It’s OK to Ask and Answer Your Own Questions. But I wonder whether you can accept your answer, technically. I have never tried it before. –  Symbol 1 Sep 2 at 13:37
    
@Mappi: You never said, that you only want to have only chapter and sections linked. –  Christian Hupfer Sep 2 at 15:44
    
@Mappi: Try you solution with some starred section (of course, not to appear in toc, no hyperlink), such as \section*{This should not be in ToC} or with \section[In ToC]{Not in Toc} You will be astonished ;-) –  Christian Hupfer Sep 3 at 6:32
    
@ChristianHupfer: Good point! Either case does not really matter where I need it though. –  Mappi Sep 3 at 7:12
    
@Mappi: Then it's not a problem, unless you don't plan to do the same for chapters as well ;-) You might have a problem then... –  Christian Hupfer Sep 3 at 7:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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