2

How do I need modify this source so that:

  1. The first output line of Chapter 1, currently "See ?? 1, below", becomes instead "See Problem 2.1, below"
  2. The first output line of Chapter 2, currently "Refer to ?? in these.", becomes instead "Refer to Problem 1 in these."?

Note that:

  • the theorem-like environment problems is not itself numbered, but when referred to in a different chapter, should have the word "Problem" followed by that chapter's number, then the item number as the reference ; and
  • when an item in a problemsenum environment is referenced within the same chapter where that environment appears, the reference should include the word "Problem".

I'm failing to understand how to use appropriate aliases or counters here.

\documentclass[oneside]{book}

\usepackage{amsthm}
\usepackage{thmtools}

\usepackage{aliascnt}
\usepackage[nameinlink]{cleveref}
\crefname{enumi}{}{} 

\declaretheoremstyle[numbered=no]{probs}
\declaretheorem[name=PROBLEMS,style=probs]{problems} 

\newenvironment{problemsenum}
{\begin{enumerate}%
Solve these problems.}%
{\end{enumerate}}

\crefname{problems}{Problem}{Problem}
\Crefname{problems}{Problem}{Problem}


\begin{document}

\chapter{}\label{chap:1}

See \cref{prob:my-problems} \cref{prob-part:first-problem}, below.

\chapter{}\label{chap:2}

Refer to \cref{prob-first-problem} in these.

\begin{problems}
\label{prob:my-problems}

\begin{problemsenum}

\item \label{prob-part:first-problem}
First problem.

\item 
Second problem.

\end{problemsenum}

\end{problems}

\end{document}

Partial work-around

With the chapter labels added in the edited version above, in the preamble make definitions:

