11

I am trying to refer to a newenvironment using cref from the cleveref package. Here is a MWE that demonstrates what I am trying to do.

\documentclass{article}
\usepackage{cleveref}

% define a newenvironment
\newcounter{projectCounter}
\setcounter{projectCounter}{0}
\newenvironment{project}[1]{\par\addvspace{1\baselineskip minus
    20pt}\refstepcounter{projectCounter}{\bf\large Project
    \arabic{projectCounter}: #1}\newline}{\par\addvspace{1\baselineskip minus 20pt}}

% define another newenvironment
\newcounter{subprojectCounter}
\setcounter{subprojectCounter}{0}
\newenvironment{subproject}[1]{\par\addvspace{1\baselineskip minus
    20pt}\refstepcounter{subprojectCounter}{\bf\large Sub-project
    \arabic{projectCounter}.\arabic{subprojectCounter}: #1}\newline}{\par\addvspace{1\baselineskip minus 20pt}}

% define a nested new environment
\newenvironment{projectStack}{\begin{project}\begin{subproject}}{\end{subproject}\end{project}}

% give names to the cref calls.
\crefname{project}{ project }{ projects }
\Crefname{project}{ Project }{ Projects }
\crefname{subproject}{ sub-project }{ sub-projects }
\Crefname{subproject}{ Sub-project }{ Sub-projects }


\begin{document}
This is a document that talks about sub-projects within projects.

\begin{project}{A}
Some information related to the project.

% begin a sub-project
\begin{subproject}{AA}
Some information related to the subproject.
\label{subproj:AA}
\end{subproject}

% begin another sub-project
\begin{subproject}{BB}
Some information related to the subproject.
\label{subproj:BB}
\end{subproject}

% begin a sub-project
\begin{subproject}{CC}
Some information related to the subproject.
\label{subproj:CC}
\end{subproject}

\label{proj:A}
\end{project}

Now let me try to refer to the projects, like so, \cref{proj:A}, and to
the subprojects, like so, \cref{subproj:AA,subproj:BB,subproj:CC}.
\end{document}

Here, I define the project and subproject newenvironments, and then try to refer to them using the cleveref cref function. This however does not seem to work, even though I have used the crefname and the Crefname functions.

Any help in resolving this issue would be very helpful.

1 Answer 1

14

\crefname and the like should refer to a counter, not to an environment, so you should say

\crefname{projectCounter}{project}{projects}

Here's a complete example, with some corrections to your macros.

\documentclass{article}
\usepackage{cleveref}

% define a newenvironment
\newcounter{projectCounter}
\newenvironment{project}[1]
  {\par\addvspace{1\baselineskip minus .3\baselineskip}%
   \refstepcounter{projectCounter}%
   \noindent{\bfseries\large Project \theprojectCounter: #1}\newline}
  {\par\addvspace{1\baselineskip minus .3\baselineskip}}

% define another newenvironment
\newcounter{subprojectCounter}[projectCounter]
\renewcommand\thesubprojectCounter{\theprojectCounter.\arabic{subprojectCounter}}
\newenvironment{subproject}[1]
  {\par\addvspace{1\baselineskip minus .3\baselineskip}%
   \refstepcounter{subprojectCounter}%
   \noindent{\bfseries\large Sub-project \thesubprojectCounter: #1}\newline}
  {\par\addvspace{1\baselineskip minus .3\baselineskip}}

% define a nested new environment
\newenvironment{projectStack}
  {\project\subproject}
  {\endsubproject\endproject}

% give names to the cref calls.
\crefname{projectCounter}{project}{projects}
\Crefname{projectCounter}{Project}{Projects}
\crefname{subprojectCounter}{sub-project}{sub-projects}
\Crefname{subprojectCounter}{Sub-project}{Sub-projects}


\begin{document}
This is a document that talks about sub-projects within projects.

\begin{project}{A}
Some information related to the project.

% begin a sub-project
\begin{subproject}{AA}
Some information related to the subproject.
\label{subproj:AA}
\end{subproject}

% begin another sub-project
\begin{subproject}{BB}
Some information related to the subproject.
\label{subproj:BB}
\end{subproject}

% begin a sub-project
\begin{subproject}{CC}
Some information related to the subproject.
\label{subproj:CC}
\end{subproject}

\label{proj:A}
\end{project}

Now let me try to refer to the projects, like so, \cref{proj:A}, and to
the subprojects, like so, \cref{subproj:AA,subproj:BB,subproj:CC}.
\end{document}
0

You must log in to answer this question.

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