如何将MATLAB代码导入LaTeX?已尝试Matlab prettifier无效求替代方案
Let me share a few tried-and-true methods I’ve used when matlab-prettifier didn’t work as expected—these should get your MATLAB code rendering properly in LaTeX:
1. Use the Native listings Package (with MATLAB Syntax Highlighting)
The core listings package has built-in support for MATLAB, so you can skip matlab-prettifier entirely if needed. Just configure it in your preamble to enable syntax highlighting:
\documentclass{article} \usepackage{listings} \usepackage{xcolor} % For custom colors % Configure MATLAB syntax highlighting \lstdefinestyle{matlab-style}{ language=Matlab, basicstyle=\ttfamily\small, keywordstyle=\color{blue}, commentstyle=\color{green!60!black}, stringstyle=\color{red}, numbers=left, numberstyle=\tiny\color{gray}, frame=single, breaklines=true } \begin{document} % Import your MATLAB file \lstinputlisting[style=matlab-style]{your-file.m} \end{document}
Double-check that your .m file path is correct (relative to your .tex file) and that LaTeX can access it—file path issues are a common culprit for failed imports.
2. Use the mcode Package (MATLAB-Specific)
The mcode package is purpose-built for typesetting MATLAB code, so it’s often more reliable for this use case. Here’s how to set it up:
\documentclass{article} \usepackage{mcode} % Load the package \begin{document} % Import your MATLAB file \lstinputlisting{your-file.m} % You can also inline small code snippets like this: \mcode{disp('Hello, LaTeX!');} \end{document}
Note: If you get package errors, make sure mcode is installed (most modern TeX distributions include it by default, but you can manually install it if needed).
3. Fallback to the verbatim Environment (No Highlighting)
If you just need to display code without syntax highlighting and want a quick fix, the verbatim environment works perfectly. It’s simple and requires no extra packages:
\documentclass{article} \begin{document} Here's your MATLAB code: \begin{verbatim} % Your MATLAB code from your-file.m function result = addNumbers(a, b) result = a + b; end \end{verbatim} % Or import directly from the file: \verbatiminput{your-file.m} \end{document}
4. Use tcolorbox with listings (Stylish, Highlighted Blocks)
For more polished, boxed code blocks with syntax highlighting, combine tcolorbox and listings. This is great for professional-looking documents:
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{listings} \usepackage{xcolor} % Configure the tcolorbox style for MATLAB \tcbset{ matlabbox/.style={ colback=white, colframe=blue!70!black, listing only, listing options={ language=Matlab, basicstyle=\ttfamily\small, keywordstyle=\color{blue}, commentstyle=\color{green!60!black}, stringstyle=\color{red} } } } \begin{document} \begin{tcolorbox}[matlabbox] \lstinputlisting{your-file.m} \end{tcolorbox} \end{document}
Quick Troubleshooting for Your Original matlab-prettifier Issue
Before switching entirely, double-check these common fixes:
- Ensure you’ve loaded
matlab-prettifiercorrectly with\usepackage{matlab-prettifier}. - Verify that your
\lstinputlistingcommand uses thestyle=Matlaboption (e.g.,\lstinputlisting[style=Matlab]{file.m}). - Check if your
.mfile uses a non-ASCII encoding—save it as UTF-8 and try again.
内容的提问来源于stack exchange,提问作者Shakeel Khan

