New Command For Inline Code

I am using the listings package to enter large blocks of code into my document, however I am trying to write a custom command that uses my current settings to do inline code. For example I want to be able to say something like:

To create a new function in Python that took two arguments you would use

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


EDIT: Here are is my 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,
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}


and my new command is:

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


however I keep getting this error:

I have hardly any experience writing custom commands so I assume that's where my problem is.

• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
– user31729
Apr 16, 2015 at 23:18
• The verbatim content of #1 is to blame, in my point of view
– user31729
Apr 16, 2015 at 23:30

The macro for inputting inline code is \lstinline[option]{<code} Hence use \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,
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}


But why not use \lstinline directly? Or you can simply do \newcommand{\inlinecode}{\lstinline} as suggested by Werner.

• In my opinion it would be far better to use \newcommand{\inlinecode}{\lstinline}. This allows you to use \inlinecode{...} or \inlinecode!...! or \inlinecode[<opts>]{...}...
– Werner
Apr 16, 2015 at 23:31
• @Werner Yes, this is just an attempt to follow OP's way. I will add a note. Thanks
– user11232
Apr 16, 2015 at 23:33
• I think defining a custom command is fine, sometimes you want to be able to influence the way code looks, add a compat mode to your document (imagine somebody doesn't have listings package, you can define a switch to fall back to regular ttstyle, and so on). Apr 16, 2015 at 23:39

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

% Custom Python Syntax
\lstset
{
basicstyle=\small\ttfamily,
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}