2

I want to have all my theorems, equations and assumptions with the same global numbering, depending on sections and subsections. I also want to reference individual bullet points in an enumerate inside an assumptions.

I have used the following code:

\documentclass{article}
\title{Untitled Document}
\author{Your Name}
\date{\today}
\usepackage{amsfonts}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{enumitem}
\usepackage{mathtools}
\usepackage{cleveref}

% Global numbering
\counterwithin*{equation}{section}
\counterwithin*{equation}{subsection}
\renewcommand{\theequation}{%
  \thesection.%
  \ifnum\value{subsection}>0 \arabic{subsection}.\fi
  \arabic{equation}%
}

% 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=\thehp~(\alph*)}
\crefalias{hypothesisenumi}{hp}

\begin{document}
\maketitle

\section{A section}
\section{Another section}
\subsection{Subsection of another section}

\begin{thm}[A theorem]
\end{thm}

\begin{hp}[Admissible shapes]\label{hp:hypo}
\hfill
\begin{hypothesisenum}
    \item  \label{hp:hypo_1} first
    \item \label{hp:hypo_2} second
\end{hypothesisenum}
\end{hp}

\begin{thm}[A theorem]
\end{thm}

We used \cref{hp:hypo_1,hp:hypo_2,hp:hypo}.

\end{document}

which just doesn't work:

enter image description here

I would in fact expect the things circled in blue not to be there.

3
  • 3
    Can you product a compilable minimal example instead of a bunch of snipplets? It will make it easy for us to test and see what's going on. Jun 2 at 1:42
  • That said, why are you using ref=\theproposition~(\alpha*)? If you are using it inside the hp 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.) Jun 2 at 1:45
  • @WillieWong I have done a major reformatting of the question, thank you for the prompt.
    – Lilla
    Jun 2 at 7:35

1 Answer 1

2

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:

  1. 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.
  2. 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

enter image description here

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.

1
  • I couldn't have asked for a better answer: it solves the problem, it does so instructively, and you can read it at multiple levels (you want to fix the problem? You also want to understand what was wrong? Both are possible and independent). Thanks a lot
    – Lilla
    Jun 2 at 19:18

You must log in to answer this question.

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