2

The following LaTeX code was saved in ~\Test.tex.

\documentclass{article}
\usepackage{cleveref}
\begin{document}
\begin{equation}\label{q}
x \leq y
\end{equation}
Behold~\cref{q}. 
\end{document}

Then the following commands were executed in the Terminal.

> cd ~
> lualatex Test

Consequently a PDF file was generated in ~\Test.pdf. When opened in a PDF viewer, the file displayed as follows. (I screencaptured only the relevant part of the display.)

Equality environment

I would like to create an equality-like environment named inequality, which will be used to typeset inequalities, and whose cross-reference will read ineq. instead of eq. . The effect of this new feature should be such that if in the above LaTeX code the two occurrences of equality are replaced by inequality, the resulting PDF file will be almost the same as before, except that the abbreviation eq. will be replaced by ineq.. The new inequality environment should share a counter with the equality environment.


An attempt

I tried carrying out my goal using the following code.

\documentclass{article}
\usepackage{cleveref}
\newenvironment{inequality}{\begin{equation}}{\end{equation}}
\crefname{inequality}{ineq.}{ineqs.}
\Crefname{inequality}{Ineq.}{Ineqs.}
\begin{document}
\begin{inequality}\label{q}
x \leq y
\end{inequality}
Behold~\cref{q}. 
\end{document}

However, the resulting PDF file looked the same as before. In particular the cross-reference was written as eq., and not as ineq.

Evan Aad
  • 11,066

2 Answers2

1

You can do this without creating a new environment, simply by cleveref:

\documentclass{article}
\usepackage{cleveref}
\newcounter{inequality}
\crefname{inequality}{Inequality}{Inequalities}
\begin{document}

\begin{equation}\label[inequality]{q} x \leq y \end{equation} Behold~\cref{q}. \end{document}

If you want to retain the (continous) numbering with the equations, you can use the aliascnt package.

\documentclass{article}
\usepackage{aliascnt}
\usepackage{cleveref}
\newaliascnt{inequality}{equation}
\crefname{inequality}{ineq.}{ineqs.}
\begin{document}

\begin{equation}\label[inequality]{q} x \leq y \end{equation}

\begin{equation}\label{r} x = y \end{equation} Behold~\cref{q} and \cref{r}. \end{document}

Update: This builds on the previous solutions, by creating indeed a new environment. The label type is overridden in the environment to inequality, so that it must not be provided.

\documentclass{article}
\usepackage{aliascnt}
\usepackage{cleveref}
\newaliascnt{inequality}{equation}
\crefname{inequality}{ineq.}{ineqs.}
\makeatletter

\newenvironment{inequality}{ \begin{equation} \protected@edef\cref@currentlabel{% \expandafter\cref@override@label@type% \cref@currentlabel@nil{inequality}}% }{ \end{equation} }

\begin{document}

\begin{inequality}\label{in1} x \leq y \end{inequality}

\begin{equation}\label{eq1} x = y \end{equation}

\begin{inequality}\label{in2} x \geq y \end{inequality}

\begin{equation}\label{eq2} x + 1 = y + 1 \end{equation}

Behold~\cref{in1} and \cref{eq2} and \cref{in2} and \cref{eq2}.

\end{document}

BotondF
  • 111
  • Thanks. This might work if I was dealing with only a handful of inequalities, but I'm dealing with a lot of inequalities. An inequality environment will reduce the amount of typing required, and the amount of typing errors. – Evan Aad Dec 05 '22 at 09:54
  • Additionally, not all inequalities are going to be labelled, and I'd like to differentiate them from equations easily. – Evan Aad Dec 05 '22 at 10:06
  • 1
    This answer is a duplicate of this one. – Evan Aad Dec 05 '22 at 12:52
  • 1
    Thanks for the comments, I've now posted an update. – BotondF Dec 05 '22 at 21:08
  • The update is good. However, I'd like to wait a little before I accept your answer, so I can possibly get more suggestions. Because while your update does answer my question under the conditions presented in my question, in real life the conditions I work under are more complicated and make your answer not ideal. (If you wish, I can explain exactly how your answer fails in my real world scenario. Again, for the scenario described in my question, your update works very well.) – Evan Aad Dec 06 '22 at 05:30
  • FYI I've described the scenario under which your solution doesn't work well in this new post. – Evan Aad Dec 06 '22 at 13:06
  • I've decided to accept egreg's answer rather than yours, even though your answer works just as well as his, and even though you answered first, because his answer doesn't require the use of the aliascnt package which is complicated, difficult to understand, and error-prone. – Evan Aad Dec 06 '22 at 16:33
1

Use \crefalias (section 6 in the manual of cleveref).

\documentclass{article}
\usepackage{cleveref}

\newenvironment{inequality} {\crefalias{equation}{inequality}\begin{equation}} {\end{equation}\ignorespacesafterend} \crefname{inequality}{ineq.}{ineqs.} \Crefname{inequality}{Ineq.}{Ineqs.}

\begin{document}

\begin{inequality}\label{q} x \leq y \end{inequality} Behold~\cref{q}.

\begin{equation}\label{r} x = y \end{equation} Behold~\cref{r}.

\end{document}

enter image description here

If you want to get the same output as for equations, you need to use \crefformat and friends.

egreg
  • 1,121,712
  • Thank you. A very elegant solution. However, there's a problem with it, albeit not one that can be seen under the conditions presented in my original question. I'll briefly explain the problem. If you can modify your solution to address the issue, it'll be great. If not, please tell me if I should start a separate post for it. (To be continued in the next comment.) – Evan Aad Dec 06 '22 at 10:46
  • The problem is this. If the document's language is Hebrew (via babel), the showlabels package doesn't work with your solution. To see this, first add the following command to the preamble and recompile to display the labels: \usepackage{showlabels}. Then add the following two commands to the preamble to set the document's language to Hebrew: \usepackage[bidi=basic,hebrew,provide=*]{babel}\babelfont{rm}[Renderer=HarfBuzz]{FreeSerif}. Recompile. The labels disappear! – Evan Aad Dec 06 '22 at 10:51
  • 1
    @EvanAad that requires a whole new question. – egreg Dec 06 '22 at 11:24
  • OK. Here's the new question. – Evan Aad Dec 06 '22 at 13:05