2

Main question: How can I change the text that prints when I cross-reference an object in my text? I'd like to get a \renewcommand that works for tables only and another one that works for sentences under \begin{enumerate}. A thorough explanation of my issue, my code, and the output images for the corresponding code are located below.

Any help is much appreciated!

When I cross-reference an object, like a table, in LaTeX using the hyperref package, I would like to have the name of the object as part of the link. For example, in the code below we see that the first example of \ref{label} does not mention the name of the object. The second example names it only if I add it manually, but it's not part of the link. And the third example makes the whole phrase a link, but the text of the link needs to be modified manually. (more below).

\documentclass{article}

\usepackage{caption}
\usepackage[colorlinks]{hyperref}

\begin{document}
    \begin{table}
    \centering
    \caption{Table with text}\vspace{-1em}\label{T1}

    \begin{tabular}{ | c | c | c | }
        \hline
        Column1 & Column2 & Column3\\
        \hline
        \hline
        C1-text1 & C2-text1 & C3-text1\\
        \hline
        C1-text2 & C2-text2 & C3-text2\\
        \hline
        C1-text3 & C2-text3 & C3-text3\\
        \hline
    \end{tabular}

    \end{table}

We can see in \ref{T1} that...

We can see in Table \ref{T1}

We can see in \hyperref[T1]{Table 1} that...

\end{document}

enter image description here

However, the third option is not one that I want to stick to, because it brings other issues down the road. For instance, if I add another table before this table, I need to make sure to go back and modify the link-text for each item, or else I end up with a wrongly labeled table in my document. For example:

\documentclass{article}

\usepackage{float} %To make sure the tables display in order.
\usepackage{caption}
\usepackage[colorlinks]{hyperref}

\begin{document}

    \begin{table}
        \caption{Other table}\label{added table}
        \centering
    \begin{tabular}{c c}
        \hline
        C1 & C2\\
        \hline
        T1 & T2\\
        \hline
    \end{tabular}

    \end{table}

We can see in \ref{added table} that


    \begin{table}[H]
        \centering
        \caption{Table with text}\vspace{-1em}\label{T1}

    \begin{tabular}{ | c | c | c | }
        \hline
        Column1 & Column2 & Column3\\
        \hline
        \hline
        C1-text1 & C2-text1 & C3-text1\\
        \hline
        C1-text2 & C2-text2 & C3-text2\\
        \hline
        C1-text3 & C2-text3 & C3-text3\\
        \hline
    \end{tabular}

    \end{table}

We can see in \ref{T1} that...

We can see in Table \ref{T1}

We can see in \hyperref[T1]{Table 1} that...

\end{document}

enter image description here

My question is: how can I provide a \renewcommand so that I can modify the text that is printed when I cross-reference an object in the body of my document? I'd also like for the command to make a distinction between tables and other objects such as sentences with the \begin{enumerate} or \begin{itemize} commands. I don't want to end up calling my sentences Table X nor do I want the enumeration between tables and sentences to get mixed up.

On a side note, do people recommend using the \centering command or the \begin{center} environment for tables? And why?

3

cleveref is your friend. It does out of the box what you are attempting to do and provides further structure for cross-references. Another thing that seems to bother you is the numbering of the labels, it shouldn't. It is a common practice to give a semantic label and let the cross-referencing system take care of the rest. Also, I take the liberty of suggesting you booktabs for nicer looking tables.

\documentclass{article}

\usepackage{float} %To make sure the tables display in order.
\usepackage{caption}
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}
\usepackage{booktabs}

\begin{document}

\begin{table}
  \centering
  \caption{Other table}
  \label{tbl:aboutXYZ}

  \begin{tabular}{cc}
    \toprule
    C1 & C2\\
    T1 & T2\\
    \bottomrule
  \end{tabular}

\end{table}

We can see in \cref{tbl:aboutXYZ} that


\begin{table}[H]
  \centering
  \caption{Table with text}
  \label{tbl:aboutABC}

  \begin{tabular}{ccc}
    \toprule
    Column1 & Column2 & Column3\\
    \midrule
    C1-text1 & C2-text1 & C3-text1\\
    C1-text2 & C2-text2 & C3-text2\\
    C1-text3 & C2-text3 & C3-text3\\
    \bottomrule
  \end{tabular}
\end{table}

We can see in \ref{tbl:aboutABC} that...

We can see in Table \ref{tbl:aboutABC}

We can see in \cref{tbl:aboutABC} that...

Or also in \Cref{tbl:aboutXYZ} that...

\end{document}

enter image description here

1

After doing more researching online and reading the hyperref and cleveref packages (thanks to @gusbrs suggestion), I found two solutions to my question. I'm posting them here for future reference for others.

1) Using the hyperref package, the \autoref command will automatically add the cross-reference name (e.g. Table) to the hyperlink. However, this means that the \autoref command needs to be use every time you want the name of the object to show up in the text. Also, there is not way to modify the text that prints with this command.

2) Using the cleveref package, the \cref or \Cref commands (the difference between these two commands is how the text prints--lowercase or uppercase) add the cross-reference name to the texts, but it does not make it part of the hyperlink. To make the name part of the hyperlink, you need to add the option nameinlink when you list the package in the preamble (i.e. \usepackage[nameinlink]{cleveref}). cleveref also allows you to modify the text that prints. This is presented in the documentation.

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.