Math & ScienceAlign equations in pseudocode

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
loujing
Posts: 9
Joined: Mon Jun 01, 2015 7:13 pm

Align equations in pseudocode

Post by loujing »

Hello,

Sorry, my english is poor.
I have written a pseudocode in which there are some equations like the figure blow.
QQ截图20150602005659.png
QQ截图20150602005659.png (34.45 KiB) Viewed 11173 times
Now i would like the equations from lines #4-#7 to align at \leftarrow , and the symbols on the left size of \leftarrow are right aligned. The others remain unchanged.

The attachment is my source file.

Thank you for your kind help.

Code: Select all

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage[cmex10]{amsmath}

\begin{document}
\def\NoNumber#1{{\def\alglinenumber##1{}\State #1}\addtocounter{ALG@line}{-1}}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\begin{algorithm}[h]
	\caption{******}
	\label{algo:stageII}
	\begin{algorithmic}[1]
	\Require ******
	\Ensure ******
	
	\State {******}
	\For {******}
		\State{
			$\begin{aligned}
			({R}_{\alpha}, {R}_{\beta})\leftarrow &\text{{\tt{\textbf{FUNC}}}}(******) \\
			 & \wedge \text{{\tt{\textbf{FUNC}}}}(******)
			\end{aligned}$
		}
		\Comment {******}
		\State {${R}_{{S}_{k}}\leftarrow ******$}
		\State {${G}_{{S}_{k}}\leftarrow$ {\tt{\textbf{FUNC}}}$(******)$}
		\State {$T\leftarrow$ {\tt{\textbf{FUNC}}}$(******)$}
		\Comment {******}
		\State {${I}_{T}\leftarrow$ {\tt{\textbf{FUNC}}}$(******)$}
		\NoNumber {${I}_{{T}^{-}}\leftarrow$ {\tt{\textbf{FUNC}}}$(******)$}
		\Comment {******}
		\NoNumber {${I}_{{T}^{+}}\leftarrow$ {\tt{\textbf{FUNC}}}$(******)$}
		\Comment {******}
	\EndFor
  \end{algorithmic}
\end{algorithm}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Align equations in pseudocode

Post by Johannes_B »

Hi and welcome,

very good that you provided an example code. It is much more comfortable if a helper does not have to retype text and knows exactely how the output is generated.

So far, i could come up with the following. The symbols are not aligned to the right, but looks much better know. Maybe someone else has a better idea.

Code: Select all

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage[cmex10]{amsmath}
\usepackage{mathtools}
\usepackage{xparse}
\DeclareDocumentCommand{\jingAlgo}{ m O{\leftarrow} }{%
	{\rlap{$#1$} \hphantom{text}$#2$}%
}
\ProvideDocumentCommand{\FUNC}{}{{\textbf{\ttfamily FUNC}}}
\begin{document}
\def\NoNumber#1{{\def\alglinenumber##1{}\State #1}\addtocounter{ALG@line}{-1}}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\begin{algorithm}[h]
	\caption{******}
	\label{algo:stageII}
	\begin{algorithmic}[1]
		\Require ******
		\Ensure ******

		\State {******}
		\For {******}
		\State{
			$\begin{aligned}
				({R}_{\alpha}, {R}_{\beta})\leftarrow &\text{{\tt{\textbf{FUNC}}}}(******) \\
				& \wedge \text{{\tt{\textbf{FUNC}}}}(******)
			\end{aligned}$
		}
		\Comment {******}
		\State {\jingAlgo{R_{S_k}} $******$}
		\State {\jingAlgo{R_{S_k}} $******$}
		\State {\jingAlgo{G_{S_k}} \FUNC$(******)$}
		\State {\jingAlgo{T} \FUNC$(******)$}
		\Comment {******}
		\State {\jingAlgo{{I}_{T}} \FUNC$(******)$}
		\NoNumber {\jingAlgo{{I}_{{T}^{-}}} \FUNC$(******)$}
		\Comment {******}
		\NoNumber {\jingAlgo{{I}_{{T}^{+}}}[\rightarrow] \FUNC$(******)$}
		\Comment {******}
	\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
loujing
Posts: 9
Joined: Mon Jun 01, 2015 7:13 pm

Align equations in pseudocode

Post by loujing »

Johannes_B wrote:Hi and welcome,

very good that you provided an example code. It is much more comfortable if a helper does not have to retype text and knows exactely how the output is generated.

So far, i could come up with the following. The symbols are not aligned to the right, but looks much better know. Maybe someone else has a better idea.
Ok, it's very nice!
Thank you for your kind help.
Post Reply