Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

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:

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.

\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}
share|improve this answer
    
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. –  Ricardo Altamirano 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. –  Peter Grill 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. –  Ricardo Altamirano May 27 '12 at 12:23
add comment

Your Answer

 
discard

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.