Linking the section titles to toc using only hyperref

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}

-
Like this? – Symbol 1 Sep 1 '14 at 17:07
If he do not update the package in 24hours, should he donate something? – Symbol 1 Sep 1 '14 at 17:32
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 '14 at 8:00

Not sure if this is what you want

\documentclass{report}

\let\oldcontentsline\contentsline%
\renewcommand\contentsline[4]{%
\oldcontentsline{#1}{\smash{\raisebox{1em}{\hypertarget{toc#4}{}}}#2}{#3}{#4}}
\newcommand\mychapter[1]{%
\newcommand\mysection[1]{%
\newcommand\mysubsection[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}

-
Sorry, not really. I am working on my own class file. Thus, the command that creates the link must be \section. – Mappi Sep 2 '14 at 13:36
@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 '14 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}%

\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{%
}%

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

\csgdef{starred#1@@noopt}##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))
\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 ;-)

-
@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 '14 at 17:54
@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 '14 at 18:27
@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 '14 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 '14 at 15:43
@Mappi: You could have readily asked about the solution before rejecting it, about etoolbox features etc. – Christian Hupfer Sep 2 '14 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]{%

@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 '14 at 6:32