1

When labelling theorems and such, the label often takes this form :

\label{th:pyth}

So when referring to theorems, we often write :

According to theorem \ref{th:pyth} ...

But there is a little bit of redundancy with "theorem" and "th", so i wanted to create a command that would look like \refenv{th}{pyth} and that would output theorem 1. In order to do that, I would need to match the first argument to a string, so I tried this with the boolexpr package :

\usepackage{boolexpr}
\NewDocumentCommand{\refenv}{mm}{
    \switch
    \case{#1 = th} theorem
    \case{#1 = prop} proposition
    \case{#1 = coro} corollary
    \case{#1 = lem} lemma
    \case{#1 = prf} proof
    \case{#1 = def} definition
    \case{#1 = rem} remark
    \case{#1 = eq} equation
    \endswitch
    \ref{#1:#2}
}

Without success, since the boolexpr package only compares numbers. How can I find an equivalent that does not require multiple nested ifthenelse commands?

4
  • 2
    Why not using cleveref?
    – egreg
    Oct 30, 2022 at 11:35
  • It's a lot easier to look at the label string, if it starts with th, theorem etc. Latex has tools to check for this. Otherwise look at cleveref. I have an unpublished package that only uses the structure of the label string it self so it can be done.
    – daleif
    Oct 30, 2022 at 11:38
  • cleveref has done the job as I wanted it to.
    – Nolord
    Oct 30, 2022 at 13:00
  • Please provide your own solution as an answer and mark it as solved after. Oct 30, 2022 at 13:21

2 Answers 2

1

You might use a more flexible case switch function.

\documentclass{article}
\usepackage{amsthm}

\ExplSyntaxOn

\NewDocumentCommand{\refenv}{mm}
 {
  \str_case:nnF { #1 }
   {
    {th}{theorem}
    {prop}{proposition}
    {coro}{corollary}
    {lem}{lemma}
    {prf}{proof}
    {def}{definition}
    {rem}{remark}
    {eq}{equation}
   }
  {??}
  \nobreakspace\ref{#1:#2}
 }

\ExplSyntaxOff

% just a couple of envs
\newtheorem{theorem}{Theorem}
\theoremstyle{definition}
\newtheorem{definition}{Definition}

\begin{document}

\begin{definition}\label{def:important}
A \emph{definition} defines.
\end{definition}

\begin{theorem}\label{th:main}
The main theorem.
\end{theorem}

\begin{equation}\label{eq:best}
0=0
\end{equation}

We see that \refenv{th}{main} depends on \refenv{def}{important}
and \refenv{eq}{best}.

\end{document}

enter image description here

But it's better to use cleveref.

\documentclass{article}
\usepackage{amsthm}
\usepackage{cleveref}

% just a couple of envs
\newtheorem{theorem}{Theorem}
\theoremstyle{definition}
\newtheorem{definition}{Definition}

\begin{document}

\begin{definition}\label{def:important}
A \emph{definition} defines.
\end{definition}

\begin{theorem}\label{th:main}
The main theorem.
\end{theorem}

\begin{equation}\label{eq:best}
0=0
\end{equation}

We see that \cref{th:main} depends on \cref{def:important}
and \cref{eq:best}.

\end{document}

enter image description here

See the manual of cleveref to see how to customize the references.

0

The package cleveref, suggested by egreg, does everything I need, and does remove the redundancy, so no need to do else if shenanigans.

You must log in to answer this question.

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