Page LayoutDisplaying reference name

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Displaying reference name

Post by svend_tveskaeg »

Hi LaTeX friends.

The name of the headline is not "correct" but I cannot find a more precise term, sorry!

Assume there is a reference, say, \label{beaver} in the middle of the document. How do I control the output name of the reference? I.e., I would like to get something like this:

Input:

Code: Select all

Somewhere in the ``\ref{beaver}'' section.
Outout:

Code: Select all

Somewhere in the ``dam building'' section.
Thanks in advance"
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

magicmoose
Posts: 90
Joined: Fri Nov 06, 2009 7:29 am

Re: Displaying reference name

Post by magicmoose »

Use the \nameref command provided by the hyperref package

Hope that helps
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Displaying reference name

Post by svend_tveskaeg »

Thanks!

It is not exactly what I am looking for but it is close.

Example:
Assume that the label \label{beaver} is inside the section \section{Fish}. Then I do not want the output name to be "Fish" when refering to the label but I want to be able to choose my own output name, say "water". I.e., I am looking for something like:

Input:

Code: Select all

\section{Fish} Research has shown that many fish live in \label{beaver}. That is now a well-known fact.
Output:
Fish <as section header>
Research has shown that many fish live in water. That is now a well-known fact.

I hope this clears up things a bit!


Thanks in advance!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Displaying reference name

Post by gmedina »

I still am not sure if I understand your question correctly. Perhpas the cleveref package could be useful for you:

Code: Select all

\documentclass{article}
\usepackage{cleveref}

\crefname{section}{water}{waters}
\Crefname{section}{Water}{Waters}

\begin{document}

\section{Fish\label{beaver}} Research has shown that many fish live in \cref{beaver}. That is now a well-known fact.

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Displaying reference name

Post by svend_tveskaeg »

You definitely understood my question well enough! :D This is (almost) exactly what I am searching for but if the section number can be removed it will be great!

The "perfect" output for me would be:
Fish
Research has shown that many fish live in water. That is now a well-known fact.
instead of
1 Fish
Research has shown that many fish live in water 1. That is now a well-known fact.
I.e., without the section number in both the header and in the text.

Thanks in advance!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Displaying reference name

Post by gmedina »

Now you can use the titlesec package to get rid of the section number in the titles and the command \creflabelformat (from cleveref) to get rid of the number in the reference.

Code: Select all

\documentclass{article}
\usepackage{cleveref}
\usepackage{titlesec}

\crefname{section}{water}{waters}
\Crefname{section}{Water}{Waters}
\creflabelformat{section}{#2#3}
\titleformat{\section}
  {\normalfont\Large\bfseries}{}{0em}{}
\titlespacing*{\section}
  {0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

\begin{document}

\setcounter{section}{100}
\section{Fish\label{beaver}} Research has shown that many fish live in \cref{beaver}. That is now a well-known fact.

\end{document}
However, now an unwanted space appears after the reference, and I only could think of a not elegant way of suppressing this additional space:

Code: Select all

\documentclass{article}
\usepackage{cleveref}
\usepackage{titlesec}

\crefname{section}{water}{waters}
\Crefname{section}{Water}{Waters}
\creflabelformat{section}{#2#3\hspace*{-.4em}}
\titleformat{\section}
  {\normalfont\Large\bfseries}{}{0em}{}
\titlespacing*{\section}
  {0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

\begin{document}

\setcounter{section}{100}
\section{Fish\label{beaver}} Research has shown that many fish live in \cref{beaver}. That is now a well-known fact.

\end{document}
Perhaps someone else can provide a better way of treating this additional space that appears in the first code of this post.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Displaying reference name

Post by svend_tveskaeg »

Thank you very much!

This works perfectly but, as gmedina mentioned, if someone can come up with a more "elegant" solution, by all means post it!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply