0

In this post Custom theorem numbering there is an explanation by egreg that shows how to implement this. Now I would like to be able to reference so that I would not have to type lemma/proposition etc. right before \ref{} when referring to a particular \label{} because by using \ref{} it only gives the numbering. However, using the cref{} from the cleveref package only gives question marks on the page after compilation (which I'm not surprised about since it's a package independent of the code given by egreg). How does one solve this problem?

\documentclass[11pt]{article}

\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
%\usepackage{xparse}
\usepackage{mathtools}
\usepackage{ulem}
\usepackage{enumerate}
\usepackage{xcolor}

\usepackage[shortlabels]{enumitem} 
\usepackage{cleveref} 

%===========================================================%
%The code below customises theorem numbering
%===========================================================%
\newtheorem{innercustomgeneric}{\customgenericname}  
\providecommand{\customgenericname}{}
\newcommand{\newcustomtheorem}[2]{%
  \newenvironment{#1}[1]
  {%
   \renewcommand\customgenericname{#2}%
   \renewcommand\theinnercustomgeneric{##1}%
   \innercustomgeneric
  }
  {\endinnercustomgeneric}
}


\newcustomtheorem{customdefinition}{Definition}
\newcustomtheorem{customlemma}{Lemma}
\newcustomtheorem{customproposition}{Proposition}
%===========================================================%

\begin{document}

\begin{customproposition}{5.2} \label{hello}
blah blah 
\end{customproposition}

I want to Proposition \ref{hello} without 
having to type proposition 
at the beginning of \ref{hello}.



\end{document}

enter image description here

Is there an efficient and clean way to do this? Either via cleveref or other alternatives by writing a particular code to reference \label{} without having to write "Proposition" in front of the reference?

5
  • As always on this site you are much more likely to get any help if you provide a full bit minimal example that others can copy and test as is. Here we have to add stuff in order to even compile your code and then a lot of people will not bother with tour question.
    – daleif
    Sep 24, 2021 at 10:31
  • Hello. Please make a full compilable document starting with \documentclass{}
    – Roland
    Sep 24, 2021 at 10:34
  • Thank you for letting me know! Hope that helps.
    – tcmtan
    Sep 24, 2021 at 10:58
  • Does \crefname{customproposition}{Proposition}{Propositions} after calling the package cleverref help you? I currently can't test it myself.
    – Lukas
    Sep 24, 2021 at 11:25
  • @Lukas Thanks for the reply. No sorry since it's not a general solution.
    – tcmtan
    Sep 24, 2021 at 11:55

2 Answers 2

2

Add the cleveref type and alias the counter.

\documentclass[11pt]{article}

\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
%\usepackage{xparse}
\usepackage{mathtools}
\usepackage{ulem}
\usepackage{enumerate}
\usepackage{xcolor}

\usepackage[shortlabels]{enumitem} 
\usepackage{cleveref} 

%===========================================================%
%The code below customises theorem numbering
%===========================================================%
\newtheorem{innercustomgeneric}{\customgenericname}  
\providecommand{\customgenericname}{}
\newcommand{\newcustomtheorem}[2]{%
  \crefname{#2}{#2}{#2s}%
  \newenvironment{#1}[1]
  {%
   \renewcommand\customgenericname{#2}%
   \crefalias{innercustomgeneric}{#2}%
   \renewcommand\theinnercustomgeneric{##1}%
   \innercustomgeneric
  }
  {\endinnercustomgeneric}
}


\newcustomtheorem{customdefinition}{Definition}
\newcustomtheorem{customlemma}{Lemma}
\newcustomtheorem{customproposition}{Proposition}
%===========================================================%

\begin{document}

\begin{customproposition}{5.2} \label{hello}
blah blah 
\end{customproposition}

\begin{customdefinition}{12.4} \label{helloagain}
blah blah 
\end{customdefinition}

I want \cref{hello} without having to type ``proposition''.

Also \cref{helloagain}.

\end{document}

enter image description here

1
  • Thank you so much! It works just as desired! On another note, would you happen to know where a beginner could read up about how to devise a code like this? I obviously don't how it works since I don't even know what 'type' and 'alias' and 'counter' are.
    – tcmtan
    Sep 24, 2021 at 23:08
0

Like this? Defined a new command \pref, which "prints" "Proposition" and the \ref .

Remarks: also works when you use labels like this environment:label (like fig:drawing), which is useful once you've got many different types of environments.

\documentclass[11pt]{article}

\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
%\usepackage{xparse}
\usepackage{mathtools}
\usepackage{ulem}
\usepackage{enumerate}
\usepackage{xcolor}

\usepackage[shortlabels]{enumitem} 
\usepackage{cleveref} 

%===========================================================%
%The code below customises theorem numbering
%===========================================================%
\newtheorem{innercustomgeneric}{\customgenericname}  
\providecommand{\customgenericname}{}
\newcommand{\newcustomtheorem}[2]{%
  \newenvironment{#1}[1]
  {%
   \renewcommand\customgenericname{#2}%
   \renewcommand\theinnercustomgeneric{##1}%
   \innercustomgeneric
  }
  {\endinnercustomgeneric}
}


\newcustomtheorem{customdefinition}{Definition}
\newcustomtheorem{customlemma}{Lemma}
\newcustomtheorem{customproposition}{Proposition}
%~~~ new ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~% <<==
\newcommand{\pref}[1]{Proposition \ref{#1}}
%===========================================================%

\begin{document}

\begin{customproposition}{5.2} \label{hello}
blah blah 
\end{customproposition}

I want to Proposition \ref{hello} without 
having to type proposition 
at the beginning of \ref{hello}. \\

Is this the way you want to make a call to \pref{hello}?% <<==

\end{document}

Result:

Result

2
  • Thanks for the reply. However, I would like it to work for any \customtheorem{} I define in the preamble. So when I call \ref{} on the \label it does it accordingly without having to define it for each \customtheorem{}.
    – tcmtan
    Sep 24, 2021 at 11:50
  • Sorry, don't get what you want. I suggest to edit your question and to put a clarifying example at the end.
    – MS-SPO
    Sep 24, 2021 at 13:02

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.