خود شما میگید کد بعد از محیط align
استفاده میکنید؟ کد را باید در محیطهای verbatim مانند بنویسید. نمونه زیر قسمتی از پروژه درس ENGG100 من در دانشگاه Wollongong هست که در آن از بسته listings
برای حروفچینی کد MatLab استفاده کردهام:
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\definecolor{hellgelb}{rgb}{1,1,0.85}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{0,0.5,0}
\definecolor{colString}{rgb}{1,0,0}
\lstset{%
language=matlab,
float=hbp,
basicstyle=\ttfamily\small,
identifierstyle=\color{colIdentifier},
keywordstyle=\color{colKeys},
stringstyle=\color{colString},
commentstyle=\color{colComments},
backgroundcolor=\color{hellgelb},
columns=flexible,
tabsize=4,
extendedchars=true,
showspaces=false,
showstringspaces=false,
numbers=left,
numbersep=2em,
numberstyle=\tiny, %
frame=single,
captionpos=b,
xleftmargin=1em,
breaklines=true,
breakautoindent=false,
breakindent=0pt,
firstnumber=last,
morekeywords={importdata,ImportTrackData,ComputeAcceleration,
ComputeDistance,Computetheta,ComputeCartesianCoordinates,
transpose}%
}
\begin{document}
\begin{lstlisting}[firstnumber=1]
% The ImportTrackData function written by VAFA KHALIGHI
% Define the function with one input which is the name of the track
% data file (InputFile) and outputs time (t), speed in m/s (v),
% longitudinal G-force (FGt),latitudinal G-force (FGn),
% tangential acceleartion (at), and normal acceleration (an)
function [t,v,FGt,FGn,at,an] = ImportTrackData(InputFile)
%
% When we look at the file 'Track-P11-GroupA.txt', we see that it has
% the following formats:
%
% Time (Seconds): 0.00
% Ground Speed (km/h): 135.0286190
% G Force Long (G): 0.27
% G Force Lat (G): 0.1632125
%
% Time (Seconds): 0.02
% Ground Speed (km/h): 135.2315170
% G Force Long (G): 0.27
% G Force Lat (G): 0.1642262
%
% ...
% From this, we can see that : separates strings and numbers in each line
% and we should have four columns. So the importdata function, is the
% perfect choice
%
% Let's first make a variable which is the number of columns of our track
% data:
nocols=4;
%
% Next, we use the importdata function to separate the columns and then
% automatically create a cell array of strings and a numeric array with
% all the data:
%
TrackDATA=importdata(InputFile,':');
%
% The TrackDATA.data now holds the numeric array; so we first reshape the
% numeric array into four rows and automatic number of columns using the
% reshape function and then we take the transpose of the reshaped numeric
% array so that we would have automatic number of rows and four columns:
%
TrackMatrix=transpose(reshape(TrackDATA.data,nocols,[]));
%
% t is the first column of this matrix:
%
t=TrackMatrix(:,1);
%
% FGt isthethirdcolumnofthematrix:
%
FGt=TrackMatrix(:,3);
%
% FGn is thefourthcolumnofthematrix:
%
FGn=TrackMatrix(:,4);
%
% The second column of the matrix is speed in km/h but we want to have
% speed in m/s so we need to multiply the second column of the matrix
% by 1000/3600=1/3.6:
%
v=TrackMatrix(:,2)/3.6;
%
% The tangential acceleartion is obtained by multiplying longitudinal
% G-force by 9.8:
%
at=9.8*FGt;
%
% The normal acceleartion is obtained by mutiplying latitudinal G-force
% by 9.8:
%
an=9.8*FGn;
%
% End the definition of the function
end
\end{lstlisting}
\end{document}