125

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?

3 Answers 3

171

\hyperref is your friend:

\usepackage{hyperref}

%... other code

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

\hyperref[sec:hello]{Word of text}
8
  • 29
    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. Commented Apr 19, 2016 at 12:20
  • 2
    There is also the \nameref and on the question tex.stackexchange.com/questions/121865/… they create the \fullref command.
    – user
    Commented May 2, 2017 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" Commented Feb 1, 2018 at 9: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. Commented Feb 1, 2018 at 20:02
  • 1
    @RussAbbot Try the following code in the document part and review the page: ` \ifcsname [email protected]\endcsname Hyperref is loaded (\csname [email protected]\endcsname). \else Hyperref is not loaded. \fi` Commented Jun 27, 2020 at 10:13
24

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.

1
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}

7
  • 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
    Commented Aug 18, 2019 at 4:42
  • It's discussed in section 4.1 of the hyperref docs here (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. Commented Aug 19, 2019 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. Commented Aug 19, 2019 at 14:01
  • 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
    Commented Aug 19, 2019 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. Commented Aug 19, 2019 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .