问题描述
博主今天在用overleaf写论文,急需插入伪代码,将查阅结果记录在此以便后续参考。
解决方案
overleaf有专门的说明文档来教大家如何正确插入伪代码:
https://www.overleaf.com/learn/latex/Algorithms
列举几种样式与其对应的latex源码。
样式1
![](https://img-blog.csdnimg.cn/4d9d81998a694b9e8be430c6349e31a3.png)
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
![](https://img-blog.csdnimg.cn/513b16b00ffe400793f304700c5106a9.png)
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
![](https://img-blog.csdnimg.cn/b0056f4fe8e54a49a396f4d820782773.png)
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
![](https://img-blog.csdnimg.cn/img_convert/f0348e86564cf48db0e31be3966a81cd.jpeg)
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
![](https://img-blog.csdnimg.cn/img_convert/a5e2f3b4f37a5c357202acefbc0500eb.jpeg)
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}
注意:如果伪代码每行前面一直出现编号,可以检查是不是添加了额外的跟伪代码相关的包,一般来说删除之后便可解决问题。
更多解释和代码示例见上面列出的官方文档链接地址和微信公众号。
祝各位友友们写论文愉快~
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)