TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using \lstinline I can include inline code snippets in a paragraph. I would like to provide a macro that selects formatting, etc, for lstinline so I can just do, say, \codeinline{snippet}.

I had implemented this as a short inline, like this:


So I could then write ~snippet~, and that works fine. However, If I need to use ~ in the snippet it doesn't work. The same applies if I change the short inline character to something else. So, I thought of just having a well defined macro, something like:


Which also works, except when certain characters are in the snippet. For example, this

\codeinline{example-command ~/myfile}

does not work because of the ~. I found a similar question (which inspired the above \codeinline macro) but I don't want to have to escape characters in the snippet, I just want it treated verbatim in the same way as lstinline does. I hope that it is possible because lstinline does it.

It is possible to write a wrapper around lstinline that works as well as lstinline works ?

share|improve this question
Try not to read in the argument: \newcommand\codeinline{\lstinline[style=inlinecode]} – clemens Mar 1 '13 at 13:14
@cgneider yes, that works. thank you. I'll accept that if you add it as an answer. I'm still learning the nuances of tex. – starfry Mar 1 '13 at 13:56
up vote 9 down vote accepted

You need to avoid to read in the argument with the wrapper macro as this would fix the category codes of the argument and \lstinline cannot do its magic anymore. So, here you go:


This way you can also use the syntax with delimiters:


This works since a macro that has no argument simply is replaced by its definition. So




which works as intended.

share|improve this answer
thank you for the explanation, that is very clear. – starfry Mar 1 '13 at 23:09
I would like something similar, but which would enclose the code snippet in quotes. There's no solution, is there? – akim Nov 20 '15 at 8:55
@akim With the xparse package you can try \NewDocumentCommand\mycode{v}{``\lstinline{#1}''} – I don't know how robust that is, though – clemens Nov 20 '15 at 10:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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