5

I believe that usually you explicitly tell cleveref what to call your label using \crefname{type}{singular}{plural}, this doen't work however as here "type" has to be the name of the counter (I believe). I've also tried using \label[type]{label} to no avail. Is there any way to accomplish two environments sharing a counter, but have cleveref be able to distinguish between them? (Or is there maybe some way to use two counters, but for them always to be equal/dependent as to accomplish the same effect?)

Below is an example:

\documentclass{article}
\usepackage[colorlinks, linkcolor=blue]{hyperref} 
\usepackage[noabbrev, capitalise]{cleveref}
\usepackage{tikz}

%new theorem environment
\newcounter{theo}[section]\setcounter{theo}{0}
\renewcommand{\thetheo}{\arabic{section}.\arabic{theo}}
\newenvironment{theo}{%
\refstepcounter{theo}%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[anchor=east,rectangle,fill=green!20]
{\strut \textbf{Theorem~\thetheo.}};
\newline
}

\crefname{theo}{Theorem}{Theorems}

%new lemma environment
\newenvironment{lem}{%
\refstepcounter{theo}
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[anchor=east,rectangle,fill=blue!20]
{\strut \textbf{Lemma~\thetheo.}};
\newline
}

\begin{document}
\begin{theo}\label{thrm}
Just some text.
\end{theo}

\begin{lem}\label{lm1}
Just some more text.
\end{lem}

\begin{lem}\label[Lemma]{lm2}
Just some more text.
\end{lem}

\noindent
\cref{thrm}\\ %give Theorem 0.1
\cref{lm1}\\ %give Lemma 0.2
\cref{lm2}\\ %give Lemma 0.3
\end{document}

At the bottom of the example I use \cref three times. In a comment I display the desired output. Here I used \crefname{theo}{Theorem}{Theorems} to give the "theo"-environment the correct name, as you can see then the "lem"-environment gets the same name, when referred to. The second time I use the "lem"-environment, I label it using \label[Lemma]{lm2}, which turns out not to have the desired effect.

(Note: The code that uses tikz is not relevant, I added it as a small justification for not using the \newtheorem command instead.)

3
  • You could add a boolean to the environment, which would be set to true for only one of the environments and use a test in the command \crefname{...}{...} to determine what to print.
    – Bernard
    Commented Mar 9, 2019 at 21:23
  • 1
    never end a paragraph with \newline Commented Mar 9, 2019 at 21:56
  • @Bernard can you elaborate on how this works?
    – Bubaya
    Commented Feb 28, 2023 at 12:54

2 Answers 2

6

You can allocate lem and theo to the same count register (with care:-)

enter image description here

\documentclass{article}
\usepackage[colorlinks, linkcolor=blue]{hyperref} 
\usepackage[noabbrev, capitalise]{cleveref}
\usepackage{tikz}

%new theorem environment
\newcounter{theo}[section]\setcounter{theo}{0}
\renewcommand{\thetheo}{\arabic{section}.\arabic{theo}}
\newenvironment{theo}{%
\refstepcounter{theo}%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[anchor=east,rectangle,fill=green!20]
{\strut \textbf{Theorem~\thetheo.}};
\newline%BADNESS 10000!!!!!
}

\makeatletter
\let\c@lem\c@theo
%now def not \let so it picks up current value
\def\p@lem{\p@theo}
\def\thelem{\thetheo}
\makeatother
\crefname{theo}{Theorem}{Theorems}
\crefname{lem}{Lemma}{Lemmas}

%new lemma environment
\newenvironment{lem}{%
\refstepcounter{lem}%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[anchor=east,rectangle,fill=blue!20]
{\strut \textbf{Lemma~\thetheo.}};
\newline%BADNESS 10000!!!!!
}

\begin{document}
\begin{theo}\label{thrm}
Just some text.
\end{theo}

\begin{lem}\label{lm1}
Just some more text.
\end{lem}

\begin{lem}\label{lm2}
Just some more text.
\end{lem}

\noindent
\cref{thrm}\\ %give Theorem 0.1
\cref{lm1}\\ %give Lemma 0.2
\cref{lm2}\\ %give Lemma 0.3
\end{document}
3
  • You may want to point out to the OP that no special effort is needed, so to say, as long as he/she employs either the amsthm or the ntheorem package.
    – Mico
    Commented Mar 9, 2019 at 22:25
  • 1
    @Mico I did wonder, but I just answered the question as asked:-) OP should accept your answer though.... Commented Mar 9, 2019 at 23:50
  • @David, I however still think your answer is better. I believe the newtheorem environment is just a special kind of environment, so your answer is the general one and fully answers my question. I knew you could use newtheorem in combination with those packages to make cleveref work, however I don't think that what I want to do, could be done with newtheorem. As you can see, the output has changed in Mico's answer. (This is also why I wrote the note at the end of my question)
    – lamcar
    Commented Mar 10, 2019 at 9:43
8

No need for any programming contortions -- just load either the amsthm or the ntheorem package before both hyperref and cleveref, and then define the theorem-like environments the usual way. In particular, it's perfectly ok for several theorem-like environments to share the same counter (theo in the following code):

enter image description here

\documentclass{article}

\usepackage{amsthm} %or: \usepackage{ntheorem}
\usepackage[colorlinks, linkcolor=blue]{hyperref} 
\usepackage[noabbrev, capitalise]{cleveref}

% two new theorem-like environments
\newtheorem{theo}{Theorem}[section] % subordinate 'theo' cntr to 'section' cntr
\newtheorem{lem}[theo]{Lemma} % make 'lem' and 'theo' share same cntr

\crefname{theo}{Theorem}{Theorems}
\crefname{lem}{Lemma}{Lemmas}

\begin{document}
\setcounter{section}{2} % just for this example

\begin{theo}\label{thrm}Just some text.\end{theo}
\begin{lem}\label{lm1}Just some more text.\end{lem}
\begin{lem}\label{lm2}Still more text.\end{lem}

\cref{thrm} \dots

\cref{lm1,lm2} \dots
\end{document}

You must log in to answer this question.

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