伪代码 matlab 样式的颜色

伪代码 matlab 样式的颜色

我如何才能algorithmic像这样获得颜色并更好地控制包中的间距:

在此处输入图片描述

我尝试了这个,看起来不错,但是有数字:

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

但使用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}

看上去很糟糕,就像这样:

在此处输入图片描述

答案1

这应该可以帮助您入门;可能还需要进行额外的定制。

在此处输入图片描述

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

相关内容