8

I happen to write the following code over and over again.

{\color{blue}\verb|>|} {\footnotesize\verb|HI|} 

So, I came up with the following code to make it a command.

\newcommand{\result}[2]{%
{\color{#1}\verb|>|} {\footnotesize\verb|#2|} 
}

The problem is that the result is not what I expected, there are | characters attached.

What might be wrong? I need to have the verbatim environment kept as the result can contain some characters something like $, \ ...

\documentclass{article}

\newcommand{\result}[2]{%
{\color{#1}$>$} {\footnotesize \verb|#2|} 
}

\usepackage{color}

\begin{document}
{\color{blue}\verb|>|} {\footnotesize\verb|HI|} 

\result{red}{HI}

\end{document}

ADDED

  • For the original source, \verb|>| should have been $>$. I updated the source example.
  • Following Willie's answer, I modified the command

    \newcommand{\result}[2]{{\color{#1}$>$} {\footnotesize\texttt{#2}}}

It seems fine, but with multicols environment, the footnotesize font, gives some blank spaces before its printing of characters. I attach the screen capture and source code.

It seems that line break causes this problem, by adding \mbox inside the \texttt, I could remove the blank.

\newcommand{\result}[2]{{\color{#1}$>$} {\footnotesize\texttt{\mbox{#2}}}}

enter image description here

\documentclass{article}
\usepackage{multicol}
\usepackage{tikz}

\newcommand{\result}[2]{{\color{#1}$>$} {\footnotesize\texttt{\mbox{#2}}}}
\newcommand{\resultp}[2]{{\color{#1}$>$} {\small\texttt{\mbox{#2}}}}
\usepackage{color}

\begin{document}

\begin{multicols*}{2}
\noindent\result{green}{Welcome to my project! These are your args: (a b c)}\\ \result{green}{hello}\\ \result{green}{HI my\string_name}
\vskip 2ex
\noindent\resultp{green}{Welcome to my project! These are your args: (a b c)}\\ \resultp{green}{hello}\\ \resultp{green}{HI}

\end{multicols*}
\end{document}
3
  • 1
    Your code won't compile for me: ! LaTeX Error: \verb illegal in command argument. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.12 \result{red}{HI}
    – vanden
    Commented Aug 3, 2010 at 23:36
  • do you still have a question here, or have you resolved it? Commented Aug 4, 2010 at 3:18
  • @Michael : Willie gave me enough hints to solve my issue, and I updated my post by answering. Thanks.
    – prosseek
    Commented Aug 4, 2010 at 3:33

4 Answers 4

4

I am surprised that it even compiled for you (your test example). On my installation

This is pdfTeX, Version
3.1415926-1.40.10 (Web2C 2009) entering extended mode 
(./test.tex 
LaTeX2e <2009/09/24> 
Babel <v3.8l> and hyphenation patterns for english, usenglishmax, dumylang, noh yphenation, pinyin, bulgarian, russian, ukrainian, basque, french, loaded. 
(/usr/share/texmf-dist/tex/latex/base/article.cls 
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class 
(/usr/share/texmf-dist/tex/latex/base/size10.clo)) 
(/usr/share/texmf-dist/tex/latex/graphics/color.sty 
(/usr/share/texmf-dist/tex/latex/latexconfig/color.cfg) 
(/usr/share/texmf-dist/tex/latex/graphics/dvips.def) 
(/usr/share/texmf-dist/tex/latex/graphics/dvipsnam.def)) (./test.aux)

! LaTeX Error: \verb illegal in command argument.

See the LaTeX manual or LaTeX Companion for explanation. Type  
H <return>  for immediate help.  ...    

                                                   l.12 \result{red}{HI}

The reason is that \verb is a very fragile command. The suggestion from the TeX faq I linked to is to replace \verb by \texttt, and when you type arguments to the \result as you defined above, use \string to escape the problematic characters. See also 'the string command'.

6
  • 1
    Thanks for the answer, and sorry for the trouble. I elaborated the question more, could you check that? And for the \string command, there doesn't seem to be a lot of example. Could you give some more hints?
    – prosseek
    Commented Aug 4, 2010 at 1:24
  • Did you look at the Link that I posted? The \string command converts the one character that follows it into a string. So you can type \string_ to get the underscore character, or \string{ to get the open brace character etc. In your revised example you are almost surely using the command wrong. Commented Aug 4, 2010 at 1:28
  • I see, I missed the link, and I updated the example.
    – prosseek
    Commented Aug 4, 2010 at 2:06
  • Ah, you missed the link. That's alright. I hope for your sake you didn't try to Google "LaTeX string". It is one of those seemingly harmless combinations of words... Commented Aug 4, 2010 at 9:17
  • I happen to google "LaTeX string", and I saw a bunch of nasty pictures.
    – prosseek
    Commented Aug 4, 2010 at 16:33
4

You could also define your own \verb-like command using xparse:

\documentclass{article}
\usepackage{xparse,xcolor}

\NewDocumentCommand\myvrb{O{blue}v}{%
  \textcolor{#1}{\(>\)} \texttt{\footnotesize#2}}

\begin{document}

\myvrb|HI|

\myvrb[red]|HI|

\myvrb|&$#|

\end{document}
2

With a modicum of coercion, the verbatim package can be made to almost do what you want. The "almost" is because you have to use an environment instead of a command.

\documentclass{article}

\usepackage{verbatim}
\usepackage{xcolor}

\makeatletter
\newenvironment{result}[1]{%
\def\verbatim@processline{\the\verbatim@line\ }%
\def\@verbatim{\the\every@verbatim
  \obeylines
  \let\do\@makeother \dospecials
  \verbatim@font
}%
\def\endverbatim{\endgroup}%
\noindent
{\color{#1}\texttt{>}}
\footnotesize
\verbatim}{\endverbatim}
\makeatother

\begin{document}

Here are your results:

\begin{result}{red}
greetings
\end{result}

\begin{result}{green}
A load of *@&$!)%*#$
\end{result}

\end{document}

In short, I looked a the definitions in the verbatim package and stripped out everything that looked as though it might insert a newline, thus making the verbatim environment work on one line instead of several and meaning that it doesn't start or end with new lines. Then I put this inside a new environment so that the old \begin{verbatim} ... \end{verbatim} construct still works as it should.

Caveat: Anything extra on the line \end{result} gets discarded (LaTeX warns about this). So if you want to take out the \noindent from the result environment and have the input more like in the example in the question, the code will have to be:

\noindent\begin{result}{red}
greetings
\end{result}
\\
\begin{result}{green}
A load of *@&$!)%*#$
\end{result}
1

A flexible implementation with Martin Scharrer’s newverbs:

\documentclass{article}
\usepackage{newverbs}
\usepackage{xcolor}

\makeatletter
\newverbcommand{\colorverb@verb}{}{\endgroup}
\newcommand{\colorverb}[2][]{%
  \leavevmode\begingroup
  \if\relax\detokenize{#1}\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\color{#2}}%
  {\color[#1]{#2}}%
  \colorverb@verb
}
\makeatother

\begin{document}

\colorverb{green}|abc{\}|

\colorverb{red!20}?abc|}?

\colorverb[HTML]{A021C2}|abc{\}|

\end{document}

enter image description here

With expl3; loading xcolor is optional if you need to use its user-level facilities such as \definecolor or \colorlet.

\documentclass{article}

\ExplSyntaxOn

\keys_define:nn { prosseek/flexiverb }
 {
  color .tl_set:N = \l__prosseek_flexiverb_color_tl,
  model .tl_set:N = \l__prosseek_flexiverb_model_tl,
  font .tl_set:N = \l__prosseek_flexiverb_font_tl,
 }

\NewDocumentCommand{\flexiverb}{O{}v}
 {
  \mbox
   {
    \keys_set:nn { prosseek/flexiverb } { #1 }
    \tl_if_empty:VTF \l__prosseek_flexiverb_model_tl
     {
      \color_select:V \l__prosseek_flexiverb_color_tl
     }
     {
      \color_select:VV \l__prosseek_flexiverb_model_tl \l__prosseek_flexiverb_color_tl
     }
    \use:c { verbatim@font }
    \l__prosseek_flexiverb_font_tl
    #2
   }
 }
\cs_generate_variant:Nn \color_select:n { V }
\cs_generate_variant:Nn \color_select:nn { VV }

\ExplSyntaxOff

% how to define a command based on \flexiverb
\NewDocumentCommand{\redverb}{}{\flexiverb[color=red!80]}

\begin{document}

\flexiverb[color=red!80]|abc}|

\redverb|abc}|

\flexiverb[color=A021C2,model=HTML,font=\footnotesize]|abc{\}|

\end{document}

enter image description here

You must log in to answer this question.

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