I'm trying to define a new command that puts text in a coloured background, reproduces it exactly how it's defined in the .tex file (in a similar way to verbatim), and won't force me to use complicated math notation in the .tex file either. The latter point is the complicated part. This is what I have so far:


 \item \terminalCommand{# SUDO_EDITOR="/usr/bin/vim -p -X" visudo} %Generates missing $ errors
 \item \terminalCommand{\# SUDO\_EDITOR="/usr/bin/vim -p -X" visudo} %Doesn't generate errors, but text is garbled and doesn't copy properly

The first \terminalCommand generates a slew of inserted missing $ errors, which stem from the _, among others. The second \terminalCommand does not generate any errors, but the underscore doesn't show up in a copy and the spacing in the PDF output is a bit off, which makes it impossible to copy into a shell.

Is there a general way I could alter this command so that it reproduces the text within it in a similar fashion to verbatim? I've been using this code for larger blocks of shell commands, but I'm looking for something inline now:




  # SUDO_EDITOR="/usr/bin/vim -p -X" visudo

I pursued a solution using \verb, but the logical way of structuring the command (\newcommand{\terminalCommand}[1]{\colorbox{customblue}{\verb= #1 =}}) returns errors of \verb illegal in command argument


1 Answer 1


You can use \lstinline along with the solution form How to redefine \lstinline to automatically highlight or draw frames around all inline code snippets? to add color to inline code snippets

So, the following code

   \item \lstinline{# SUDO_EDITOR="/usr/bin/vim -p -X" visudo} 
   \item \lstinline{\# SUDO\_EDITOR="/usr/bin/vim -p -X" visudo} 


enter image description here

Known Issues:

  • Highlighting does not span line breaks correctly.

You can also define a special character to be the delimiter to use instead of \lstinline{}, so the following yields results identical to above image.


   \item |# SUDO_EDITOR="/usr/bin/vim -p -X" visudo| 
   \item |\# SUDO\_EDITOR="/usr/bin/vim -p -X" visudo| 




% change this to customize the appearance of the highlight
\tikzstyle{highlighter} = [
  line width = \baselineskip,

% enable these two lines for a more human-looking highlight
%\tikzstyle{highlighter} += [decorate, decoration = random steps]

% implementation of the core highlighting logic; do not change!
\newcommand{\tikzhighlightanchor}[1]{\ensuremath{\vcenter{\hbox{\tikz[remember picture, overlay]{\coordinate (#1 highlight \arabic{highlight});}}}}}
\AtBeginShipout{\AtBeginShipoutUpperLeft{\ifthenelse{\value{highlight} > 0}{\tikz[remember picture, overlay]{\foreach \stroke in {1,...,\arabic{highlight}} \draw[highlighter] (begin highlight \stroke) -- (end highlight \stroke);}}{}}}

\makeatletter %   Redefine macros from listings package:

    \leavevmode\bgroup\toggletrue{@InInlineListing}\bh % \hbox\bgroup --> \bgroup
      \@ifnextchar\bgroup{\afterassignment\lst@InlineG \let\@let@token}%



   \item \lstinline{# SUDO_EDITOR="/usr/bin/vim -p -X" visudo} 
   \item \lstinline{\# SUDO\_EDITOR="/usr/bin/vim -p -X" visudo} 
  • Does your second solution have the same issue with line breaks as the first? I'm not concerned with line breaks, since these commands will never span multiple lines, but I'm curious. Also, maybe it's a nuance of evince, but when I copy the text from the PDF file, the underscore still does not appear. May 26, 2012 at 22:02
  • @pythonscript: If you want thigh highlighting then the second solution will still have issues if there are lines breaks. The copying from PDF is a separate problem and if the other questions on this site don't help, please post a separate question. May 26, 2012 at 23:21
  • Come to think of it, I've had the copying problem before with PDF's on several systems, so I'll bother with that issue later. Thank for the solution, though. It works perfectly for what I need. May 27, 2012 at 12:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .