📄 code.tex
字号:
\renewcommand{\lstlistlistingname}{源代码目录}
\renewcommand{\lstlistingname}{源代码}
\lstlistoflistings
\lstset{language=Matlab}
\lstset{xleftmargin=3em,xrightmargin=1em}
\lstset{frame=TBLR}
\lstset{commentstyle=\color{blue},keywordstyle=\color{red},caption=\lstname,breaklines=true,columns=flexible,mathescape=true}
\lstset{framexleftmargin=2em,framexrightmargin=1em,framextopmargin=1em,framexbottommargin=1em}
\lstdefinestyle{numbers}{numbers=left,stepnumber=1,numberstyle=\small,numbersep=1em}
\chapter{源代码宏包使用示例}
本章使用的是 \texttt{listings} 宏包。版本为
1.0(2002年10月)。如果您的不是,\textbf{可能}无法编译,
可以下载最新红包或不要编译本章内容。如果你要在其他章节使用本宏包,可以将本章导言区的内容拷贝过去。
\lstset{language=Matlab}
\begin{lstlisting}[style=numbers,caption={A Matlab Code},label={forMatlab}]
function r = sfunchk(name)
e = exist(name, 'file');
if e == 2
%disp('We''re dealing with an m-function here')
r = 'ttkernelMATLAB';
elseif e == 3
%disp('We''re dealing with a mex-function here')
r = name;
else
error(['No m-function or mex-function named ''' name ''' found!'])
end
\end{lstlisting}
\newpage
\lstset{language=C++}
\begin{lstlisting}[style=numbers,caption=A C++ Code]
#define S_FUNCTION_NAME controller_init
#include "ttkernel.cpp"
// Event-driven controller node
class PD_Data { public:
// ctrl params
double K, Td, N, h, ad, bd;
// ctrl states
double yold, Dold, u;
};
// controller code function
double ctrl_code(int seg, void *data) {
double *m;
double r, y, P, D;
PD_Data* d = (PD_Data*) data;
switch(seg) {
case 1:
ttWait("packet");
return 0.0;
case 2:
$\cdots\cdots\cdots\cdots\cdots\cdots$
case 3:
m = new double;
*m = d->u;
ttSendMsg(1, 2, m, 10); // Send 10 bytes to node 2 (actuator) on network 1
ttSetNextSegment(1); // loop and wait for new packet
return 0.0;
}
}
// interfering task code function
double dummy_code(int seg, void *data) {
$\cdots\cdots\cdots\cdots\cdots\cdots$
}
double msgRcvhandler(int seg, void *data) {
$\cdots\cdots\cdots\cdots\cdots\cdots$
}
void init() {$\cdots\cdots\cdots\cdots\cdots\cdots$}
\end{lstlisting}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -