LaTeX forum ⇒ Math & ScienceAlign equations in pseudocode Topic is solved

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

Align equations in pseudocode

Postby loujing » Mon Jun 01, 2015 8:04 pm

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 5066 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.

  1. \documentclass{article}
  2. \usepackage{algorithm}
  3. \usepackage[noend]{algpseudocode}
  4. \usepackage[cmex10]{amsmath}
  5.  
  6. \begin{document}
  7. \def\NoNumber#1{{\def\alglinenumber##1{}\State #1}\addtocounter{ALG@line}{-1}}
  8. \renewcommand{\algorithmicrequire}{\textbf{Input:}}
  9. \renewcommand{\algorithmicensure}{\textbf{Output:}}
  10. \begin{algorithm}[h]
  11. \caption{******}
  12. \label{algo:stageII}
  13. \begin{algorithmic}[1]
  14. \Require ******
  15. \Ensure ******
  16.  
  17. \State {******}
  18. \For {******}
  19. \State{
  20. $\begin{aligned}
  21. ({R}_{\alpha}, {R}_{\beta})\leftarrow &\text{{\tt{\textbf{FUNC}}}}(******) \\
  22. & \wedge \text{{\tt{\textbf{FUNC}}}}(******)
  23. \end{aligned}$
  24. }
  25. \Comment {******}
  26. \State {${R}_{{S}_{k}}\leftarrow ******$}
  27. \State {${G}_{{S}_{k}}\leftarrow$ {\tt{\textbf{FUNC}}}$(******)$}
  28. \State {$T\leftarrow$ {\tt{\textbf{FUNC}}}$(******)$}
  29. \Comment {******}
  30. \State {${I}_{T}\leftarrow$ {\tt{\textbf{FUNC}}}$(******)$}
  31. \NoNumber {${I}_{{T}^{-}}\leftarrow$ {\tt{\textbf{FUNC}}}$(******)$}
  32. \Comment {******}
  33. \NoNumber {${I}_{{T}^{+}}\leftarrow$ {\tt{\textbf{FUNC}}}$(******)$}
  34. \Comment {******}
  35. \EndFor
  36. \end{algorithmic}
  37. \end{algorithm}
  38. \end{document}

Tags:

User avatar
Johannes_B
Site Moderator
Posts: 4044
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Mon Jun 01, 2015 9:08 pm

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.

  1. \documentclass{article}
  2. \usepackage{algorithm}
  3. \usepackage[noend]{algpseudocode}
  4. \usepackage[cmex10]{amsmath}
  5. \usepackage{mathtools}
  6. \usepackage{xparse}
  7. \DeclareDocumentCommand{\jingAlgo}{ m O{\leftarrow} }{%
  8. {\rlap{$#1$} \hphantom{text}$#2$}%
  9. }
  10. \ProvideDocumentCommand{\FUNC}{}{{\textbf{\ttfamily FUNC}}}
  11. \begin{document}
  12. \def\NoNumber#1{{\def\alglinenumber##1{}\State #1}\addtocounter{ALG@line}{-1}}
  13. \renewcommand{\algorithmicrequire}{\textbf{Input:}}
  14. \renewcommand{\algorithmicensure}{\textbf{Output:}}
  15. \begin{algorithm}[h]
  16. \caption{******}
  17. \label{algo:stageII}
  18. \begin{algorithmic}[1]
  19. \Require ******
  20. \Ensure ******
  21.  
  22. \State {******}
  23. \For {******}
  24. \State{
  25. $\begin{aligned}
  26. ({R}_{\alpha}, {R}_{\beta})\leftarrow &\text{{\tt{\textbf{FUNC}}}}(******) \\
  27. & \wedge \text{{\tt{\textbf{FUNC}}}}(******)
  28. \end{aligned}$
  29. }
  30. \Comment {******}
  31. \State {\jingAlgo{R_{S_k}} $******$}
  32. \State {\jingAlgo{R_{S_k}} $******$}
  33. \State {\jingAlgo{G_{S_k}} \FUNC$(******)$}
  34. \State {\jingAlgo{T} \FUNC$(******)$}
  35. \Comment {******}
  36. \State {\jingAlgo{{I}_{T}} \FUNC$(******)$}
  37. \NoNumber {\jingAlgo{{I}_{{T}^{-}}} \FUNC$(******)$}
  38. \Comment {******}
  39. \NoNumber {\jingAlgo{{I}_{{T}^{+}}}[\rightarrow] \FUNC$(******)$}
  40. \Comment {******}
  41. \EndFor
  42. \end{algorithmic}
  43. \end{algorithm}
  44. \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

Postby loujing » Tue Jun 02, 2015 9:33 am

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.


Return to “Math & Science”

Who is online

Users browsing this forum: No registered users and 2 guests