2

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

enter image description here

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:

enter image description here

HCAI
  • 3,325
  • possible duplicate: http://tex.stackexchange.com/questions/75116/inserting-matlab-code-in-the-appendix –  May 05 '13 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 05 '13 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. – Torbjørn T. May 05 '13 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. –  May 05 '13 at 10:55
  • You could always use the listing package which works great for what you are trying to accomplish. – dustin May 29 '13 at 01:57
  • 1
    Consider using the matlab-prettifier package; see this answer of mine. – jub0bs Feb 10 '14 at 13:21

1 Answers1

2

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

enter image description here

\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}%

% redefine comments
\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}
jub0bs
  • 58,916
  • 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 – Vossie Jun 10 '13 at 10:48