This question follows on from this one, which I asked a few weeks ago. I have since discovered the cleveref package, and I would like to use it for ease in referring to theorem-like environments without having to remember whether a given statement was a theorem, a lemma, a proposition, etc.
Here is a small example document. When I compile this document, the final line of text begins "Here are some references to ?? 1.1, ?? 1.2 and theorem 1.3." The attempt to use \crefname
to set reference names (I'm not up on the terminology) for the different environments is not working. However, if the line marked with % (*)
is uncommented, the second "??" will become "toybox". So it is something to do with being in a \foreach
. But I can't find a way to make it work. It is as though the invocation of \crefname
is totally ignored within the \foreach
. Why does this happen?
\documentclass[a4paper]{amsart}
\def\myTheoremEnvironments{%
theorem/theorems,%
cobblestone/cobblestones,%
toybox/toyboxes%
}
\usepackage{tikz, titlecaps, cleveref}
% Number all definition, theorem, etc. environments using the same counter.
% Start counting again at the start of each section.
% Style these environments using LaTeX's "definition" style.
\theoremstyle{definition}
\newtheorem{baseTheorem}{Base Theorem}[section]
\foreach \x/\y in \myTheoremEnvironments {
\edef\tmp{\noexpand\newtheorem{\x}[baseTheorem]{\noexpand\titlecap{\x}}}\tmp
\crefname{\x}{\x}{\y}
}
%\crefname{toybox}{toybox}{toyboxes} % (*)
\begin{document}
\section{Hello, and welcome to my document.}
\begin{cobblestone}
\label{csref}
Hello. I am an ``cobblestone'' environment.
\end{cobblestone}
\begin{toybox}
\label{tbref}
I am a ``toybox'' environment. What a strange name that is for an environment.
\end{toybox}
\begin{theorem}
\label{thmref}
People trying to do strange things with ``foreach'' should expect trouble.
\end{theorem}
Here are some references to \cref{csref}, \cref{tbref} and \cref{thmref}.
I could have tried using just one call to ``cref'' here,
but it's best not to run before you can walk.
\end{document}
\foreach
executes its cycles in a group and\crefname
only does local assignments.cleveref
package, @TobyCubitt, appears now to be active on this StackExchange.\crefname
do local assignments. Luckily, my former and smarter self kindly left an explanation in the manual (implementation section). To quote myself: "\crefname
must not create global definitions, or else it will breakbabel
's\otherlanguage
and\foreignlanguage
commands." I no longer recall why this will breakbabel
, but presumably the answer's somewhere in thebabel
manual/code.