46

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?

66

\hyperref is your friend:

\usepackage{hyperref}

%... other code

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

\hyperref[sec:hello]{Word of text}
  • 16
    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
  • 1
    There is also the \nameref and on the question tex.stackexchange.com/questions/121865/… they create the \fullref command. – user May 2 '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 1 '18 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. – Heiko Oberdiek Feb 1 '18 at 20:02
2

\hyperref does the job:

\usepackage{hyperref}

%... other code

\section{Alice in Wonderland}
\label{sec:Alice}

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

%Links to the section with variable name Alice showing the name of the Section here "Alice in Wonderland"
\nameref[sec:Alice]

%Links to the section with variable name Alice showing the description of the section here "section 1"
\hyperref[sec:Alice]{Some Displayed Text}

Example: https://v2.overleaf.com/read/cbxfjrpmjcqq

  • 2
    Welcome to TeX.SX! Please post code directly into the body of the answer, so that in case the link ceases to exist, your answer will still be valid. – Phelype Oleinik Jul 30 at 21:41
  • Well, that was already mentioned in the accepted answer ... – Mensch Jul 30 at 22:06
  • to be clear all you need is in the code already the example only contains a rendered view (pdf) even without the link it would be a valid answer, right? – Xanlantos Jul 31 at 10:16
1

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.

New contributor
user5957401 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 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 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. – user5957401 yesterday
  • 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 yesterday
  • 1
    @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 yesterday
  • 1
    @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 yesterday

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.