Advertisement
Guest User

LaTeX MWE refering to vertical line on algorithm package

a guest
Jan 20th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. \documentclass[final,5p,times,twocolumn,authoryear]{elsarticle}
  2.  
  3.  
  4.  
  5. \usepackage{algorithm}
  6. \usepackage{algpseudocode}
  7. \usepackage{etoolbox}
  8. \usepackage{lipsum}
  9.  
  10. \usepackage{amsmath,amsfonts,amssymb,yfonts}
  11. \usepackage{cleveref}
  12.  
  13.  
  14.  
  15. % Creating new commands...
  16.  
  17. \newcommand{\Test}{$\mathcal{D}_{i^{\ast}\xi\textfrak{N}^{i^{\ast}}}$}
  18.  
  19.  
  20. % Patch to correct the way of vertical lines are align or arranged throughout the code using long texts in a line, becouse theses ones is broken due to long texts. This is solution provid by <https://tex.stackexchange.com/a/524700/155670>.
  21.  
  22. \makeatletter
  23. % start with some helper code
  24. % This is the vertical rule that is inserted
  25. \newcommand*{\algrule}[1][\algorithmicindent]{%
  26.     \hspace*{.2em}% <------------- This is where the rule starts from
  27.     \vrule %height .75\baselineskip depth .25\baselineskip
  28.     \hspace*{\dimexpr#1-.2em-.4pt}%
  29. }
  30.  
  31. \newcommand{\StatePar}[1]{%
  32.     \State\parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{\strut #1\strut}%
  33.     \vspace{-\lineskip}
  34. }
  35.  
  36. \newcount\ALG@printindent@tempcnta
  37. \def\ALG@printindent{%
  38.     \ifnum \theALG@nested > 0% is there anything to print
  39.     \ifx\ALG@text\ALG@x@notext% is this an end group without any text?
  40.     % do nothing
  41.     \else
  42.     \unskip
  43.     % draw a rule for each indent level
  44.     \ALG@printindent@tempcnta=1
  45.     \loop
  46.     \algrule[\csname ALG@ind@\the\ALG@printindent@tempcnta\endcsname]%
  47.     \advance \ALG@printindent@tempcnta 1
  48.     \ifnum \ALG@printindent@tempcnta<\numexpr\theALG@nested+1\relax
  49.     \repeat
  50.     \fi
  51.     \fi
  52. }
  53. % the following line injects our new indent handling code in place of the default spacing
  54. \patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
  55. \patchcmd{\ALG@doentity}{\item[]\nointerlineskip}{}{}{} % no spurious vertical space
  56. % end vertical rule patch for algorithmicx
  57. \makeatother
  58.  
  59. % Add \struts to keywords
  60. \algrenewcommand\algorithmicend{\strut\textbf{end}}
  61. \algrenewcommand\algorithmicdo{\strut\textbf{do}}
  62. \algrenewcommand\algorithmicwhile{\strut\textbf{while}}
  63. \algrenewcommand\algorithmicfor{\strut\textbf{for}}
  64. \algrenewcommand\algorithmicforall{\strut\textbf{for all}}
  65. \algrenewcommand\algorithmicloop{\strut\textbf{loop}}
  66. \algrenewcommand\algorithmicrepeat{\strut\textbf{repeat}}
  67. \algrenewcommand\algorithmicuntil{\strut\textbf{until}}
  68. \algrenewcommand\algorithmicprocedure{\strut\textbf{procedure}}
  69. \algrenewcommand\algorithmicfunction{\strut\textbf{function}}
  70. \algrenewcommand\algorithmicif{\strut\textbf{if}}
  71. \algrenewcommand\algorithmicthen{\strut\textbf{then}}
  72. \algrenewcommand\algorithmicelse{\strut\textbf{else}}
  73.  
  74. \algrenewcommand\algorithmicrequire{\strut\textbf{Input:}}
  75. \algrenewcommand\algorithmicensure{\strut\textbf{Output:}}
  76.  
  77. \let\oldState\State
  78. \renewcommand{\State}{\oldState\strut}
  79.  
  80.  
  81.  
  82. \begin{document}
  83.  
  84.  
  85. \begin{align}
  86.     a & = b \label{eq:1}
  87. \end{align}
  88.  
  89. % ---> Algorithm 01.
  90. \begin{algorithm}
  91.     \caption{Arbitrary Algorithm}
  92.     \begin{algorithmic}[1]
  93.         \Require A matrix $\mathbf{A}$ of size $m\times n$.
  94.         \Ensure Something.  
  95.         \Procedure{MainProcedure}{}
  96.             \State $a\gets{}b$
  97.             \For{$i$ in $m$}
  98.                 \For{$j$ in $n$}
  99.                 \showoutput
  100.                     \If{$i = j$}
  101.                         \State Obtain \Test
  102.                         \State $\textfrak{M}^{(l)}$
  103.                         \State $\textfrak{M}^{(l)}$
  104.                         \State $\displaystyle{\sum_{i=1}^{c}{\alpha^{(b^{\ast})}}}$
  105.                         \State According to \cref{eq:1}
  106.                         \State According to \cref{eq:1}
  107.                         \State According to \cref{eq:1}
  108.                         \State $\beta = \displaystyle{\max_{i=1}^{c}\!\left(\alpha^{(\beta^{\ast})}\right)}$   
  109.                         \StatePar{Select a random action. \lipsum[1]}
  110.                     \Else
  111.                         \If{$i = j + 1$}
  112.                             \State Stay silent
  113.                         \Else
  114.                             \State Break
  115.         \algstore{bkbreak}
  116.     \end{algorithmic}
  117. \end{algorithm}
  118.  
  119. % ---> Algorithm 01: continuation.
  120. \begin{algorithm}
  121.     \begin{algorithmic}
  122.         \algrestore{bkbreak}
  123.                         \EndIf
  124.                     \EndIf
  125.                 \EndFor
  126.             \EndFor
  127.             \ForAll{condition}
  128.             \EndFor
  129.         \EndProcedure
  130.     \end{algorithmic}
  131. \end{algorithm}
  132.  
  133.  
  134. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement