问题描述

博主今天在用overleaf写论文,急需插入伪代码,将查阅结果记录在此以便后续参考。

解决方案

overleaf有专门的说明文档来教大家如何正确插入伪代码:

https://www.overleaf.com/learn/latex/Algorithms

 列举几种样式与其对应的latex源码。

样式1

 Latex源码:

\documentclass{article}

\usepackage{algorithm}

\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}

\caption{An algorithm with caption}\label{alg:cap}

\begin{algorithmic}

\Require $n \geq 0$

\Ensure $y = x^n$

\State $y \gets 1$

\State $X \gets x$

\State $N \gets n$

\While{$N \neq 0$}

\If{$N$ is even}

\State $X \gets X \times X$

\State $N \gets \frac{N}{2}$ \Comment{This is a comment}

\ElsIf{$N$ is odd}

\State $y \gets y \times X$

\State $N \gets N - 1$

\EndIf

\EndWhile

\end{algorithmic}

\end{algorithm}

\end{document}

样式2

 Latex源码:

\documentclass{article}

\usepackage{algorithm2e}

\begin{document}

%% This declares a command \Comment

%% The argument will be surrounded by /* ... */

\SetKwComment{Comment}{/* }{ */}

\RestyleAlgo{ruled}

\begin{algorithm}

\caption{An algorithm with caption}\label{alg:two}

\KwData{$n \geq 0$}

\KwResult{$y = x^n$}

$y \gets 1$\;

$X \gets x$\;

$N \gets n$\;

\While{$N \neq 0$}{

\eIf{$N$ is even}{

$X \gets X \times X$\;

$N \gets \frac{N}{2}$ \Comment*[r]{This is a comment}

}{\If{$N$ is odd}{

$y \gets y \times X$\;

$N \gets N - 1$\;

}

}

}

\end{algorithm}

\end{document}

样式3

Latex源码:

\documentclass{article}

\usepackage{algorithm2e}

\begin{document}

%% This declares a command \Comment

%% The argument will be surrounded by /* ... */

\SetKwComment{Comment}{/* }{ */}

\begin{algorithm}

\caption{An algorithm with caption}\label{alg:two}

\KwData{$n \geq 0$}

\KwResult{$y = x^n$}

$y \gets 1$\;

$X \gets x$\;

$N \gets n$\;

\While{$N \neq 0$}{

\eIf{$N$ is even}{

$X \gets X \times X$\;

$N \gets \frac{N}{2}$ \Comment*[r]{This is a comment}

}{\If{$N$ is odd}{

$y \gets y \times X$\;

$N \gets N - 1$\;

}

}

}

\end{algorithm}

\end{document}

以下两种代码来自:

用LaTeX优雅地书写伪代码:Algorithm2e简明指南

样式4

 Latex源码:

\def\SetClass{article}

\documentclass{\SetClass}

\usepackage[ruled,linesnumbered]{algorithm2e}

\begin{document}

\begin{algorithm}

\caption{Simulation-optimization heuristic}\label{algorithm}

\KwData{current period $t$, initial inventory $I_{t-1}$, initial capital $B_{t-1}$, demand samples}

\KwResult{Optimal order quantity $Q^{\ast}_{t}$}

$r\leftarrow t$\;

$\Delta B^{\ast}\leftarrow -\infty$\;

\While{$\Delta B\leq \Delta B^{\ast}$ and $r\leq T$}{$Q\leftarrow\arg\max_{Q\geq 0}\Delta B^{Q}_{t,r}(I_{t-1},B_{t-1})$\;

$\Delta B\leftarrow \Delta B^{Q}_{t,r}(I_{t-1},B_{t-1})/(r-t+1)$\;

\If{$\Delta B\geq \Delta B^{\ast}$}{$Q^{\ast}\leftarrow Q$\;

$\Delta B^{\ast}\leftarrow \Delta B$\;}

$r\leftarrow r+1$\;}

\end{algorithm}

\end{document}

样式5

Latex源码: 

\documentclass{article}

\usepackage[linesnumbered, ruled]{algorithm2e}

\SetKwRepeat{Do}{do}{while}%

\begin{document}

\begin{algorithm}[H]

\KwData{this text}

\KwResult{how to write algorithm with \LaTeX2e }

initialization\;

\While{not at end of this document}{

read current\;

\Repeat{this end condition}{

do these things\;

}

\eIf{understand}{

go to next section\;

current section becomes this one\;

}{

go back to the beginning of current section\;

}

\Do{this end condition}{

do these things\;

}

}

\caption{How to write algorithms}

\end{algorithm}

\end{document}

注意:如果伪代码每行前面一直出现编号,可以检查是不是添加了额外的跟伪代码相关的包,一般来说删除之后便可解决问题。

更多解释和代码示例见上面列出的官方文档链接地址和微信公众号。

祝各位友友们写论文愉快~

精彩链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。