122

I would like to make a word of text, that when clicked upon in the PDF file takes me to some section. I'm using the hyperref package. I rejected \nameref because it produces the section title and I want my own link text. I also rejected \hyperlink because it does not work (no link, does not seem to know the section label as a link target).

What's a good way to do this?

Stefan Pinnow
  • 29,535
Thomas
  • 1,927

3 Answers3

163

\hyperref is your friend:

\usepackage{hyperref}

%... other code

\section{Hello World}
\label{sec:hello}

\hyperref[sec:hello]{Word of text}
Heiko Oberdiek
  • 271,626
  • 28
    also have a look at \autoref{sec:hello} which produces automatically text like section 1.1 or figure 4, depending on what you reference. Note that your labels have to have prefixes such as sec, itm, fig etc. – Ciprian Tomoiagă Apr 19 '16 at 12:20
  • 2
    There is also the \nameref and on the question https://tex.stackexchange.com/questions/121865/nameref-how-to-display-section-name-and-its-number they create the \fullref command. – user May 02 '17 at 22:30
  • 1
    Question within an answer to a question, but here goes...Is there a way to properly capitalize for \autoref? For example, when using it at the start of a sentence, it should print "Section 1", but it prints "section 1" – Hamman Samuel Feb 01 '18 at 09:40
  • 2
    @HammanSamuel No, capitalization is not supported. As a workaround, \Autoref could be defined that calls \autoref in a group, where the group starts with redefinition of the "autoref" names with their capitalized variants. – Heiko Oberdiek Feb 01 '18 at 20:02
  • Just came across this. In Overleaf, \hyperref [ ] { } seems to work as described above even without \usepackage{hyperref}. Does that mean it is automatically imported in Overleaf? – RussAbbott Jun 27 '20 at 04:05
  • 1
    @RussAbbot Try the following code in the document part and review the page: \ifcsname ver@hyperref.sty\endcsname Hyperref is loaded (\csname ver@hyperref.sty\endcsname). \else Hyperref is not loaded. \fi – Heiko Oberdiek Jun 27 '20 at 10:13
  • @Heiko Oberdiek. Thanks. Got: Hyperref is loaded (2019/11/10 v7.00c Hypertext links for LaTeX). – RussAbbott Jun 29 '20 at 01:42
22

link to a section

hyperref does the job:

\usepackage{hyperref} % import the package

%... other code

\section{Alice in Wonderland} % a normal section we want to link to \label{sec:Alice} % this is the bookmark for the links which refers to the last section

% links to the section with the variable name Alice, showing: "Some Displayed Text" \hyperref[sec:Alice]{Some Displayed Text}

% links to the section with the variable name Alice showing the name of the Section, here: "Alice in Wonderland" \nameref{sec:Alice}

% Links to the section with the variable name Alice showing the description of the section, here: "section 1" \autoref{sec:Alice}

Example:

Links preview

all 3 will link to

enter image description here

Overleaf Example

link to a section by its number

If you really want to link to a section number (not the section with the number XXX) use:

%link to the section with number XXX   
\hyperlink{section.XXX}{Some Other Displayed Text}
%link to the section with the number 2
\hyperlink{section.2}{Some Other Displayed Text}

Be aware that if you add a section so that the section number changes, this link will point to a different section that now has the number specified. I would highly recommend the solution above which is much more resilient to changes.

Xanlantos
  • 321
7

Hyperref builds section links automatically. You should be able to reference them as section.i where i is the section number. If you wanted to say "Section 3" and link to section 3, it would look as follows:

\hyperlink{section.3}{Section 3}

If you are prone to reorganizing your sections, this is not perfect, and I would advise using one of the above solutions, however, for relatively stable orderings, this is much simpler as it doesn't require adding a \label tag whenever you want a new section.

Testing indicates this also works for other sectioning commands like \chapter (where the reference becomes chapter.1). For nested sectioning commands like \subsection, the reference becomes subsection.1.1.

If you want to reference chapter 3 section 2 you can use the following code:

\hyperlink{section.3.2}{Section 3.2}

  • 1
    Welcome to TeX.SE! That seems improbable at best. This implementation is absolutely not in the spirit of how LaTeX works and the package authors are generally in keeping with that spirit. Have you checked your answer in a new document? In the document in which this works, I suspect you have something in your preamble that allows you to do this. It is generally not advised to have static elements like this in your document. – thymaro Aug 18 '19 at 04:42
  • It's discussed in section 4.1 of the hyperref docs here (https://www.tug.org/applications/hyperref/manual.html#x1-50003.1). ”usually hyperref automatically adds bookmarks for \section and other similar macros.” I've used the feature in general without actively setting other details in the preamble, but I can't swear at the moment that they aren't needed. – user5957401 Aug 19 '19 at 12:44
  • 2
    I've tested a minimal document. \documentclass{article} \usepackage{hyperref} \begin{document} \hyperlink{section.1}{Link is these words} \newpage \section{Introduction} \end{document}

    It works just fine. This makes sense to me as other prominent features in the hyperref package, like bookmarks and toc links, require having references to each section built in.

    – user5957401 Aug 19 '19 at 14:01
  • So you want the OP to manually change stuff if a section is inserted between section 1 and 2? Because now 'section.3' points at the old section 2. That is not good advise. – daleif Aug 19 '19 at 14:16
  • 3
    @user5957401 I stand corrected. I would never have suspected this to exist in a package like hyperref (for reasons mentioned in comment 1 to answer 3 to question 180571). – thymaro Aug 19 '19 at 16:39
  • 2
    @daleif, you are correct, if the sections are reordered, this is an issue -- as I clearly note in paragraph two of my answer. That does not make it a bad answer. My own use case when I came to this page looking for an answer requires rewriting when sections are re-ordered anyhow (end of an intro in an academic paper). My frustration with the pre-existing answers lead me to the hyperref docs. This is nearly the best answer possible in my use case, which I've posted -- with a warning about reorganizations -- so that others may use it. – user5957401 Aug 19 '19 at 19:45
  • @thymaro, I totally understand. I agree, in general it is not in keeping with the spirit of LaTeX, though it is damn convenient when it works. I also would not have been surprised to learn that part of my preamble was critical -- though I didn't believe that to be the case. – user5957401 Aug 19 '19 at 19:48