1

I am trying to automatically import code written in Julia and make it into semi-pseudocode. As an minimal example I am trying to replicate the following pseudocode style:

Example Psudocode 1

Example Psudocode 2

I am guessing this was made using the algorithmx package. My best attempt at replicating this style is shown in the following image

My attempt so far

However my code has some minor / major flaws:

  • How can I make everything that is not a keyword, nor a function name into maths? It looks so much better than the ttfamily font.

  • My topline defined in lset is not as thick as the one in the example. I know where it is defined, but not how to change it's width.

  • It is not necessary to replicate the commenting style in the AlgorithmX examples.

main.tex

\documentclass[10pt]{article} 

% Defines fonts and language settnings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel} % Tilpasning til norsk.
\usepackage{mathtools}

\usepackage{subcaption} \usepackage{caption}

% Defines the style for the codeblocks
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{listings}
\usepackage{geometry}
\geometry{left=1.0in,right=1.0in,top=1.0in,bottom=1.0in }

\lstset{ frame=top,frame=bottom,
  basicstyle=\normalfont,
  stepnumber=1, % the step between two line-numbers. If it is 1 each line will be numbered
  numbersep=10pt, % how far the line-numbers are from the code
  tabsize=2, % tab size in blank spaces
  extendedchars=true, %
  breaklines=true, % sets automatic line breaking
  captionpos=t, % sets the caption-position to top
  mathescape=true,
  mathescape=true,
  literate=
         {=}{$\leftarrow{}$}{1}
         {+=}{${}\uparrow{}$}{2}
         {==}{$={}$}{2}
         {||}{OR}{2},
  showspaces=false, % Leerzeichen anzeigen ?
  showtabs=false, % Tabs anzeigen ?
  xleftmargin=17pt, framexleftmargin=17pt, framexrightmargin=17pt,
  framexbottommargin=5pt,
  framextopmargin=5pt,
  showstringspaces=false % Leerzeichen in Strings anzeigen ?
} \lstMakeShortInline[columns=fixed]|

\DeclareCaptionFormat{listing}{\rule{\dimexpr\textwidth+17pt\relax}{0.4pt}\par\vskip1pt#1#2#3}
\captionsetup[lstlisting]{format=listing,singlelinecheck=false, margin=0pt,
  font={sf},labelsep=space,labelfont=bf}

\renewcommand\lstlistingname{Algorithm}

%%
%% Julia definition (c) 2014 Jubobs
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lstdefinelanguage{Julia}%
  {morekeywords={abstract,break,case,catch,const,continue,do,else,elseif,%
      end,export,false,for,function,immutable,import,importall,if,in,%
      macro,module,otherwise,quote,return,switch,true,try,type,typealias,%
      using,while},%
   sensitive=true,%
   alsoother={$},%
   morecomment=[l]\#,%
   morecomment=[n]{\#=}{=\#},%
   morestring=[s]{"}{"},%
   morestring=[m]{'}{'},%
}[keywords,comments,strings]%

\lstset{%
    language         = Julia,
    keywordstyle     = \bfseries,
    stringstyle      = \color{magenta},
    commentstyle     = \color{ForestGreen},
    showstringspaces = false,
}

\begin{document}

\lstinputlisting[language=Julia, 
                 caption=Recursive route-counting function,
            ]{countRoutes1.jl}

\lstinputlisting[language=Julia, 
                 caption=Recursive route-counting function,
            ]{countRoutes2.jl}

\end{document}

countRoutes1.jl

function countRoutes(m, n)
    if n == 0 || m == 0
        return 1
    end
    return countRoutes(m, n - 1) + countRoutes(m - 1, n)
end

CountRoutes2.jl

Note some minor changes had to be made in order to make the pseudocode into runnable Julia code.

cache = Dict()

function countRoutes(m,n)
    if n == 0 || m == 0
        return 1
    end

    try
        return cache[(m, n)]
    end

    cache[(m, n)] = countRoutes(m, n - 1) + countRoutes(m - 1, n)
    return cache[(m, n)]
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.