如何改善`lstlisting`环境中的间距?

如何改善`lstlisting`环境中的间距?

我想beamer使用发布一些 MATLAB 代码matlab-prettifier。我想使用 选项为代码创建主标题titlelstlisting但是,结果如下

在此处输入图片描述

  1. 是否可以将标题和框架之间的空间设置为零?
  2. 如何减少空行之间的间距?
  3. 如何\space在每行前添加一个单数?
  4. 我希望直接复制并粘贴幻灯片上的代码。如何添加行号作为不可复制的材料?

我的MWE

\documentclass[aspectratio=169, xcolor={x11names}, t]{beamer}
\usefonttheme{professionalfonts}
\renewcommand{\sfdefault}{ppl}
\BeforeBeginEnvironment{frame}{\fontencoding{T1}\selectfont}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} % you need this to get the fonts right

    \usepackage[]{matlab-prettifier}
        
        % MATLAB Prettifying Options
        
            % style
            \lstset{style=Matlab-editor, basicstyle=\mlttfamily\footnotesize, escapechar=", mathescape=true}
                        
            % frames
            \lstset{frame=lines, framerule=0.61pt, rulecolor=\color{DodgerBlue3}, backgroundcolor=\color{Ivory1}}
                    
            % spacing
            \lstset{aboveskip=0pt, belowskip=\medskipamount, framesep=0pt}
                
                
                    
            % section rules
            \lstset{mlshowsectionrules=true, mlsectionrulethickness=0.08, mlsectionrulecolor=black!25}
        
            % indentation
            \lstset{literate=*{\    }{\ }3, breaklines=true, breakindent=0mm}
                
            % numbering
            \lstset{numbers=right, numberstyle=\tiny\color{black!80!white!20}, stepnumber=1, numbersep=5pt, numberblanklines=false}

\usepackage{tikz}
\newcommand{\titleMATLABCode}[1]{%
\begin{tikzpicture}%
    \node[fill=CadetBlue2, text=black, inner sep=0mm, outer sep=0mm, inner ysep=1mm, text width=\linewidth, align=flush left, font=\normalsize\bfseries] {~#1};%
\end{tikzpicture}%
}

\begin{document}

\begin{frame}[fragile, environment=frame, plain, allowframebreaks=0.99]
    
    The algorithm goes like this:
    \begin{lstlisting}[title=\titleMATLABCode{Try It in MATLAB}]
% define symbolic variables
syms y(t) x t
Dy = diff(y, 't', 1);
D2y = diff(y, 't', 2);

% the differential equation and initial conditions

    % the differential equation
    eq = D2y + 12*Dy + 32*y == 32;
    
    % the initial conditions
    cond1 = y(0) == 0; cond2 = Dy(0) == 0;
    conds = [cond1, cond2];

% the solution
y_t_sol = dsolve(eq, conds);
pretty(y_t_sol)
    \end{lstlisting}
\end{frame}

\end{document}

答案1

针对问题 1.、2 以及也许 4 的答案。

\documentclass[aspectratio=169, xcolor={x11names}, t]{beamer}
\usefonttheme{professionalfonts}
\renewcommand{\sfdefault}{ppl}
\BeforeBeginEnvironment{frame}{\fontencoding{T1}\selectfont}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} % you need this to get the fonts right

\usepackage[]{matlab-prettifier}

% MATLAB Prettifying Options

% style
% \lstset{style=Matlab-editor, basicstyle=\mlttfamily\footnotesize, escapechar=", mathescape=true}
\lstset{style=Matlab-editor, basicstyle=\linespread{0.8}\mlttfamily\footnotesize, escapechar=", mathescape=true}
% https://tex.stackexchange.com/questions/129377/unwanted-white-lines-in-listings-environment/129413#129413

% frames
\lstset{frame=lines, framerule=0.61pt, rulecolor=\color{DodgerBlue3}, backgroundcolor=\color{Ivory1}}

% spacing
\lstset{aboveskip=0pt, belowskip=\medskipamount, framesep=0pt}

% section rules
\lstset{mlshowsectionrules=true, mlsectionrulethickness=0.08, mlsectionrulecolor=black!25}

% indentation
\lstset{literate=*{\    }{\ }3, breaklines=true, breakindent=0mm}

% numbering
%\lstset{numbers=right, numberstyle=\tiny\color{black!80!white!20}, stepnumber=1, numbersep=5pt, numberblanklines=false}

\usepackage{tikz}
\newcommand{\titleMATLABCode}[1]{%
  \begin{tikzpicture}%
    \node[fill=CadetBlue2, text=black, inner sep=0mm, outer sep=0mm, inner ysep=1mm, text width=\linewidth, align=flush left, font=\normalsize\bfseries] {~#1};%
  \end{tikzpicture}%
}

\begin{document}

\begin{frame}[fragile, environment=frame, plain, allowframebreaks=0.99]
  \frametitle{ The algorithm goes like this: } 
  % The algorithm goes like this:
  \begin{lstlisting}[title=\titleMATLABCode{Try It in MATLAB}]
    % define symbolic variables
    syms y(t) x t      
    Dy = diff(y, 't', 1);
    D2y = diff(y, 't', 2);

    % the differential equation and initial conditions

    % the differential equation
    eq = D2y + 12*Dy + 32*y == 32;
    
    % the initial conditions
    cond1 = y(0) == 0; cond2 = Dy(0) == 0;
    conds = [cond1, cond2];

    % the solution
    y_t_sol = dsolve(eq, conds);
    pretty(y_t_sol)
  \end{lstlisting}
\end{frame}

\end{document}

在此处输入图片描述

相关内容