1
\documentclass[12pt, a4paper]{report}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[ruled]{algorithm2e}

\begin{document}

\begin{algorithm}
    \caption{my capt}
    \begin{minipage}{\dimexpr\textwidth-2\algomargin\relax}
    \begin{enumerate}
        \item step 1
        \item step 2
        \item step 3 
        \begin{equation}\label{alg1}
            \tau = x + y.
        \end{equation}
        \item step 5
    \end{enumerate}
    \end{minipage}
\end{algorithm}
I have an equation:
\begin{equation}\label{myeq}
        \alpha = x^2 y.
    \end{equation}
\end{document}

Here in the above MWE, I use a minipage environment to avoid getting an overfull hbox error. I want to align the equation numbers vertically inside and outside the algorithm. It looks like in the figure below. I want to align equations (1) and (2):

It looks like this. I want to align equation numbers (1) and (2).

How can I do that?

This question is very similar to the one posted here: https://tex.stackexchange.com/a/460466/139314 Instead of a box, I have an algorithm environment.

Many thanks in advance.

Lynx
  • 67
  • If you want it perfectly aligned, repeat both the minipage and use \item[] (itemize instead of enumerate). – John Kormylo Feb 01 '22 at 15:35
  • I either couldn't get it, or it didn't work. I added one more minipage and used itemize, and it pushed the equation number (1) more inside. – Lynx Feb 01 '22 at 15:49
  • When I do that I get an overfull hbox error (that's why I used minipage), and the line is too wide. Also, I want to have it with enumerate if possible. Thank you for the help. – Lynx Feb 01 '22 at 16:01
  • Interestingly, algorithm does shift everything to the right by \algomargin, but it does not change @totalleftmargin, \leftskip, or \linewidth. It does change \hsize. – John Kormylo Feb 01 '22 at 16:23

1 Answers1

2

This aligns the inner equation to the outer equation (as opposed to aligning the outer to the inner). It uses \makebox to overlap the left margin, and negative \hspace to overlap the right margin.

enter image description here

\documentclass[12pt, a4paper]{report}
\usepackage{mathtools}
\usepackage[ruled]{algorithm2e}

\begin{document}

\begin{algorithm} \caption{my capt} \begin{enumerate} \item step 1 \item step 2 \item step 3\ \makebox[\dimexpr \linewidth-2\algomargin][r]{% overlap left margin \begin{minipage}{\textwidth}% equation is in vmode \begin{equation}\label{alg1} \tau = x + y. \end{equation} \end{minipage}\hspace{-\algomargin}}% overlap right margin \item step 5 \end{enumerate} \end{algorithm} I have an equation:

\begin{equation}\label{myeq} \alpha = x^2 y. \end{equation}

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Thank you very much! That looks great, but I get the error: Overfull \hbox (17.62482pt too wide) detected. What can be wrong? – Lynx Feb 01 '22 at 17:18
  • Not sure, but I didn't get overfull error with \textwidth-\algomargin. The new version uses \textwidth-2\algomargin – John Kormylo Feb 01 '22 at 22:43
  • I think linebreak \\ causes this error. I keep getting it, although I tried the new version. – Lynx Feb 02 '22 at 09:16
  • Does \newline or \par work better? – John Kormylo Feb 02 '22 at 15:17
  • No, none of them has worked. But, I have found a workaround using your method: instead of using enumerate, I used linesnumbered option. For the line with an equation it was adding a line number and I disable it using this trick: https://tex.stackexchange.com/questions/153646/algorithm2e-disabling-line-numbers-for-specific-lines

    Works perfectly now! Thank you so much, again!

    – Lynx Feb 02 '22 at 15:42