gpt4 book ai didi

latex - 在宏内使用逐字环境

转载 作者:行者123 更新时间:2023-12-02 09:00:21 27 4
gpt4 key购买 nike

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

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

我能否以某种方式避免双反斜杠 (\),同时仍然获得正确的代码格式?

最佳答案

首先,您应该定义主要宏

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

关于latex - 在宏内使用逐字环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1679671/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com