2

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.

enter image description here

But ofcourse what comes out now is this:

enter image description here

and from pdflatex as

enter image description here

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?

3

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
\let\@notlinked\relax
\def\notlinked{\noexpand\@notlinked}
\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
    \ifcase\Hy@linktoc % none
      \csname l@#1\endcsname{#2}{#3}%
    \or % section
      \csname l@#1\endcsname{%
        \in@{\@notlinked}{#2}
        \ifin@
           \def\@notlinked{\egroup\hyper@linkend\bgroup}
           \hyper@linkstart{link}{\Hy@tocdestname}\bgroup#2\egroup 
        \else
          \hyper@linkstart{link}{\Hy@tocdestname}\bgroup#2\egroup\hyper@linkend
        \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}}{%
          \hyper@linkstart{link}{\Hy@tocdestname}{#3}\hyper@linkend
        }%
      \fi
    \else % all
      \def\Hy@temp{#3}%
      \ifx\Hy@temp\ltx@empty
        \csname l@#1\endcsname{%
          \hyper@linkstart{link}{\Hy@tocdestname}{#2}\hyper@linkend
        }{}%
      \else
        \csname l@#1\endcsname{%
          \hyper@linkstart{link}{\Hy@tocdestname}{#2}\hyper@linkend
        }{%
          \hyper@linkstart{link}{\Hy@tocdestname}{#3}\hyper@linkend
        }%
      \fi
    \fi
  \fi
}

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

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

\end{document}

The result is:

enter image description here

  • 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

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.