我使用了此处的 listingcode 示例我可以用什么来在我的文档中排版 MATLAB 代码? 将 matlab 代码文件直接加载到 LaTeX 中,但我使用不同的文件目录,如下所示:
\documentclass[11pt,a4paper]{article}
\usepackage{listing}
\def\matlab{D:/Dokumente/MATLAB/model/}
\definecolor{MLgreen}{RGB}{28,172,0} % color values Red, Green, Blue
\definecolor{MLlilas}{RGB}{170,55,241}
\begin{document}
\import{./}{Appendix.tex}
\end{document}
% !TEX root = Mainfile.tex
\appendix
\lstset{language=Matlab,%
%basicstyle=\color{red},
breaklines=true,%
morekeywords={matlab2tikz},
keywordstyle=\color{blue},%
morekeywords=[2]{1}, keywordstyle=[2]{\color{black}},
identifierstyle=\color{black},%
stringstyle=\color{MLlilas},
commentstyle=\color{MLgreen},%
showstringspaces=false,%without this there will be a symbol in the places where there is a space
numbers=left,%
numberstyle={\tiny \color{black}},% size of the numbers
numbersep=9pt, % this defines how far the numbers are from the text
emph=[1]{for,end,break},emphstyle=[1]\color{red}, %some words to emphasise
%emph=[2]{word1,word2}, emphstyle=[2]{style},
}
\lstinputlisting{{\matlab\myscript.m}}
但我无法运行该代码。
答案1
我自己解决了这个问题。我使用包含在主文件文档中的 Tex 子文件,其工作原理如下:主文件:
\providecommand{\pgfsyspdfmark}[3]{}
\documentclass[11pt,a4paper]{article}
\usepackage{listings}
\usepackage{color}
%define different working directory
\def\matlab{D:/Dokumente/MATLAB/model/}
\definecolor{MLgreen}{RGB}{28,172,0} % color values Red, Green, Blue
\definecolor{MLlilas}{RGB}{170,55,241}
\begin{document}
\lstset{language=Matlab,%
%basicstyle=\color{red},
breaklines=true,%
morekeywords={matlab2tikz},
keywordstyle=\color{blue},%
morekeywords=[2]{1}, keywordstyle=[2]{\color{black}},
identifierstyle=\color{black},%
stringstyle=\color{MLlilas},
commentstyle=\color{MLgreen},%
showstringspaces=false,%without this there will be a symbol in the places where there is a space
numbers=left,%
numberstyle={\tiny \color{black}},% size of the numbers
numbersep=9pt, % this defines how far the numbers are from the text
emph=[1]{for,end,break},emphstyle=[1]\color{red}, %some words to emphasise
%emph=[2]{word1,word2}, emphstyle=[2]{style},
}
\import{./}{Appendix.tex}
\end{document}
附录文件:
% !TEX root = Mainfile.tex
\appendix
\section{Appendix}
\lstinputlisting{\matlab/myfun.m}