2

I am using \ref to make cross references inside my article.

This works good but the text then will appear is like

In the next chapter 1 we will [...]

But I want something like

In the next chapter background we will [...]

Here an example of my usage

folgenden Kapitel \ref{mehrInformatiker} werden Maßnahmen

Will result in

result

I have read this article Cross-Reference with custom text

But I would like to use \ref. Isn't it possible?

cgnieder
  • 66,645

1 Answers1

5

After I posted, cgnieder pointed out the existence of the nameref package, as in

\documentclass[11pt]{article}
\usepackage{nameref}
\begin{document}
I can even refer to section \nameref{s-1} before it is defined.

\section{Background so Long} \label{s-1} I want to print just a number for Section \ref{s-1}.

But here, I want to print section \nameref{s-1}. \end{document}

enter image description here

With it, \nameref{<label>} gives the full name of the section. In this regard, what I had suggested is slightly different, in that the name one assigns to the textual reference may or may not be the same as the full section title. Here was my original answer, edited to accentuate this difference:

I introduce optional arguments to \label to assign a textual reference (which, importantly, need not match the section name), and then use the newly defined [s] option to \ref to invoke the "special" textual reference rather than the usual [numerical] response.

By writing it to the aux file, I can invoke the textual reference before it is even defined.

\documentclass[11pt]{article}
\let\svlabel\label
\let\svref\ref
\makeatletter
\renewcommand\label[2][\relax]{%
  \svlabel{#2}%
  \ifx\relax#1\else
    \immediate\write\@mainaux{\string%
      \expandafter\gdef\noexpand\csname custom#2\noexpand\endcsname{#1}}%
  \fi
}
\makeatother
\renewcommand\ref[2][\relax]{%
  \ifx s#1\csname custom#2\endcsname\else\svref{#2}\fi
}
\begin{document}
I can even refer to section \ref[s]{s-1} before it is defined.

\section{Background so Long} \label[Background]{s-1} I want to print just a number for Section \ref{s-1}.

But here, I want to print section \ref[s]{s-1}. \end{document}

enter image description here

If one only needed to reference "backwards", meaning after a label was defined, one can dispense with the aux file write to a simpler alternative:

\documentclass[11pt]{article}
\let\svlabel\label
\let\svref\ref
\renewcommand\label[2][\relax]{%
  \svlabel{#2}%
  \ifx\relax#1\else
      \expandafter\gdef\csname custom#2\endcsname{#1}%
  \fi
}
\renewcommand\ref[2][\relax]{%
  \ifx s#1\csname custom#2\endcsname\else\svref{#2}\fi
}
\begin{document}
I cannot refer to section ``\ref[s]{s-1}'' before it is defined.

\section{Background so Long} \label[Background]{s-1} I want to print just a number for Section \ref{s-1}.

But here, I want to print section \ref[s]{s-1}. \end{document}

enter image description here

  • Thanks a lot for your answer. I am LaTeX beginner and this sounds like advanced stuff, is there not an easier solution. I mean in contrast to your answer using the hyperef would be easier right? – Aalexander Feb 09 '21 at 17:11
  • @Aalexander If you will only be referencing "backwards", meaning refer to section "Background" only after section "Background" is already labeled, then the \immediate\write\@mainaux{\string\expandafter\gdef\noexpand\csname custom#2\noexpand\endcsname{#1}} can be replaced with \expandafter\gdef\csname custom#2\endcsname{#1}. Likewise, without using \@mainaux, the \makeatletter and \makeatother can be removed. – Steven B. Segletes Feb 09 '21 at 17:15
  • @Aalexander See my edit, in which I show the simpler version. – Steven B. Segletes Feb 09 '21 at 17:19
  • Have you just re-invented https://ctan.org/pkg/nameref? – cgnieder Feb 09 '21 at 17:20
  • @cgnieder I have no idea. I've not used it. – Steven B. Segletes Feb 09 '21 at 17:21