\newcommand{\probref}[1]{Problem~\cref{#1}}
\newcommand{\otherprobref}[1]{Problem~\thechapter.\cref{#1}}

And then create the cross-references in chapters 1 and 2, respectively, as follows:

See \otherprobref{prob-part:first-problem}

Refer to \probref{prob-part:first-problem} in these.

Why the partial work-around is unsatisfactory. Unfortunately, if I now also include in preamble...

\usepackage[colorlinks=true]{hyperref} 

... then the links in chapters 1 and 2, respectively, will be only on the actual problem number (here, 1) rather than on the entire phrases "Problem 1.1" and "Problem 1". And that negates, for this type of cross-reference, the option nameinlink I'm using for cleveref.

Better partial work-around

In his answer to How suppress number of numbered theorem?, Christian Hupfer provides a clever new partial solution that:

  • does create cross-references with numbers to enumerate items within an unnumbered theorem-like environment (which was one of my goals); but
  • does not fulfill one of my requirements, namely, that when the reference is in the same chapter where that environment occurs, then the chapter number is suppressed in the reference.

Is there some way to modify Hupfer's answer so as to fulfill my requirement #2 at the top of the current post?

Here in essence is Hupfer's clever answer, modified slightly to fit my situation, with the unit divisions being chapters rather than sections (and added colored links, extra text to show readily where the links point, etc.):

\documentclass[oneside]{book}

\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{enumitem}
\usepackage[colorlinks, linkcolor=blue]{hyperref}

\declaretheoremstyle[numbered=yes,headpunct={}]{probs}
\declaretheorem[name=PROBLEMS,style=probs,numberwithin=chapter,preheadhook={\let\theproblems\relax},postheadhook={\newcommand{\theproblems}{\thechapter.\arabic{problems}}\leavevmode}]{problems} 

\newlist{probenum}{enumerate}{1}

\setlist[probenum,1]{label=({\arabic*)},ref={Problem~\thechapter\,(\arabic*)}}

\usepackage{lipsum}

\begin{document}

\chapter{First chapter}

In \ref{fooother} you will prove that \dots .

\begin{problems}
\begin{probenum}
  \item foo \label{foo} \lipsum[1]
  \item bar \label{bar} \lipsum[2]
\end{probenum}
\end{problems}

In \ref{bar} and \ref{foo}, you already proved that \dots .
In \ref{fooother} you will prove that \dots .


\chapter{Second chapter}

\begin{problems}
\begin{probenum}
  \item foo \label{fooother} \lipsum[3]
  \item bar \label{barother} \lipsum[4]
\end{probenum}
\end{problems}

Earlier in \ref{bar}, you had already showed that \dots ; now in \ref{fooother} you just showed that \dots .

\end{document}
murray
  • 7,944

1 Answers1

1

You want to number your problems environment, but hiding the number.

\documentclass[oneside]{book}

\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{enumitem}

\usepackage{aliascnt}
\usepackage[nameinlink]{cleveref}

\declaretheoremstyle[
  headformat=\NAME\NOTE,
]{probs}
\declaretheorem[
  name=PROBLEMS,
  style=probs,
]{problemsinner}
\newenvironment{problems}
  {\problemsinner Solve these problems.}
  {\endproblemsinner}

\newlist{problemsenum}{enumerate}{1}
\setlist[problemsenum,1]{label=\arabic*}
\makeatletter
\renewcommand{\p@problemsenumi}{\maybe@thechapter{\thechapter}}
\protected\def\maybe@thechapter#1{%
  \ifnum#1=\value{chapter}%
  \else
    #1.%
  \fi
}
\makeatother

\crefformat{problemsenumi}{#2Problem~#1#3}
\Crefformat{problemsenumi}{#2Problem~#1#3}
\crefformat{problemsinner}{#2#3}
\Crefformat{problemsinner}{#2#3}

\begin{document}

\chapter{First}\label{chap:1}

See \cref{prob-part:first-problem} in a different chapter.

\chapter{Second}\label{chap:2}

Refer to \cref{prob-part:first-problem} in this chapter.

\begin{problems}
\label{prob:my-problems}

\begin{problemsenum}

\item \label{prob-part:first-problem}
First problem.

\item 
Second problem.

\end{problemsenum}

\end{problems}

\end{document}

enter image description here

Technical note: I used A6 paper format to produce the image.

egreg
  • 1,121,712
  • Nice that this method does not require messing with preheadhook and postheadhook while retaining use of \newlist and \setlist. I had completely overlooked the possibility of using \crefformat. – murray Sep 05 '16 at 19:01
  • Seems to do everything I need, and it works nicely with hyperref, too. Let me just bang on this method within my complicated book-length (memoir) before accepting the answer. – murray Sep 05 '16 at 19:01
  • If in the example the item "Second problem" also has a label \label{prob-part:second-problem}, then neither \cref{prob-part:first-problem,prob-part:second-problem} nor \crefrange{prob-part:first-problem}{prob-part:second-problem}works here. – murray Sep 05 '16 at 20:22
  • I can live with the \crefrange issue by substituting \cref{...}--\creflabel{...}. Probably likewise for the \cref{..., ...} issue, but always a shame to have to do things ad hoc manually that cleveref was designed to do automatically. – murray Sep 05 '16 at 20:36
  • @murray You're asking for something the package is not designed for. – egreg Sep 05 '16 at 21:03
  • Which package are you referring to -- enumitem? – murray Sep 05 '16 at 22:15
  • I find that with several of these problems environments within a single chapter, it's easy to continue the same series of numbers from one to another by initializing a global counter (\setcounter{problemnumber}{1}) at the start of each chapter; and then add to the \setlist[problemsenum,1]... command the key expressions after=\stepcounter{problemsenumi}\setcounter{problemnumber}{\value{problemsenumi}}, start=\value{problemnumber}. Perhaps enumitem's start / resume keys can do the same thing, but I didn't see how. – murray Sep 05 '16 at 22:19
  • @murray I was referring to cleveref. I guess that you want to use resume and add code to \chapter in order to stop resuming. – egreg Sep 05 '16 at 22:31
  • resume does not work at all as option to \begin{problemsenum}; neither series=... nor resume are accepted as key options to \setlist[problemsenum,1]. Docs for enumitem just don't show how to use these with \setlist. – murray Sep 06 '16 at 15:23
  • Found a snag: if, as I do need, I include formatting of the label in problemsenum ( \setlist[problemsenum,1]{label=\bfseries\sffamily\arabic*.,.} ).then the link produced with \cref and (and usinghyperref) also has the item number formatted the same way. the link should be normal text, just with the coloring created by hyperref and not the bold and sans-serif that the corresponding item number gets when it appears in the list. – murray Sep 07 '16 at 16:50
  • And the link produced by \cref for a problemsenum item is also, unfortunately, picking up the period that my label key value must include. – murray Sep 07 '16 at 18:09
  • What is p@problemsenumithat is renewed in this method? – murray Nov 13 '22 at 17:06
  • @murray It's the prefix always prepended to the representation of the counter when writing in the .aux file. – egreg Nov 13 '22 at 17:09