我正在尝试自动导入用 Julia 编写的代码并将其转换为半伪代码。作为一个最小示例,我尝试复制以下伪代码样式:
我猜这是用这个algorithmx
包做的。下图是我尽力复制的这种风格
但是我的代码存在一些小/主要缺陷:
我怎样才能将所有非关键字或非函数名称的内容转化为数学?它看起来比字体好多了
ttfamily
。我在 lset 中定义的顶线没有示例中的那么粗。我知道它在哪里定义,但不知道如何更改它的宽度。
没有必要复制
AlgorithmX
示例中的注释风格。
主文本
\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
请注意,为了将伪代码转换为可运行的 Julia 代码,必须进行一些细微的更改。
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