0

I've formulated this question inline inside my SSCCE:

\documentclass[12pt,oneside,onecolumn,a4paper]{article}

\usepackage{amssymb,amsmath,amsthm} \usepackage{imakeidx} % NOTE: Hyperref should typically be loaded last of all the packages. \usepackage{hyperref}

% Add a label and an index entry at the same time. \newcommand{\idxlabel}[2]{\label{#1}\index{#2}}

\makeindex

% Various theorem environments. Only one variety included here. \theoremstyle{plain} % just in case the style had changed \newcommand{\thistheoremname}{}

\newtheorem{thm}{Theorem}[section] % the main one \newtheorem{thm}{Theorem} % unnumberedtheorem % there are generic theorems used to construct the environments below: \newtheorem{genericthm}{\thistheoremname}

\newenvironment{namedthm}[1] {\renewcommand{\thistheoremname}{#1}% \begin{genericthm}} {\end{genericthm*}}

\title{How do I select a named theorem name as a label reference using nameref?} \begin{document}

\maketitle \section{This is the first section}

\begin{namedthm*}{Some Theorem}[book-I-found-it-in] \idxlabel{my-theorem}{Some Theorem} Here, I define a theorem I would like to refer to later. I would like the text form to display ``Some Theorem" rather than a section or page number.

It would be nice if \idxlabel could interpret the theorem name from the namedthm environment, but I would also like to know how to override that and provide my own just in case.

It may need to differ from the index, because the index supports nested terms such as Latex!Cross-referencing. \end{namedthm*}

\section{This is the second section}

Here I would like to talk about \nameref{my-theorem} (and this should say ``Some Theorem"", ideally.

\clearpage \printindex

\end{document}

To compile this you should probably use a tool like latexmk or rubber (I use the former) in order that the links are picked up. The problem is as follows:

Rendered document

What I would really like is for that "book-I-found-it-in" to correctly pick up the theorem name. I would also like to know if I can control what appears there. I have tried moving around the label statement including inside the theorem definition, and it picks up nothing or variously the section title or something preceding the theorem.

I understand from other answers on this site how such labels make their way to the aux file, and now I would like to control their display.

diagprov
  • 121
  • your question is too long and misses the only really important part: a small but complete example. – Ulrike Fischer Feb 18 '22 at 18:30
  • I've reworded the Q inside an SSCCE, hopefully this makes it clearer. I'm using several different theorem environments, not just this one. I'm good either way using a custom nameref or understanding how label acquires the text it decides to use. I can't quite figure it out. – diagprov Feb 18 '22 at 20:20

1 Answers1

1

Redefine \@currentlabelname after the theorem head.

\documentclass[12pt,oneside,onecolumn,a4paper]{article}

\usepackage{amssymb,amsmath,amsthm} \usepackage{imakeidx} % NOTE: Hyperref should typically be loaded last of all the packages. \usepackage{hyperref}

% Add a label and an index entry at the same time. \newcommand{\idxlabel}[2]{\label{#1}\index{#2}}

\makeindex

% Various theorem environments. Only one variety included here. \theoremstyle{plain} % just in case the style had changed \newcommand{\thistheoremname}{}

\newtheorem{thm}{Theorem}[section] % the main one \newtheorem{thm}{Theorem} % unnumberedtheorem % there are generic theorems used to construct the environments below: \newtheorem{genericthm}{\thistheoremname}

\makeatletter \newenvironment{namedthm}[1] {\renewcommand{\thistheoremname}{#1}% \begin{genericthm}% \def@currentlabelname{#1}}% ADDED {\end{genericthm*}} \makeatother

\title{How do I select a named theorem name as a label reference using nameref?} \begin{document}

\maketitle \section{This is the first section}

\begin{namedthm*}{Some Theorem}[book-I-found-it-in] \idxlabel{my-theorem}{Some Theorem} Here, I define a theorem I would like to refer to later. I would like the text form to display ``Some Theorem" rather than a section or page number.

It would be nice if \idxlabel could interpret the theorem name from the namedthm environment, but I would also like to know how to override that and provide my own just in case.

It may need to differ from the index, because the index supports nested terms such as Latex!Cross-referencing. \end{namedthm*}

\section{This is the second section}

Here I would like to talk about \nameref{my-theorem} (and this should say ``Some Theorem"", ideally.

\clearpage \printindex

\end{document}

mbert
  • 4,171