指出遗漏某些行的最佳方式

指出遗漏某些行的最佳方式

假设我myclass在 Matlab 中编写了一个具有许多属性和方法的类,现在我想显示

  • 它最重要的属性(foobar),
  • 其最重要的方法的标题(myclassbaz)以及
  • 他们的文件和
  • 一行概括所有步骤。

哪种方式最能表明原始源代码中遗漏了一些行,就像这里的第 6、11 和 15 行一样?

电流输出

MWE 的输出

平均能量损失

\begin{filecontents*}{\jobname.m}
classdef myclass
    % MYCLASS A very good class.
    properties
        foo
        bar
        ... and many more.
    end
    methods
        function mc = myclass(foo, bar)
            % MYCLASS Construct an instance of this class.
            ... check for allowed input, accept input.
        end
        function baz(mc)
            % BAZ Does something great.
            ... print info.
        end
    end
end
\end{filecontents*}

\documentclass{beamer}
\usepackage{listings}
\lstset{
    language=matlab,
    numbers=left,
    basicstyle=\footnotesize,
    keywordstyle=\bfseries\color{blue!75!black},
    commentstyle=\color{green!50!black},
    morecomment=[l]{...},
    morekeywords={classdef,methods,properties},
    deletekeywords={bar},
}

\begin{document}
    \begin{frame}[fragile]{See my code}
        \lstinputlisting{\jobname.m}
    \end{frame}
\end{document}

答案1

一种方法是定义转义字符并使用它来突出显示省略的文本。例如,添加到lsset

escapechar={|},

然后使用

|\color{red}... check for allowed input, accept input.|

A

\begin{filecontents*}[overwrite]{\jobname.m}
    classdef myclass
    % MYCLASS A very good class.
    properties
    foo
    bar
    |\color{red}... and many more.|
    end
    methods
    function mc = myclass(foo, bar)
    % MYCLASS Construct an instance of this class.
    |\color{red}... check for allowed input, accept input.|
    end
    function baz(mc)
    % BAZ Does something great.
    ... print info.
    end
    end
    end
\end{filecontents*}

\documentclass{beamer}
\usepackage{listings}
\lstset{
    language=matlab,
    numbers=left,
    basicstyle=\footnotesize,
    keywordstyle=\bfseries\color{blue!75!black},
    commentstyle=\color{green!50!black},
    escapechar={|}, % added <<<<<<<<<<
    morekeywords={classdef,methods,properties},
    deletekeywords={bar},
}

\begin{document}
    \begin{frame}[fragile]{See my code}
        \lstinputlisting{\jobname.m}
    \end{frame}
\end{document}

您可以使用minipage在此处输入图片描述

\begin{filecontents*}[overwrite]{\jobname.m}
    classdef myclass
    % MYCLASS A very good class.
    properties
    foo
    bar
    |\color{red}... and many more.|
    end
    methods
    function mc = myclass(foo, bar)
    % MYCLASS Construct an instance of this class.
    |\begin{minipage}{\linewidth}       
    \color{red}... check for allowed input, accept input. \\
        more lines of code \\
        more lines of code\\
        more lines of code
    \end{minipage}|
    end
    function baz(mc)
    % BAZ Does something great.
    ... print info.
    end
    end
    end
\end{filecontents*}

相关内容