# how to prevent part of section name from being part of the hyperlink generated automaticaly in TOC?

sometimes I'd like part of the section name text (and subsection and anything that goes to TOC) to show up but without it being part of the link that is generated automatically by pdflatex and by htlatex.

This way I can annotate a section name in TOC, but not have the annotation by part of the href text itself.

A simple example will help. Given this .tex file, where I put the text that I do not want be part of the link inside \small{} just for illustration. (I know this is wrong way to make font small in \section but I do not know any better now).

\documentclass[12pt,titlepage]{article}%

\usepackage{hyperref}

\begin{document}
\tableofcontents
\section{HW1 {\small{this hw was on subject x}}}
jdkfjdkfjdf

\section{HW2 {\small{this hw was on subject y but is also related to subject x}}}

\end{document}


I'd like the TOC generated to look like this below which I drew this to show the part of the name I want to underlinked.

But ofcourse what comes out now is this:

and from pdflatex as

one can see that the whole text inside \section is underlined.

The commands used: htlatex foo.tex and pdflatex foo.tex

question: Is there a trick or option to tell latex/pdflatex not to include part of the section in the hyperref link when making TOC?

Here a suggestion. I don't know if there are any drawbacks.

Use the command \notlinked inside section to place the end of the hyperlink.

\documentclass[12pt,titlepage]{article}%

\usepackage{hyperref}
\makeatletter
\def\contentsline#1#2#3#4{%
\begingroup
\Hy@safe@activestrue
\edef\x{\endgroup
\def\noexpand\Hy@tocdestname{#4}%
}\x
\ifx\Hy@tocdestname\ltx@empty
\csname l@#1\endcsname{#2}{#3}%
\else
\csname l@#1\endcsname{#2}{#3}%
\or % section
\csname l@#1\endcsname{%
\ifin@
\else
\fi
}{#3}%
\or % page
\def\Hy@temp{#3}%
\ifx\Hy@temp\ltx@empty
\csname l@#1\endcsname{#2}{#3}%
\else
\csname l@#1\endcsname{{#2}}{%
}%
\fi
\else % all
\def\Hy@temp{#3}%
\ifx\Hy@temp\ltx@empty
\csname l@#1\endcsname{%
}{}%
\else
\csname l@#1\endcsname{%
}{%
}%
\fi
\fi
\fi
}

\begin{document}
\tableofcontents
\section{HW1\notlinked {\small{ this hw was on subject x}}}
jdkfjdkfjdf


• Thanks alot. It worked with pdflatex, but htlatex foo.tex shows the link over the whole section name for some reason. That's always the problem I see many times. What works in pdflatex does not always work the same way with htlatex. Thanks again. – Nasser Jun 8 '13 at 15:14
• @Nasser: Indeed. tex4ht.sty has it's one definition. I am not familiar with this syntax. – Marco Daniel Jun 8 '13 at 15:30