1

I have a custom environment defined in a style file, for which I am trying to create a specific cref counter. Here is my environment and the code in mystyle.sty

\newcounter{exampleCtr}
\setcounter{exampleCtr}{0}

\newenvironment{LogicProgram}{%
    \refstepcounter{exampleCtr}%
    \start@align\tw@\st@rredtrue\m@ne
    \noindent\textbf{Example~\theexampleCtr}
    \hskip\parindent    
}{%
    \endalign   
}
\crefname{exampleCtr}{Example}{Examples}
\Crefname{exampleCtr}{Examples}{Examples}

When I try to create a logicprogram, and reference it with cref, I just get "??" (questionmarks) meaning, I suppose, that cref doesn't know what I want to reference to.

\begin{LogicProgram}
\label{ex:logic}
& \bot \leftarrow \neg F & : w
\end{LogicProgram}

I try to create a reference with \cref{ex:logic}, but nothing works. \Cref does work very well at other points in my document / work, but I cannot figure out why it cannot find the reference in my logicprogram environment.

I have tried Using cleveref with newenvironment but this does not work for my environment in my style file. What am I doing wrong / how to make this work?

  • I have provided a fix below, but this seems to be a very strange way to code mixing text and math mode material. What exactly are you trying to achieve? – Andrew Swann May 12 at 15:50
  • Thanks for the fix, that works for now! I have to admit I'm not very familiar with custom environments, and couldn't find a nice environment that allowed the slight indent along with the align — at least, I didn't get anything to work. I then found the idea of this environment definition on this website and changed it only slightly, because it gives the alignment that I wanted exactly. I'm definitely guilty of copying-pasting code here, without good knowledge of what I am doing... – Jacco.Oosterhuis May 12 at 17:01
  • Is this always a one-line environment, or do you need the multiline capabilities of align*? – Andrew Swann May 13 at 5:57
  • Multiline generally, the single line example was just to make it easy. – Jacco.Oosterhuis May 13 at 17:50
1

amsmath does all sorts of tricks with the labels in its environments. You need to restore the standard definition which is stored in \ltx@label and the syntax here is a little non-standard (as you will discover if you try a simple \let):

Sample output

\documentclass{article}

\usepackage{amsmath,cleveref}

\newcounter{exampleCtr}
\setcounter{exampleCtr}{0}

\makeatletter
\newenvironment{LogicProgram}{%
    \refstepcounter{exampleCtr}%
    \start@align\tw@\st@rredtrue\m@ne
    \renewcommand{\label}[1]{\ltx@label{{##1}}}
    \textbf{Example~\theexampleCtr}
    \hskip\parindent
}{%
  \endalign
}
\makeatother

\crefname{exampleCtr}{Example}{Examples}
\Crefname{exampleCtr}{Example}{Examples}

\begin{document}

\begin{LogicProgram}
\label{ex:logic}
& \bot \leftarrow \neg F & : w
\end{LogicProgram}

\Cref{ex:logic}

\end{document}

Note you need to include the \makeatletter...\makeatother pair around your code, and \noindent has no effect where you used it as you are already in display math, not an ordinary paragraph.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.