我有几段 Matlab 脚本,我使用下面的方法将它们放入框中。我希望能够将它们命名为列表 1 2 3 4 等。最好有一个命令可以自动为我执行此操作。我是 LaTeX 新手,所以不知道,我搜索过但发现很少。
\documentclass[a4paper,12pt]{article}
\usepackage{mdframed}
\begin{document}
\begin{mdframed}[skipabove=\topsep,skipbelow=\topsep]
\begin{verbatim}
S = 55; % Value of the underlying
...
V =
2.2147 %This is the value of our put option
\end{verbatim}
\end{mdframed}
\end{document}
答案1
如果要保留当前设置 ( mdframed
+ verbatim
),可以\captionof
使用caption
包来获取标题。另一个选择是使用listings
包(而不是使用mdframed
+ verbatim
)来编写您的列表;lstlisting
环境使您能够为您的列表提供框架和标题:
\documentclass[a4paper,12pt]{article}
\usepackage{listings}
\usepackage{mdframed}
\usepackage{caption}
\captionsetup[lstlisting]{labelsep=none}
\lstset{frame={tblr}}
\begin{document}
\begin{lstlisting}[caption={\null}]
S = 55; % Value of the underlying
...
V =
2.2147 %This is the value of our put option
\end{lstlisting}
\begin{mdframed}[skipabove=\topsep,skipbelow=\topsep]
\captionof{lstlisting}{}
\begin{verbatim}
S = 55; % Value of the underlying
...
V =
2.2147 %This is the value of our put option
\end{verbatim}
\end{mdframed}
\end{document}
如果您希望自动应用此功能,您可以用 括起来verbatim
并mdframed
使用settings
键来自动生成标题:
\documentclass[a4paper,12pt]{article}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{caption}
\captionsetup[lstlisting]{labelsep=none}
\surroundwithmdframed[
skipabove=\topsep,
skipbelow=\topsep,
settings=\captionof{lstlisting}{}
]{verbatim}
\begin{document}
\begin{verbatim}
S = 55; % Value of the underlying
...
V =
2.2147 %This is the value of our put option
\end{verbatim}
\begin{verbatim}
S = 55; % Value of the underlying
...
V =
8.2147 %This is the value of our put option
\end{verbatim}
\end{document}
答案2
我建议使用这个listings
包,使用它你既可以输入代码片段,也可以将完整(或部分)文件加载到输出中。引用单个列表也相当容易。
我尝试给你举一个小例子,希望这对你有所帮助。
\documentclass[a4paper,12pt]{article}
\usepackage{listings}
\usepackage{color}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{lstlisting}[language=Matlab,
frame=single,
caption=Some Matlab code,
label=matlab]
S = 55; % Value of the underlying
...
V = 2.2147 %This is the value of our put option
\end{lstlisting}
Here is some more text, which is then also referring to listing~\ref{matlab}, which we will explain later. There is also a second example, which is shown in listing~\ref{python}.
\lipsum[1]
\lstinputlisting[lastline=25,
language=Python,
frame=single,
caption=Python code,
label=python]
{/path/to/python/file.py}
\lipsum[1]
\end{document}