# Colors in Pseudocode matlab style

How do I get colours and more control over spacing in the algorithmic package like this:

I tried this which looks good, but has numbers:

\begin{enumerate}
\item Create the cumulative sum of probabilities for each care type, thus defining the interval width
\item \textcolor{blue}{for} jj=1:m \textcolor{gray}{\% where m is the number of nurses}
\item Choose care type
\item \hspace{3cm}Randomly select care length ($n$) based on care type
\item \item \textcolor{blue}{for} ii=1:n  \textcolor{gray}{\% where n is the sequence length}
\item\hspace{3cm} Generate a random number $w$
\item \hspace{3cm}Check into which cumulative probability interval $w$ falls and choose the corresponding surface category
\item update ii=ii+1
\item update jj=i+1
\item \textcolor{blue}{end}
\item \textcolor{blue}{end}
\end{enumerate}


But using algorithm

\documentclass{article}
\usepackage{color}
\usepackage{algorithmic}

\begin{document}

\begin{algorithmic}
\STATE\COMMENT{Create the cumulative sum of probabilities for each care type, thus defining the interval width}
\WHILE{jj=1:m} \% where m is the number of nurses
\STATE\COMMENT{Choose care type}
\STATE\COMMENT{Randomly select care length ($n$) based on care type}
\WHILE{ii=1:n} \% where n is the sequence length
\STATE\COMMENT{Generate a random number $w$}
\STATE\COMMENT{Check into which cumulative probability interval $w$ falls and choose the corresponding surface category}
\ENDWHILE
\ENDWHILE
\end{algorithmic}
\end{document}


Looks nasty, like this:

• possible duplicate: tex.stackexchange.com/questions/75116/…
– user13907
May 5, 2013 at 10:06
• I would like to use one of the algorithm packages because that mcode which I use elsewhere puts numbers down the side.
– HCAI
May 5, 2013 at 10:38
• mcode numbers lines of listings only if you add numbered as an option to the package. You can turn off numbering for a single listing by adding numbers=none as an optional argument to that listing, or you can have it off by default (remove numbered from the package options) and number only those listings you want to, by adding numbers=left,numberstyle=\tiny to the optional arguments of the listing. You can also use \lstset in the document to change the behaviour of all the subsequent listings. May 5, 2013 at 10:53
• @HCAI Please, see the link above. Just remove or comment the lines numbers=left, numberstyle={\tiny \color{black}}, numbersep=9pt, from the code presented as the first answer.
– user13907
May 5, 2013 at 10:55
• Consider using the matlab-prettifier package; see this answer of mine. Feb 10, 2014 at 13:21

That should get you started; additional customisation might be required.

\documentclass{article}

\usepackage{xcolor}
\colorlet{kw}{blue}
\definecolor{com}{rgb}{0,0.6,0.3}

\usepackage{algorithmicx}
\usepackage{algpseudocode}

% redefine keywords
\algrenewcommand\algorithmicfunction{\textcolor{kw}{function}}
\algrenewcommand\algorithmicwhile{\textcolor{kw}{while}}
\algrenewcommand\algorithmicfor{\textcolor{kw}{for}}
\algrenewcommand\algorithmicif{\textcolor{kw}{if}}
\algrenewcommand\algorithmicelse{\textcolor{kw}{else}}
\algrenewcommand\algorithmicend{\textcolor{kw}{end}}

% new keywords
\algnewcommand\Break{\textcolor{kw}{break}}%
\algnewcommand\Continue{\textcolor{kw}{continue}}%

% redefine loops
\algdef{SE}[WHILE]{While}{EndWhile}[1]{\algorithmicwhile\ #1}{\algorithmicend}%
\algdef{SE}[FOR]{For}{EndFor}[1]{\algorithmicfor\ #1}{\algorithmicend}%
\algdef{C}[IF]{IF}{ElsIf}[1]{\algorithmicelse\algorithmicif\ #1}%

\algrenewcommand{\algorithmiccomment}[1]{{\color{com}\%#1}}

\begin{document}

\ttfamily

\begin{algorithmic}
\State Create the cumulative sum of probabilities for each care type,
thus defining the interval width
\For{jj=1:m} \Comment{ where m is the number of nurses}
\State Choose care type
\State Randomly select care length ($n$) based on care type
\For{ii=1:n} \Comment{ where n is the sequence length}
\State Generate a random number $w$
\State Check into which cumulative probability interval $w$ falls
and choose the corresponding surface category
\State update ii=ii+1
\State update jj=i+1
\EndFor
\EndFor
\end{algorithmic}

\end{document}

• Thanks a lot Jubobs, this is really helpful. I have a small additional question: in your example, the "Check into which ... surface category" line spans two lines, and the second line is not indented. Is there a way to modify the algorithmicx to also have the second line indented to the same (or other) extent?? Thanks Jun 10, 2013 at 10:48