I'm not quite sure how to describe this, but I want to create a custom command that treats its input as verbatim and allows some control of the output.

In pseudocode, I want:


(But that obviously doesn't work)

newverbs gets me close to what I need, but e.g.


requires \code!~! rather than \code{~}

  • The main problem is that \verb can never be used in the argument to another command. – egreg May 18 '13 at 20:57
  • @egreg Exactly - what's the workaround? – hadley May 18 '13 at 21:16
  • There's no workaround. What's wrong in typing \verb|something| instead of \code|something|? Can you make some examples of usage of your command? Maybe there's a different way to accomplish the task. – egreg May 18 '13 at 21:19
  • 1
    @egreg it's less semantic - e.g. in the future I might want to automatically syntax highlight code chunks. – hadley May 18 '13 at 21:23
  • Well, \let\code\verb will do, of course. But with the same syntax as \verb and not as the argument of another command. In general newverbs is the way to go, if you want to change formatting. – egreg May 18 '13 at 21:25

I prepared something using the listings package. You can adjust the appearance by customizing the \lstset and \lstdefinestyle commands. See the documentation for further information.

Here is some \code{verbatim} text.

The output will looks something like this

Verbatim text using lstlisting

  • 1
    And how badly does this blow up if you do things like \code{1 != 0}? – kahen May 19 '13 at 18:00
  • 1
    Feel free to suggest a better delimiting character than ! – Henri Menke May 19 '13 at 18:20
  • 1
    Note that newer versions of listings also support \lstinline{#1} which would not need the character-marker !. Also, in case if anyone is interested, I wanted to have the style like \mathnormal. You can do that via basicstyle=\usefont{OML}{cmm}{m}{it}. – Albert Jun 7 '13 at 10:06

This will give inline conversion of the argument to catcode 12 tokens, except for spaces and percents (%), which it still takes as ignore-the-rest-of-the-line-characters. Thus, it will wrap. But what it will not do is honor linefeeds in the argument. But as an inline command, you wouldn't want it to.

As Joseph Wright points out, unbalanced braces will also break this version.

This is \fauxverb{\verbatim} text.

enter image description here

  • Won't work with for example \fauxverb|%{| – Joseph Wright Jun 6 '13 at 19:03

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.