美文网首页
Latex安装mcode包展示matlab代码

Latex安装mcode包展示matlab代码

作者: 寽虎非虫003 | 来源:发表于2023-05-14 16:40 被阅读0次

参考网页

LaTeX技巧57:使用mcode宏包在LATEX制作m文件效果

下载地址

http://www.mathworks.com/matlabcentral/fileexchange/8015-m-code-latex-package
需要注册,然后Ubuntu下的火狐浏览器可能没法登陆成功。

安装

下载之后需要安装,我的办法是直接安装到texlive的安装目录下:

sudo cp -r mcode /usr/share/texlive/texmf-dist/tex/latex
sudo cp -r mcode /usr/share/texmf/tex/latex

但是单独安装以上的文件夹似乎没有用,编辑器那边还是不能调用。
然后在命令行跑一个

texhash

在关掉你的编辑器重新开,基本上就可以见效了,我的总是没见效的原因是没有重新开编辑器,对了,我的编辑器是vscode + LaTeX Workshp

然后就可以参考示例写MATLAB代码了。

官方示例

\documentclass{article}


% load package with some of the available options - you may not need this!
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}

% something NOT relevant to the usage of the package.
\usepackage{url,textcomp}
\setlength{\parindent}{0pt}
\setlength{\parskip}{18pt}
\title{\texttt{mcode.sty} Demo}
\author{Florian Knorn, \texttt{florian@knorn.org}}
% //////////////////////////////////////////////////

\begin{document}

\maketitle

\begin{center}
\begin{minipage}{.75\linewidth}
    \color{red}{\hfill\textbf{NOTE --- BEFORE YOU START}\hfill\strut}

    All that this package does is to configure the \verb|listings| package for you. If anything is not working the way you want it, refer to the \verb|listings| documentation first and / or take a look at the \verb|mcode.sty| file itself, which is well documented internally.\\
    
    The \verb|listings| documentation can be accessed either by typing \verb|texdoc listings| into a command prompt on your system, or online:\\\scriptsize\url{http://mirrors.ctan.org/macros/latex/contrib/listings/listings.pdf}
    
\end{minipage}
\end{center}

\section*{Installation of the package}

As with any other small package, just place the \verb|mcode.sty| file in the same folder as your document, or put it somewhere where \LaTeX{} can find it.  Done!

\medskip

\section*{Usage --- 3 ways}

1) This inline demo \mcode{for i=1:3, disp('cool'); end;} uses the \verb|\mcode{}| command.\footnote{Works also in footnotes: \mcodefn{for i=1:3, disp('cool'); end;}}

2) The following is a block using the \verb|lstlisting| environment.
\begin{lstlisting}
for i = 1:3
    if i >= 5 && a ~= b       % literate programming replacement
        disp('cool');           % comment with some §\mcommentfont\LaTeX in it: $\mcommentfont\pi x^2$§
    end
    [:,ind] = max(vec);
    x_last = x(1,end) - 1;
    v(end);
    really really long really really long really really long really really long really really long line % blaaaaaaaa
    ylabel('Voltage (µV)');
end
\end{lstlisting}
Note: Here, the package was loaded with the \verb|framed|, \verb|numbered|, \verb|autolinebreaks| and \verb|useliterate| options.  \textbf{Please see the top of mcode.sty for a detailed explanation of these options.}


3) Finally, you can also directly include an external m-file from somewhere on your hard drive (the very code you use in \textsc{Matlab}, if you want) using the \verb|\lstinputlisting{/SOME/PATH/FILENAME.M}| command.  If you only want to include certain lines from that file (for instance to skip a header), you can use \verb|\lstinputlisting[firstline=6, lastline=15]{/SOME/PATH/FILENAME.M}|.

\section*{FAQ}

\begin{description}
    \item[Why does {\tt delta} get replaced by $\Delta$, {\fontfamily{ptm}\selectfont\texttildelow=} by $\neq$, etc.?]
        Well, that's precisely what the \verb|useliterate| option does. If you don't want that, don't use it.
    \item[Can I get contiguous line numbers from one code block to another?]
        Yes, but you have to read the \verb|listings| documentation for that (Section~4.8 in particular).
    \item[{\tt mcode.sty} doesn't work in my document!] Well, try your (Matlab) code fragment in this demo document here to see whether there's something in it that might be causing a problem (not so likely, but possible), or if there's some conflict between the \verb|listings| package and some other package you have loaded.
    \item[Is feature XYZ possible?] Well, the \verb|listings| package might already be able to do that. Please consult its documentation (see red box at the top)!
\end{description}


\end{document}

官方效果

官方效果

相关文章

网友评论

      本文标题:Latex安装mcode包展示matlab代码

      本文链接:https://www.haomeiwen.com/subject/hrepsdtx.html