2

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:

enter image description here

is there a way to have

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

and have as out put

enter image description here

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

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

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

enter image description here

\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? – Andrea Leo 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 – David Carlisle Oct 29 '16 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.