在宏内使用逐字环境

发布于 2024-08-09 14:28:07 字数 944 浏览 3 评论 0原文

我乳胶我想定义一个宏,它接受三个参数:一个字符串和两个代码段。 我的第一个想法是使用逐字环境显示代码段,但这严重失败并显示错误消息

! 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£噩梦荏苒 2024-08-16 14:28:07

首先,您应该定义主宏

\def\definematlabfunctionMain#1#2#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}} 

然后您可以使用 \definematlabfunctionMain 定义 \definematlabfunction

\makeatletter 
\def\definematlabfunction#1{\def\tempa{#1}\begingroup 
  \let\do\@makeother \dospecials \catcode`\{=1 \catcode`\}=2 \obeylines \@vobeyspaces
  \definematlabfunctionplus}
\def\definematlabfunctionplus#1#2{\definematlabfunctionMain{\tempa}{#1}{#2}\endgroup}
\makeatother

现在不需要 \\

\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}

First of all you should define your main macros

\def\definematlabfunctionMain#1#2#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}} 

Then you can define \definematlabfunction using \definematlabfunctionMain

\makeatletter 
\def\definematlabfunction#1{\def\tempa{#1}\begingroup 
  \let\do\@makeother \dospecials \catcode`\{=1 \catcode`\}=2 \obeylines \@vobeyspaces
  \definematlabfunctionplus}
\def\definematlabfunctionplus#1#2{\definematlabfunctionMain{\tempa}{#1}{#2}\endgroup}
\makeatother

Now no \\ is needed.

\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}
几味少女 2024-08-16 14:28:07

听起来您需要 fancyvrb 包。

It sounds like you need the fancyvrb package.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文