有人知道这是为什么吗?我尝试将文档类别从“Article”更改为“Beamer”,还尝试删除一堆包,但没有任何内容使 Matlab 编辑器样式上的编号出现。我从文档中复制粘贴了示例,但除了编号之外的所有内容都出现了。有人知道这是为什么吗?谢谢
示例(修订、精简的尝试):
\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{matlab-prettifier}
\begin{document}
\maketitle
\section*{Problem 1}
We verify the $LU$ decomposition of $T$ by following the $LU$ decomposition algorithm used from Homework 2, onward. This process is iterative, so it is helpful to identify the elements of $L$ by the stage of their iteration.
The algorithm goes like this:\\
\begin{lstlisting}[
style=Matlab-editor,
basicstyle=\mlttfamily,
escapechar=`,
]
function [L,U] = GE(A)
%The square matrix A has LU decomposition
[n,n] = size(A);
for k = 1:n-1
A(k+1:n,k) = A(k+1:n,k)/A(k,k)
%each element underneath the k'th diagonal element is
%divided by said element.
A(k+1:n,k+1:n) = A(k+1:n,k+1:n) - A(k+1:n,k)*A(k,k+1:n)
%The elements of the lower-right submatrix of dimension
%(n-k-1)x(n-k-1) are updated. See figures in the
%explanation below.
end
L = eye(n,n) + tril(A,-1);
U = triu(A);
\end{lstlisting}
\end{document}
答案1
看来你需要
\usepackage[numbered]{matlab-prettifier}
启用编号,默认情况下不启用。(您也可以添加numbers=left
到选项中lstlisting
)。您还需要
\usepackage[T1]{fontenc}
获取正确的字体,如手册中所述matlab-prettifier
:
\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} % you need this to get the fonts right
\usepackage[english]{babel}
\usepackage[numbered]{matlab-prettifier} % <-- add [numbered]
\begin{document}
The algorithm goes like this:
\begin{lstlisting}[
style=Matlab-editor,
basicstyle=\mlttfamily,
escapechar=`,
]
function [L,U] = GE(A)
%The square matrix A has LU decomposition
[n,n] = size(A);
for k = 1:n-1
A(k+1:n,k) = A(k+1:n,k)/A(k,k)
%each element underneath the k'th diagonal element is
%divided by said element.
A(k+1:n,k+1:n) = A(k+1:n,k+1:n) - A(k+1:n,k)*A(k,k+1:n)
%The elements of the lower-right submatrix of dimension
%(n-k-1)x(n-k-1) are updated. See figures in the
%explanation below.
end
L = eye(n,n) + tril(A,-1);
U = triu(A);
\end{lstlisting}
\end{document}