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.

If I have a section like so

\section{Section 1}

then in the ToC, so long as I use the hyperref package, it is linked to that particular section. However, if I use an href tag as the section name,like so

\section{\href{http://www.google.com}{Section 1}}

In the ToC it goes directly to google.com (not what I want) instead of Section 1 (what I want). Is there any way to have it such that when I click on the Section title, it goes to the link, but when I click on the section in the ToC it goes to the Section (and not the link)?

Minimal example

\documentclass{article}

\usepackage{hyperref}
\usepackage{xspace}

\hypersetup{colorlinks=true, 
   linkcolor=[rgb]{0.192157,0.309804,0.309804},
   urlcolor=cyan, 
   citecolor=blue}

\newcommand{\latex}{\LaTeX\xspace}

\begin{document}
   \title{Minimal \latex\ Example}
   \date{}
   \vspace{65 mm}
   \author{Julius Caesar}

   \maketitle

   \clearpage

   \tableofcontents

   \clearpage

   \section{Section 1}
   \label{sec:one}
   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

   \section{\href{http://www.google.com}{Section 2}}
   \label{sec:two}
   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum


\end{document}
share|improve this question
    
What should happen to the link in chapter marks (“running head”) in for example the book class? –  Qrrbrbirlbel Apr 25 '13 at 0:05
    
@Qrrbrbirlbel I'm sorry, I have no idea what you are talking about –  puk Apr 25 '13 at 0:08

2 Answers 2

cmhughes’ answer is the first thing I thought of too. But it is a rather manual solution.

By \lettting \href to \@secondoftwo its definition changes to

\renewcommand{\href}[2]{#2}

(which we also could have done without any actual knowledge of \@secondoftwo).

The .toc file and the Table of Contents still contain \href{www.google.com}{Section 2} but \href then expands to Section 2, its second argument.

Enclosing this in \begingroup … \endgroup keeps the change to \href local to \killHref. (There are other ways to restore the original definition of \href without the use of this grouping, though.)


If the editor of your choice gives you trouble, replace

\makeatletter
\newcommand*{\killHref}[1]{\begingroup\let\href\@secondoftwo#1\endgroup}
\makeatother

with

\newcommand*{\killHref}[1]{\begingroup\renewcommand{\href}[2]{##2}#1\endgroup}

References

Code

\documentclass{article}
\usepackage{hyperref}
\usepackage{lipsum}
\hypersetup{colorlinks=true, 
   linkcolor=[rgb]{0.192157,0.309804,0.309804},
   urlcolor=cyan, 
   citecolor=blue}

\makeatletter
\newcommand*{\killHref}[1]{\begingroup\let\href\@secondoftwo#1\endgroup}
\makeatother
\begin{document}
    \killHref{\tableofcontents}
    \section{Section 1}
    \lipsum[1]
    \section{\href{http://www.google.com}{Section 2}}
    \lipsum[2]
\end{document}

Output

enter image description here

share|improve this answer
    
Can you briefly describe what \makeatletter\let\href\@secondoftwo does. Thank You –  puk Apr 25 '13 at 0:05
    
I like your solution more cmhughes', but it gives me the following error in vim `Do not use @ in LaTeX macro names. "\@secondoftwo". –  puk Apr 25 '13 at 0:18
    
@puk Erm, yes, well … The @ is usually not a letter that is allowed in macro names, hence the \makeatletter (see reference). If you want to satisfy your editor, use \newcommand*{\killHref}[1]{\begingroup\renewcommand{\href}[2]{##2}#1\endgroup} instead, though you may ignore this warning (not an error per se). –  Qrrbrbirlbel Apr 25 '13 at 0:24
    
What line would I substitute with that? –  puk Apr 25 '13 at 0:31
    
@puk I have updated the answer with a hopefully good explanation what to substitute. –  Qrrbrbirlbel Apr 25 '13 at 0:58

This can be fixed once you know that each heading command takes an optional argument for the toc.

In your case, you can use, for example,

   \section[Section 2 in toc]{\href{http://www.google.com}{Section 2}}

Here's a complete minimal working example (MWE).

\documentclass{article}

\usepackage{hyperref}

\hypersetup{colorlinks=true, 
   linkcolor=[rgb]{0.192157,0.309804,0.309804},
   urlcolor=cyan, 
   citecolor=blue}


\begin{document}
   \title{Minimal Example}
   \author{Julius Caesar}

   \maketitle

   \clearpage

   \tableofcontents

   \clearpage

   \section{Section 1}
   \label{sec:one}
   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

   \section[Section 2 in toc]{\href{http://www.google.com}{Section 2}}
   \label{sec:two}
   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum


\end{document}
%
share|improve this answer

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.