0

Is it possible to use the algorithmicx package and modify the command blocks somehow that I get vertical lines for the indentation? Should this be probably done with TikZ? (The package algorithm2e can do this but I do not use it!)

A simple example I came up with is this one here:

How is it possible to remove the end tags and still have the vertical lines As soon as I add \algnotext{EndIf} the TikZ is obviously no more drawn and there is no end tag as required?

\documentclass{article}

\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{algorithm}% http://ctan.org/pkg/algorithm

\usepackage{tikz}

\begin{document}

\newcommand{\keyword}[1]{\textbf{\textsf{#1}}}
\newcounter{scopeCounter}
\newcommand{\decScopeCounter}{\addtocounter{scopeCounter}{-1}}
\newcommand{\incScopeCounter}{\addtocounter{scopeCounter}{1}}

% if blocks
\algblockdefx{If}{EndIf}%
  [1]{%
  \incScopeCounter%
  \tikz[remember picture,baseline=-0.5ex] \coordinate (b\thescopeCounter);%
  \keyword{if} #1\hspace{0.5mm}\keyword{:}%
  }%
  {%
  \tikz[remember picture,baseline=-0.5ex] \coordinate (e\thescopeCounter);%
  \begin{tikzpicture}[remember picture,overlay]%
    \draw[-,very thick]  (b\thescopeCounter) -- (e\thescopeCounter);%
  \end{tikzpicture}%
  \algorithmicend%
  }

\algcblockdefx{If}{Else}{EndIf}%
  {%
  \tikz[remember picture,baseline=-0.5ex] \coordinate (e\thescopeCounter);%
  \begin{tikzpicture}[remember picture,overlay]%
    \draw[-,very thick]  (b\thescopeCounter) -- (e\thescopeCounter);%
  \end{tikzpicture}%
  \tikz[remember picture,baseline=-0.5ex] \coordinate (b\thescopeCounter);%
  \keyword{else:}%
  }%
  {%
  \tikz[remember picture,baseline=-0.5ex] \coordinate (e\thescopeCounter);%
  \begin{tikzpicture}[remember picture,overlay]%
    \draw[-,very thick]  (b\thescopeCounter) -- (e\thescopeCounter);%
  \end{tikzpicture}%
  \algorithmicend%
  \decScopeCounter
  }%

\algcblockdefx{If}{Elif}{EndIf}%
  [1]{%
  \tikz[remember picture,baseline=-0.5ex] \coordinate (e\thescopeCounter);%
  \begin{tikzpicture}[remember picture,overlay]%
    \draw[-,very thick]  (b\thescopeCounter) -- (e\thescopeCounter);%
  \end{tikzpicture}%
  \tikz[remember picture,baseline=-0.5ex] \coordinate (b\thescopeCounter);%
  \keyword{elif} #1\hspace{0.5mm}\keyword{:}
  }%
  {%
  \tikz[remember picture,baseline=-0.5ex] \coordinate (e\thescopeCounter);%
  \begin{tikzpicture}[remember picture,overlay]%
    \draw[-,very thick]  (b\thescopeCounter) -- (e\thescopeCounter);%
  \end{tikzpicture}%
  \algorithmicend%
  \decScopeCounter
  }%

 %\algnotext{EndIf}  %%% UNCOMMENT THIS LINE TO REMOVE end TAGS

\begin{algorithm}
\caption{Euclid’s algorithm}\label{euclid}
\begin{algorithmic}[1]
\If{$i\geq maxval$}
    \State $i\gets 0$
\Else
    \If{$i+k\leq maxval$}
        \State $i\gets i+k$
    \EndIf
\EndIf
\end{algorithmic}
\end{algorithm}



\end{document}

enter image description here

Thanks a lot for the help!

LaRiFaRi
  • 43,807
Gabriel
  • 1,574
  • Please make your code compilable and as minimal as possible. Thanks. – LaRiFaRi Sep 02 '15 at 08:43
  • no duplicate since I want to use tikz :-) – Gabriel Sep 02 '15 at 09:13
  • I made the code compilable, sorry for the mistakes! – Gabriel Sep 02 '15 at 09:13
  • Oh-uh, Sorry it closed immediately. If you need it to be open please let us know. –  Sep 02 '15 at 09:14
  • If you want it to be re-opend, you will have to tell us why you refuse algorithm2e (which would be the typical approach) and why you do not want the solution from the linked post. If you want someone to do some gratis TikZ magic for you, it should be clear for what purpose. – LaRiFaRi Sep 02 '15 at 09:21

0 Answers0