内联代码的新命令

内联代码的新命令

我正在使用该listings包将大量代码输入到我的文档中,但是我试图编写一个使用我当前设置的自定义命令来执行内联代码。例如,我希望能够说出类似以下内容:

Python要创建一个接受两个参数的新函数,您可以使用

\inlinecode{def myFunction(x, y):}

编辑:这是我的 MWE:

\documentclass{article}
\usepackage{listings,lstautogobble}
\usepackage[usenames,dvipsnames]{color}
\newcommand{\inlinecode}[1]{\begin{lstlisting}#1\end{lstlisting}}

% Custom Python Syntax
\lstset
{
    basicstyle=\small\ttfamily,
    commentstyle=\color{Green},
    keywordstyle=\color{Cerulean},
    frame=L,
    language=python,
    morekeywords={True, False},
    numbers=left,
    numbersep=10pt,
    numberstyle=\footnotesize\color{Gray},
    showstringspaces=false,
    stringstyle=\color{Mulberry},
    tabsize=3,
}

% Color Numbers
\lstset
{
    literate=%
    {0}{{{\color{Orange}0}}}1
    {1}{{{\color{Orange}1}}}1
    {2}{{{\color{Orange}2}}}1
    {3}{{{\color{Orange}3}}}1
    {4}{{{\color{Orange}4}}}1
    {5}{{{\color{Orange}5}}}1
    {6}{{{\color{Orange}6}}}1
    {7}{{{\color{Orange}7}}}1
    {8}{{{\color{Orange}8}}}1
    {9}{{{\color{Orange}9}}}1
}

\begin{document}
    To create a new function in Python that took two arguments you would use \inlinecode{def myFunction(x, y):}
\end{document}

我的新命令是:

\newcommand{\inlinecode}[1]{\begin{lstlisting}#1\end{lstlisting}}

但我一直收到这个错误:

控制台输出

我几乎没有编写自定义命令的经验,所以我认为这就是我的问题所在。

答案1

\lstinline[option]{<code}因此使用 用于输入内联代码的宏\newcommand{\inlinecode}[1]{\lstinline{#1}}

\documentclass{article}
\usepackage{listings,lstautogobble}
\usepackage[usenames,dvipsnames]{color}
\newcommand{\inlinecode}[1]{\lstinline{#1}}

% Custom Python Syntax
\lstset
{
    basicstyle=\small\ttfamily,
    commentstyle=\color{Green},
    keywordstyle=\color{Cerulean},
    frame=L,
    language=python,
    morekeywords={True, False},
    numbers=left,
    numbersep=10pt,
    numberstyle=\footnotesize\color{Gray},
    showstringspaces=false,
    stringstyle=\color{Mulberry},
    tabsize=3,
}

% Color Numbers
\lstset
{
    literate=%
    {0}{{{\color{Orange}0}}}1
    {1}{{{\color{Orange}1}}}1
    {2}{{{\color{Orange}2}}}1
    {3}{{{\color{Orange}3}}}1
    {4}{{{\color{Orange}4}}}1
    {5}{{{\color{Orange}5}}}1
    {6}{{{\color{Orange}6}}}1
    {7}{{{\color{Orange}7}}}1
    {8}{{{\color{Orange}8}}}1
    {9}{{{\color{Orange}9}}}1
}

\begin{document}
    To create a new function in Python that took two arguments you would use \inlinecode{def myFunction(x, y):}
\end{document}

在此处输入图片描述

但为什么不直接使用呢\lstinline?或者你可以简单地\newcommand{\inlinecode}{\lstinline}按照 Werner 的建议去做。

答案2

在此处输入图片描述

\documentclass{article}
\usepackage{listings,lstautogobble}
\usepackage[usenames,dvipsnames]{color}
\newcommand{\inlinecode}{\lstinline[basicstyle=\ttfamily\normalsize, prebreak=]}

% Custom Python Syntax
\lstset
{
    basicstyle=\small\ttfamily,
    commentstyle=\color{Green},
    keywordstyle=\color{Cerulean},
    frame=L,
    language=python,
    morekeywords={True, False},
    numbers=left,
    numbersep=10pt,
    numberstyle=\footnotesize\color{Gray},
    showstringspaces=false,
    stringstyle=\color{Mulberry},
    tabsize=3,
}

% Color Numbers
\lstset
{
    literate=%
    {0}{{{\color{Orange}0}}}1
    {1}{{{\color{Orange}1}}}1
    {2}{{{\color{Orange}2}}}1
    {3}{{{\color{Orange}3}}}1
    {4}{{{\color{Orange}4}}}1
    {5}{{{\color{Orange}5}}}1
    {6}{{{\color{Orange}6}}}1
    {7}{{{\color{Orange}7}}}1
    {8}{{{\color{Orange}8}}}1
    {9}{{{\color{Orange}9}}}1
}

\begin{document}
    To create a new function in Python that took two arguments you would use \inlinecode{def myFunction(x, y):}
\end{document}

相关内容