我正在尝试让 Overleaf 将几行复制粘贴的 MATLAB 代码显示为常规文本。我认为代码的屏幕截图看起来并不专业,因此我正认真尝试格式化 MATLAB 代码,使其在我的 LaTeX 文档中看起来与在 MATLAB 环境中的样子相似。目前,我最关心的是防止 Overleaf 认为 MATLAB 代码的任何部分是 LaTeX 命令。我找到了缩进的解决方法,但“\textregular”命令无法防止出现错误消息和格式问题。如果可能的话,我想给某些文本字符串上色(我确信这是可行的),但这不是必需的。
请注意,我一直在寻找是否有人已经解决了这个问题,如果已经解决了,那我肯定找错了地方。但是,我花了不少时间寻找。提前感谢大家的帮助,但请不要让任何人炫耀他们的“恶心的格式化技巧”或指出我已经很明显的缺乏经验。再次感谢。
答案1
使用已在环境matlab-prettifier
中放置 MATLAB 代码的包。lstlisting
梅威瑟:
\documentclass[]{IEEEtran}
\usepackage[T1]{fontenc}
\usepackage{bigfoot}
\usepackage[]{matlab-prettifier}
% The packages above allow for printing MATLAB-like format of codes when using the command "\begin{lstlisting}"
% MATLAB Pretifying Options
\lstset{
% Style
style = Matlab-editor, % default style for matlab pretification
basicstyle = \mlttfamily, % Bera Mono font
escapechar = ", % character to escape matlab pretification mode. This value should be " because " is not allowed in MATLAB
%
% Section rulers
mlshowsectionrules = true, % horizontal rules above section titles (default: false)
mlsectionrulethickness = 0.08, % thickness of horizontal lines above section titles (default: 0.05)
mlsectionrulecolor = black!25, % color of horizontal rules above section titles (default: black!15)
}
\begin{document}
\title{Development of a Generalized PV Model in MATLAB/Simulink Using Datasheet Values}
% Authors names and emails
\author{Al-Motasem I. Aldaoudeyeh
% Information about the authors (in the margins)
\thanks{Al-Motasem I. Aldaoudeyeh is with the Department of Electrical and Computer Engineering, North Dakota State University, Fargo, ND, 58102 USA. email: [email protected]}
}
\maketitle
\appendix
A MATLAB code to find the parameters $K_{p}$ and $\beta$ is given below:
\begin{lstlisting}[]
%% Wind Turbine Data (GEV MP R 275 KW)
% Cut-in speed an rated power
v_ci = 4; P_r_WT = 275e3;
% Rotor diameter
D = 32;
% === Power speed data ===
% GCR wind speeds and their corresponding outputs
v_w_GCR = 4:12;
P_e_WT_GCR = [3 18 36 58 98 141 189 243 275]*1e3;
% The swept area
A = (D/2)^2 * pi;
%% Fitting The Parameters
% Fitting options
options =
fitoptions('Method', 'NonlinearLeastSquares', 'Lower',[0.5, 1], 'Upper',[500, 4], 'StartPoint',[50, 3]);
% === Exponential fit equation ===
ft =
fittype( '1/2 * rho_n * A * K_p * (x^beta - v_ci^beta)', 'problem', {'rho_n', 'A','v_ci'}, 'options', options);
% Do the fitting
fit_model =
fit( v_w_GCR', P_e_WT_GCR', ft, 'problem', {rho_n, A, v_ci} );
\end{lstlisting}
\end{document}