0

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

\documentclass{article}
%\usepackage[bidi=basic,hebrew,provide=*]{babel}
%\babelfont{rm}[Renderer=HarfBuzz]{FreeSans}

\usepackage{amsmath}

\usepackage{aliascnt} \newaliascnt{inequality}{equation} \aliascntresetthe{inequality}

\makeatletter \def\inequality{$$\refstepcounter{inequality}} \def\endinequality{\eqno\hbox{@eqnnum}$$@ignoretrue} \makeatother \begin{document} \begin{equation}\label{eq} 1=1 \end{equation} \begin{inequality}\label{ineq} 1\neq2 \end{inequality} \end{document}

The code uses the method described in this answer to create a new equation-like environment called inequality, whose counter is aliased to the counter of the equation environment. This trick is intended to be used in conjunction with cleveref cross-references, but for the purposes of this question cross-referencing is redudant, and therefore was not exercised in the MWE above.

Then the following commands were executed in the Terminal:

> cd ~
> lualatex Test

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

Inequality in an English document

As can be seen, the numbers of both the equation and the inequality appear on the right side of the text region.

The two commented LaTeX lines were now uncommented to establish Hebrew as the document's main language, and the code was recompiled. The resulting PDF file looked as follows.

Inequality in a Hebrew document

As can be seen, the number of the inequality switched sides and is now left-aligned, whereas the number of the equality remains right-aligned.

This behavior is undesirable. The inequality's number should be right-aligned to match the alignment of the equality's number. How can this be accomplished?

Evan Aad
  • 11,066

2 Answers2

2

\def, $$ and \eqno are low level tex primitves. Naturally if you cut through latex interfaces and code at that level you may need to do more work "by hand". Here changing \eqno to \leqno depending on the directionality.

enter image description here

\documentclass{article}
\usepackage[bidi=basic,hebrew,provide=*]{babel}
\babelfont{rm}[Renderer=HarfBuzz]{FreeSans}

\usepackage{amsmath}

\usepackage{aliascnt} \newaliascnt{inequality}{equation} \aliascntresetthe{inequality}

\makeatletter \def\inequality{$$\refstepcounter{inequality}} \def\endinequality{\leqno\hbox{@eqnnum}$$@ignoretrue} \makeatother \begin{document} \begin{equation}\label{eq} 1=1 \end{equation} \begin{inequality}\label{ineq} 1\neq2 \end{inequality} \end{document}

David Carlisle
  • 757,742
  • This is a good solution. However, if I now add \usepackage{showlabels} (I rely on the showlabels package a lot in the "real world"), the braces around the inequalitie's label are turned inside out. It's not catastrophic, but it's quite annoying. – Evan Aad Dec 05 '22 at 16:22
  • But the vertical spacing before and after the equation can be wrong (larger than the correct one). – Javier Bezos Dec 05 '22 at 16:27
  • @EvanAad well that is to be expected if you use $$ you can not expect latex packages written last millenium to magically adapt to your definition. You are generating the issue by not using equation so odd to get annoyed by that – David Carlisle Dec 05 '22 at 16:29
  • @JavierBezos sure, $$ is not right but it is no more or less right in RTL text. – David Carlisle Dec 05 '22 at 16:31
  • @DavidCarlisle I don't really understand the internals of the code I used. I simply copied it from this answer. – Evan Aad Dec 05 '22 at 16:35
  • @EvanAad there is no $$ or \eqno in that link – David Carlisle Dec 05 '22 at 16:40
  • Oops. You're right. My apologies. I meant this answer. – Evan Aad Dec 05 '22 at 16:43
  • 1
    If you replace $$ by \[ and \], respectively, the braces around the label face the correct direction. However, the label is then positioned inside the text region rather than in the margin where it should be. – Evan Aad Dec 05 '22 at 17:23
  • @EvanAad \(l)eqno in \[ (or $$) is not supported latex syntax so anything showlabels does with the label is accidental untested code paths and not a bug. – David Carlisle Dec 05 '22 at 17:31
1

Your code is low-level (\eqno and \hbox are not really LaTeX commands). I’m not sure what’s the purpose of your definitions, but try with:

\newenvironment{inequality}
  {\[%
   \refstepcounter{inequality}%
   \tag{\theinequality}}
  {\]\ignorespacesafterend}

Edit. Replaced the equation environment by \[...\] (see comments).

Javier Bezos
  • 10,003
  • To clarify to my future self: the code in Javier's answer is supposed to replace the part of my MWE between (and including) the \makeatletter and \makeatother commands. – Evan Aad Dec 05 '22 at 16:45
  • 1
    Yes, it replaces that part. – Javier Bezos Dec 05 '22 at 16:51
  • I'm sorry. I've unaccepted your answer. After further tests I've realized there are some serious issues with the way your code interacts with cleverefs. I will open a new question about it. – Evan Aad Dec 05 '22 at 17:11
  • I'm managed to resolve one of the serious issues. With your code a \cref to the inequality cites a wrong number. However, if you replace your \begin{equation} with \[, and your \end{equation} with \], then \cref reports the correct number. – Evan Aad Dec 05 '22 at 17:19
  • The second serious issue, which I haven't managed to resolve, is that when your code is used in an English document, \cref doesn't say "ineq.", it says "eq." In a Hebrew document this problem doesn't surface. – Evan Aad Dec 05 '22 at 17:21
  • I've written this other post to address a problem with your solution: that it fails when the document's language is NOT Hebrew. – Evan Aad Dec 05 '22 at 18:18
  • I've accepted your answer even though it doesn't work in an English document, because working in an English document was not a requirement in my question. In a Hebrew document I prefer your solution over David Carlisle's, because when the showlabels package is loaded, David's solution places the label inside the equation, whereas yours places it in the margin. – Evan Aad Dec 06 '22 at 16:38
  • Having said that, Ulrike Fischer's solution, posted as an answer to another question of mine, is the one I'm going to deploy in the "real world", since it works both in an English document as well as in a Hebrew one, and because it doesn't require the use of the aliascnt package. – Evan Aad Dec 06 '22 at 16:40