There are two issues:
The "eq." problem
That is because you tried to use cleveref
with theorem environments without loading either amsthm
or ntheorem
. A lot of the code in cleveref
that handles references to theorems (especially when it comes to dealing with shared/sibling counters) are designed to work only when one of the two are loaded. (See section 14.1 "Non-bugs" of the cleveref
documentation.) If you also load \usepackage{amsthm}
this problem goes away.
The upside-down question mark issue
I am pretty sure the compilation of your PDF should've aborted with an error! The issue is a combination of several problems:
- When you expand
\value{subsection}
you just get \c@subsection
and it is not a good idea for this to be floating around, since it will then pick up the subsection number at points of use, instead of at the point of definition.
- As warned in the documentation for
enumitem
, what follows ref=
is a moving argument, so you have to worry about fragile commands.
So as written your code would not have compiled. To solve the second issue, we can abstract the code to hide the subsection numbers into a separate macro and protect that.
\renewcommand{\theequation}{%
\thesection.\protect\hide{\value{subsection}}{\arabic{subsection}}\arabic{equation}%
}
\newcommand{\hide}[2]{\ifnum#1>0 #2.\fi}
If you do this, your code will build just fine. Unfortunately, you will get the wrong numbers, since you find in the .aux
file something like:
\newlabel{hp:hypo}{{2.\hide {\c@subsection }{1}2}{1}{Admissible shapes}{equation.2.1.2}{}}
\newlabel{hp:hypo@cref}{{[hp][2][2,1]2.\hide {\c@subsection }{1}2}{[1][1][]1}}
\newlabel{hp:hypo1}{{{2.\hide {\c@subsection }{1}2.(a)}}{1}{Admissible shapes}{Item.3}{}}
\newlabel{hp:hypo1@cref}{{[hp][1][]{2.\hide {\c@subsection }{1}2.(a)}}{[1][1][]1}}
\newlabel{hp:hypo2}{{{2.\hide {\c@subsection }{1}2.(b)}}{1}{Admissible shapes}{Item.4}{}}
\newlabel{hp:hypo2@cref}{{[hp][2][]{2.\hide {\c@subsection }{1}2.(b)}}{[1][1][]1}}
Which means that whenever you call \ref{hp:hypo2}
, your code will at the point of reference evaluate the current value of the subsection
counter, and then use it to decide whether to hide the display. This is obviously a problem if you have a set of hypotheses defined in section 1, but referred to in section 2.1.
To work around this, we can use the fact that the arabic form of a counter expands to an integer, and can be evaluated by \ifnum
just fine, and we will run \hide
on the arabic form itself instead on the counter value.
So here's the complete code (with some extras):
\documentclass{article}
\title{Untitled Document}
\author{Your Name}
\date{\today}
\usepackage{amsfonts}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{enumitem}
\usepackage{mathtools}
\usepackage{cleveref}
% Global numbering
\counterwithin*{equation}{section}
\counterwithin*{equation}{subsection}
\renewcommand{\theequation}{%
\thesection.\protect\hide{\arabic{subsection}}\arabic{equation}%
}
\newcommand{\hide}[1]{\ifnum#1>0 #1.\fi}
% Used for creating new theorem and remark environments
\newtheorem{thm}[equation]{Theorem}
\newtheorem{hp}[equation]{Assumption}
\crefname{thm}{Theorem}{Theorem}
\crefname{hp}{hypothesis}{hypotheses}
% Reference items in enumerate
\newlist{hypothesisenum}{enumerate}{1}
\setlist[hypothesisenum]{label=\alph*), ref=\theequation.(\alph*)}
\crefalias{hypothesisenumi}{hp}
\begin{document}
\maketitle
\section{A section}
\begin{hp}[Admissible shapes]\label{hp:hypop}
\hfill
\begin{hypothesisenum}
\item \label{hp:hypo21} first
\item \label{hp:hypo22} second
\end{hypothesisenum}
\end{hp}
\section{Another section}
\subsection{Subsection of another section}
\begin{thm}[A theorem]\label{thm:thm1}
\end{thm}
\begin{hp}[Admissible shapes]\label{hp:hypo}
\hfill
\begin{hypothesisenum}
\item \label{hp:hypo1} first
\item \label{hp:hypo2} second
\end{hypothesisenum}
\end{hp}
\begin{thm}[A theorem]
\end{thm}
We used \cref{hp:hypo1,hp:hypo2,hp:hypo}.
We also used \cref{hp:hypo21,hp:hypo22} to prove \cref{thm:thm1}.
\end{document}
Which generates the following output

Tangential Remark There are occasions when the underscore character does not work as equation label; I removed them from the code above in the course of debugging just to make sure they are not the issue. Generally I would recommend using colons :
instead.
ref=\theproposition~(\alpha*)
? If you are using it inside thehp
environment, shouldn't it be\theequation
since you are using the same universal counter? (Or at least, maybe\thehp
(I don't remember if amsthm duplicates the counter), but certainly not\theproposition
.)