在宏内使用逐字环境
我乳胶我想定义一个宏,它接受三个参数:一个字符串和两个代码段。 我的第一个想法是使用逐字环境显示代码段,但这严重失败并显示错误消息
! File ended while scanning use of \@xverbatim.
相反,我想出了以下宏。
\newcommand{\definematlabfunction}[3]{
\noindent
\begin{minipage}{\textwidth}
\noindent
#1 \vspace{0.2cm} \\
Function definition: \\
\texttt{#2} \vspace{0.2cm} \\
Example usage of the function: \\
\texttt{#3} \\
\end{minipage}
}
目前我使用这个宏,如下所示。
\definematlabfunction
{Create a function which takes one input argument (a list of numbers)
and returns the last five elements of this list. If the list does not contain
five elements, the function should display a warning.}
{function res = lastFiveElements(list)}
{>> lastFiveElements(1:10) \\
ans = [6, 7, 8, 9, 10] \\
>> lastFiveElements(7:10) \\
warning}
我能否以某种方式避免双反斜杠 (\),同时仍获得正确的代码格式?
I latex i want to define a macro which takes three arguments: a string and two code segments.
My first idea were to display the code segments using the verbatim environment, but this fails horribly with the error message
! File ended while scanning use of \@xverbatim.
Instead I have come up with the following macro.
\newcommand{\definematlabfunction}[3]{
\noindent
\begin{minipage}{\textwidth}
\noindent
#1 \vspace{0.2cm} \\
Function definition: \\
\texttt{#2} \vspace{0.2cm} \\
Example usage of the function: \\
\texttt{#3} \\
\end{minipage}
}
Currently I use this macro like below.
\definematlabfunction
{Create a function which takes one input argument (a list of numbers)
and returns the last five elements of this list. If the list does not contain
five elements, the function should display a warning.}
{function res = lastFiveElements(list)}
{>> lastFiveElements(1:10) \\
ans = [6, 7, 8, 9, 10] \\
>> lastFiveElements(7:10) \\
warning}
Can I somehow avoid the double backslashes (\) while still getting the correct code formatting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您应该定义主宏
然后您可以使用
\definematlabfunctionMain
定义\definematlabfunction
现在不需要
\\
。First of all you should define your main macros
Then you can define
\definematlabfunction
using\definematlabfunctionMain
Now no
\\
is needed.听起来您需要 fancyvrb 包。
It sounds like you need the fancyvrb package.