成品
加載宏包
\documentclass[UTF8,fontset=macnew]{ctexart}
\usepackage{amsmath}
\usepackage[algoruled]{algorithm2e}
\usepackage{xcolor}
\usepackage{enumitem}
老樣子,macnew只是爲了TeXStudio看到我要的字體.
排版算法有許多的宏包.其中有用製表位的tabbing,algorithm,algorithm2e.我是看到劉海洋大師推薦的algorithm2e,讀了1個晚上documentation排出來的.
當然這是我用"所見即所得的方法"排版出來的,不建議學…其實用tikz或者用box也能排版…只是強迫症有點難受…
Algorithm2e宏包
你如果在terminal(cmd)輸入texdoc Algorithm的話出來的是另外的宏包我也讀過.但是效果不大好…還是建議用Algorithm2e.
一開始在讀的時候以爲不支持中文字體.但是還是嘗試了下,還是支持的…
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
先看看doc裏面一開始寫了啥.咋寫的…Algorithms是一個浮動題環境.浮動題嗎…麻煩…所以可選參數裏面有[h]…
\caption標題…\label{吧啦啦啦}可以之後引用了.
由於我在加載宏包的時候設置了ruled所以外面會有邊框.\SetAlgoLined是能夠顯示豎直的線…具體還有好多得讀documentation…
plus: 宏包裏說了,如果你在某個環境裏想要換行的話一定要加;否則不行.\KwData{this text}裏面放入參數.\KwInput .\KwOutput也一樣…有許多關鍵詞.
\eIf{understand}{
go to next section\;
current section becomes this one\;
}
在某一個環境裏面輸入的話.出來會在關鍵詞的下面也會縮進…但是括號一定要匹配…
如何修改中文
\renewcommand{\algorithmcfname}{算法}
\SetKw{KwData}{\textcolor{red}{\textbf{變量和參數:}}}{}
注意的是你在SetKw的時候最後的{}必須要有.否則會一直出現小括號.我找了半天沒找到爲啥…就是試了試.讀了1小時多試了doc裏面的方法纔出來了…
\SetKw{argname}{format}{}
同樣你也可以自己設置關鍵詞編譯會自動識別.不過如果要換行的話要自己手動加…
最後的代碼
\documentclass[UTF8,fontset=macnew]{ctexart}
\usepackage{amsmath}
\usepackage[algoruled]{algorithm2e}
\usepackage{xcolor}
\usepackage{enumitem}
\setlist{noitemsep}
\begin{document}
\renewcommand{\algorithmcfname}{算法}
\SetKw{KwData}{\textcolor{red}{\textbf{變量和參數:}}}{}
\begin{algorithm}[htp]
%\SetAlgoLined
%\SetAlgoVlined
\SetAlgoSkip{smallskip}
\KwData{ \\ \hspace{2.5em} \small {$\mathbf{x}(n)=m+1$ 維輸入向量} \\
\hspace{5.2em} \small {$=[+1,x_1(n),x_n, \dots x_m(n)]^T$}\\
\hspace{2.7em} \small {$\mathbf{w}(n) = m+1 $ 維權值向量} \\
\hspace{5.2em} \small{$ = [b,w_1(n),w_n, \dots w_m(n)]^T$} \\
\hspace{4.5em} \small{$b =$ 偏置 }\\
\hspace{3.0em} \small {$y(n) = $ 實際響應$\left(\text{量化的} \right)$ } \\
\hspace{3.0em} \small{$d(n) = $數學期望} \\
\hspace{4.4em} \small{$\eta =$ 學習效率參數,一個比$1$小的正常數} \\
\begin{enumerate}[left= 0pt]
\item \fontsize{8pt}{1.25}初始化. 設$\mathbf{w}(0) = \mathbf{0}.$ 對時間步 $n = 1,2 \cdots$執行下列計算
\item \fontsize{8pt}{1.25}激活.在時間步$n$,通過提供連續輸入向量 $\mathbf{x}(n)$ 和期望響應 $d(n)$ 來激活感知器
\item {計算實際響應.計算感知器的實際響應:
\vspace{-1em}
\begin{center}
$y(n) = sgn[ \mathbf{w}^T(n) \mathbf{x}(n) ] $
\end{center}
\vspace{-1em}這裏sgn$\left(·\right)$ 是符號函數 }
\item {權值向量的自適應.更新感知器的權值向量:
\vspace{-0.5em}
\begin{center}
$ \mathbf{w}(n+1) = \mathbf{w}(n) + \eta[d(n) - y(n)] \mathbf{x}(n)$
\end{center}
\vspace{-1em}這裏
\[ d(n) = \begin{cases}
+1 & \text{若} \mathbf{x}(n) \text{屬於類別} \psi_1\\
-1 & \text{若} \mathbf{x}(n) \text{屬於類別} \psi_2
\end{cases}
\]
\item 繼續.時間步$n$增加1,返回第$2$步
}
\end{enumerate}
}
\caption{感知器收斂算法概述}
\end{algorithm}
\end{document}
這裏面我設置了enumerate環境的間距,很多換行也是手動的.我還得多學習學習…大佬的建議是把公式排外面,但是我是所見即所得…其實是被詬病的 …但是隻是看到華章<<神經網絡和機器學習>>裏面的算法描述很想排排看.
結語
Still on the way! 每週六儘量從考研複習中抽出點時間謝謝python和LaTeX.