# Typeset text as written (similar to verbatim) within a custom command?

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:

\documentclass[10pt]{article}
\usepackage{color}
\definecolor{customblue}{RGB}{235,241,245}
\newcommand{\terminalCommand}[1]{\colorbox{customblue}{#1}}

\begin{document}
\begin{itemize}
\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 \end{itemize} \end{document}  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:

\documentclass[10pt]{article}
\usepackage{color}
\usepackage{listings}

\definecolor{customblue}{RGB}{235,241,245}
\newcommand{\terminalCommand}[1]{\colorbox{customblue}{#1}}

\lstnewenvironment{terminalblock}{%
\lstset{backgroundcolor=\color{customblue},
frame=single,
framerule=0pt,
basicstyle=\ttfamily,
breaklines=true,
columns=fullflexible}}{}

\begin{document}
\begin{terminalblock}
# SUDO_EDITOR="/usr/bin/vim -p -X" visudo
\end{terminalblock}
\end{document}


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

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

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


produces:

## 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.

\lstMakeShortInline[basicstyle=\ttfamily]{|}

\begin{itemize}
\item |# SUDO_EDITOR="/usr/bin/vim -p -X" visudo|
\item |\# SUDO\_EDITOR="/usr/bin/vim -p -X" visudo|
\end{itemize}


## Code:

\documentclass[10pt]{article}
\usepackage{color}
\usepackage{listings}
\usepackage{etoolbox}
\usepackage{atbegshi,ifthen,tikz}

\definecolor{customblue}{RGB}{235,241,245}

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

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

% implementation of the core highlighting logic; do not change!
\newcounter{highlight}[page]
\newcommand{\tikzhighlightanchor}[1]{\ensuremath{\vcenter{\hbox{\tikz[remember picture, overlay]{\coordinate (#1 highlight \arabic{highlight});}}}}}
\newcommand{\bh}[0]{\stepcounter{highlight}\tikzhighlightanchor{begin}}
\newcommand{\eh}[0]{\tikzhighlightanchor{end}}
\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:
\newtoggle{@InInlineListing}%
\togglefalse{@InInlineListing}%

\renewcommand\lstinline[1][]{%
\leavevmode\bgroup\toggletrue{@InInlineListing}\bh % \hbox\bgroup --> \bgroup
\def\lst@boxpos{b}%
\lsthk@PreSet\lstset{flexiblecolumns,#1}%
\lsthk@TextStyle
\@ifnextchar\bgroup{\afterassignment\lst@InlineG \let\@let@token}%
\lstinline@}%

\def\lst@LeaveAllModes{%
\ifnum\lst@mode=\lst@nomode
\expandafter\lsthk@EndGroup\iftoggle{@InInlineListing}{\eh{}}{}%
\else
\expandafter\egroup\expandafter\lst@LeaveAllModes
\fi%
}
\makeatother

\lstset{backgroundcolor=\color{customblue},basicstyle=\ttfamily,}%

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

• 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 '12 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 '12 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 '12 at 12:23