# Define a command based on lstlisting usable in the normal line of the text.

Is there a way in order to define a command of 1 argument (that we can call '\likelisting') that allow us to inherits some defined things with lstlisting. For istance with the code

      \begin{lstlisting}
int main(void)
\end{lstlisting}


give me this as out put:

is there a way to have

        some text \likelisting{int main (void)} some text


and have as out put

but with the exacly style of lstlisting (so with the same gray highlighted that use lstlisting?

• search for \lstinline Oct 29 '16 at 6:50

This is provided by listings's \lstinline macro. You can either use \lstinline directly, or create an alias:

\documentclass{article}

\usepackage{listings,xcolor}

\lstset{
language=c,
basicstyle=\ttfamily,
keywordstyle=\color{blue}
}
\newcommand{\likelisting}{\lstinline}

\setlength{\parindent}{0pt}% Just for this example

\begin{document}

Some text
\begin{lstlisting}
int main(void)
\end{lstlisting}

some text \likelisting{int main(void)} some text

some text \likelisting[keywordstyle={\color{red}}]{int main(void)} some text

\end{document}

• Thank you so much that is what I want. But how to have the same backgroundcolor? Why the command doesn't work in tabular? Oct 29 '16 at 7:02
• @AndreaLeo: The documentation mentions the argument-style interface for \lstinline using {..} is experimental. I would image it is fragile in general (like most verbatim-like content) and therefore may be limited in its use (like in tabular). You can use an alternative delimiter (say) \likelisting!...!.Regarding the colour: That's a completely different problem. \lstinline does not support that (mentioned in the documentation as well; section 4.2 Typesetting listings).
– Werner
Oct 29 '16 at 7:16
• the command should work in tabular. like any verb command it won't work in the argument of another command or "fake" environments like tabularx Oct 29 '16 at 9:43