0

Note: There is a question here with an almost identical title, but the person there is asking for a completely different thing.

I am currently writing a document that regularly refers to the name of a section and the numbers of the theorems. Below is a MWE:

\documentclass{article}
\usepackage{nameref}

\newtheorem{theorem}{Theorem}

\begin{document}
\section{Section Name}
\label{sec1}

\begin{theorem}
\label{thm1}
    Every section has a name.
\end{theorem}

By Theorem \ref{thm1}, this section has a name, which is \nameref{sec1}.

\end{document}

This runs perfectly, but I do not wish to distinguish \ref from \nameref. That is, I would like to instead write:

By Theorem \ref{thm1}, this section has a name, which is \ref{sec1}.

or:

By \ref{thm1}, this section has a name, which is \ref{sec1}.

and the text will output as:

By Theorem 1, this section has a name, which is Section Name.

Is this possible? And yes, I wish to keep it as \ref, instead of something like \myref.

5
  • 1
    with the zref package you could check the counter name and then retrieve the name if it is section, but imho this input is confusing, and what will you do if you want the section number in one place? Commented Mar 31, 2022 at 13:05
  • @UlrikeFischer my document has no section number (i.e. I only have \section*), so it's not an issue for me. Commented Mar 31, 2022 at 13:10
  • 1
    That's not what's in your MWE.
    – Teepeemm
    Commented Mar 31, 2022 at 13:37
  • @Teepeemm I don't think it matters if it is \section or \section* in my MWE, since my question has nothing to do with section number. Commented Mar 31, 2022 at 13:41
  • See also tex.stackexchange.com/questions/6238/…. The nameref package stores the section title into the \@currentlabelname entry which is normally used for the counter name (section). Commented Mar 31, 2022 at 16:38

2 Answers 2

3

(I'm not able to comment currently, or I would ask if you were trying to avoid cleveref before posting this. If you are, let me know.)

If your goal is just to get the output, and not specifically figure out how to combine commands like that, then I would suggest using the cleveref package, as what you describe is essentially reinventing the \cref command.

Also, because unless its functionality is modified somehow, using \nameref{thm1} is going to give you the name of the section containing your theorem (which I don't think is the goal).

Here's an example of your code with \cref:

\documentclass{article}
\usepackage{nameref}
\usepackage{cleveref}

% defines how the name for references to a single, and multiple
% items of the given type should be referenced
\crefname{theorem}{Theorem}{Theorems}

\newtheorem{theorem}{Theorem}

\begin{document}
\section{Section Name}
\label{sec1}

\begin{theorem}
\label{thm1}
    Every section has a name.
\end{theorem}

By \cref{thm1}, this section has a name, which is \nameref{sec1}.

\end{document}

There is one line included to specify how the package should refer to theorems.

This package also allows you to specify formatting for the references:

% make references bold
\crefformat{theorem}{\B{Theorem~#2#1#3}}

as well as specifying what to do when two or more items are specified:

\newcommand{\crefrangeconjunction}{--}  % placed between numbers in a range
\newcommand{\crefpairconjunction}{ and }  % placed between two items
\newcommand{\crefmiddleconjunction}{ , }  % placed between items in a list
\newcommand{\creflastconjunction}{, and }  % placed before last item in a list

% specify format for two items
\crefrangeformat{theorem}{\B{Theorems~#3#1#4\crefrangeconjunction#5#2#6}}

% specify format for more than two items
\crefmultiformat{theorem}{\B{Theorems~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}

Defining internal references

An example of the file from my thesis, which defined reference styles for everything I needed. (There's some difference between \Crefname and \crefname that escapes me right now.) Each block contains identical commands, the only difference being the type of thing they are for:

\Crefname{equation}{Equation}{Equations}
\Crefname{figure}{Figure}{Figures}
\Crefname{tabular}{Table}{Tables}
\Crefname{section}{Section}{Sections}
\Crefname{chapter}{Chapter}{Chapters}
\Crefname{appendix}{Appendix}{Appendices}

\crefname{equation}{Equation}{Equations}
\crefname{figure}{Figure}{Figures}
\crefname{tabular}{Table}{Tables}
\crefname{section}{Section}{Sections}
\crefname{chapter}{Chapter}{Chapters}
\crefname{appendix}{Appendix}{Appendices}

\crefdefaultlabelformat{#2\B{#1}#3}
\crefformat{equation}{\B{Equation~#2#1#3}}
\crefformat{table}{\B{Table~#2#1#3}}
\crefformat{figure}{\B{Figure~#2#1#3}}
\crefformat{section}{\B{Section~#2#1#3}}
\crefformat{chapter}{\B{Chapter~#2#1#3}}
\crefformat{appendix}{\B{Appendix~#2#1#3}}

\newcommand{\crefrangeconjunction}{--}
\newcommand{\crefpairconjunction}{ and }
\newcommand{\crefmiddleconjunction}{ , }
\newcommand{\creflastconjunction}{, and }


\crefrangeformat{table}{\B{Tables~#3#1#4\crefrangeconjunction#5#2#6}}
\crefrangeformat{figure}{\B{Figures~#3#1#4\crefrangeconjunction#5#2#6}}
\crefrangeformat{equation}{\B{Equations~#3#1#4\crefrangeconjunction#5#2#6}}
\crefrangeformat{chapter}{\B{Chapters~#3#1#4\crefrangeconjunction#5#2#6}}
\crefrangeformat{section}{\B{Sections~#3#1#4\crefrangeconjunction#5#2#6}}
\crefrangeformat{appendix}{\B{Appendices~#3#1#4\crefrangeconjunction#5#2#6}}

\crefmultiformat{table}{\B{Tables~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}
\crefmultiformat{figure}{\B{Figures~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}
\crefmultiformat{equation}{\B{Equations~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}
\crefmultiformat{chapter}{\B{Chapters~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}
\crefmultiformat{section}{\B{Sections~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}
\crefmultiformat{appendix}{\B{Appendices~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}

7
  • Thank you. I'm not familiar with the technicality of tex, so I'm unsure how this solves my problem. Do you mind elaborating further? Commented Apr 1, 2022 at 23:40
  • The first bit of code does what your question asked about—when you call \cref{<label>}, it will print out something like Theorem 1, if you pass the label for theorem 1. Most of the rest has to do with referencing multiple items at once; it would allow you to do things like \cref{thm1,thm2}, \cref{thm1,thm2,thm3}, \cref{thm1,thm3,thm4}` and get: Theorems 1 and 2, Theorems 1–3, and Theorems 1, 3, and 4 in the text. This may be more advanced functionality than you need, but it shows just how flexible the cleveref package can be. Commented Apr 1, 2022 at 23:52
  • I see, but how does that merge \ref with \nameref? Commented Apr 2, 2022 at 0:04
  • As I said at the beginning of my answer, this provides the functionality you describe as the goal for combining \ref and \nameref; it does not actually combine them. You state that the output you want is to call \ref{thm1} and have it produce Theorem 1. \nameref doesn't help with that. If you call \nameref{thm1} with your code, it produces Section Name, because it produces the name of the section containing the label you pass it. If what you want is a way to combine two commands without creating a new one, this isn't that. If you want to easily get Theorem 1, this will work. Commented Apr 2, 2022 at 0:10
  • If I'm understanding your reply (and answer) correctly, you're explaining how one may use cleveref to produce things like Theorem 1 with just \ref{thm1}. However, I was mostly concerned with producing Section 1 with \ref{sec1} instead of \nameref{sec1} (or some similar command like \cref, as long as I can use the same command for both theorems and sections). I may be missing something, but does your answer solve that problem as well? Commented Apr 2, 2022 at 0:18
0

This uses a special \label command instead of a special \ref command.

\documentclass{article}
\usepackage{nameref}

\newtheorem{theorem}{Theorem}

\makeatletter
\newcommand{\sectionlabel}[1]{\let\@currentlabel=\@currentlabelname
  \edef\@currentlabelname{section}%
  \label{#1}}
\makeatother

\begin{document}
\section{Section Name}
\sectionlabel{sec1}

\begin{theorem}
\label{thm1}
    Every section has a name.
\end{theorem}

By Theorem \ref{thm1}, this section has a name, which is \ref{sec1}.

\end{document}

This modifies \@startsection, which is used by \section, \subsection, etc.

\documentclass{article}
\usepackage{etoolbox}

\newtheorem{theorem}{Theorem}

\makeatletter
\patchcmd{\@sect}{\refstepcounter{#1}}%
  {\refstepcounter{#1}\edef\@currentlabel{#7}}% #7=short title, #8=long title
  {}{FAILED}
\makeatother

\begin{document}
\section{Section Name}
\label{sec1}

\begin{theorem}
\label{thm1}
    Every section has a name.
\end{theorem}

By Theorem \ref{thm1}, this section has a name, which is \ref{sec1}.

\end{document}
2
  • Thank you, but I would like to keep \label the same as well. Commented Apr 1, 2022 at 23:40
  • The third alternative is to modify \section. Commented Apr 2, 2022 at 1:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .