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 have \newcommand\dd[2]{\item[\texttt{#1}] #2} and later I have

\dd{bc}{text \verb#$(echo 5+4 | bc)# text \verb#$((5+4))# text}

and I get

! You can't use `macro parameter character #' in math mode.
<argument> ...nicht mit \verb ##$(echo 5+4 | bc)##
rechnen, sondern die neue...
l.74 ...ekten Rechnen, \verb#$((5+4))#, benutzen.}
Sorry, but I'm not programmed to handle this case;
I'll just pretend that you didn't ask for it.
If you're in the wrong mode, you might be able to
return to the right one by typing `I}' or `I$' or `I\par'.

How can I get the verbatim into my command without the command interpreting it wrongly?

share|improve this question
3  
You can't use verbatim inside a macro argument in general, because the whole argument will then already be parsed before \verb does its thing. Therefore # is still the normal macro argument character which can't be used on its own here. –  Martin Scharrer Oct 30 '11 at 12:44
    
    
Okay, thanks for the hints! –  queueoverflow Oct 30 '11 at 12:52
1  
The best thing to do is to define \dd with one argument, that is, \newcommand{\dd}[1]{\item[\texttt{#1}]}; then \dd{bc} text \verb#$(echo 5+4 | bc)# text \verb#$((5+4))# text will work as you wish. –  egreg Oct 30 '11 at 13:36
add comment

2 Answers

up vote 2 down vote accepted

I'm not sure I fully understand what you're trying to achieve, but I think the simplest solution may be something along the following model. Note that one has to "escape" the dollar sign by prefixing a backslash to it; on the other hand, it's not necessary to introduce verbatim strings explicitly.

\documentclass{article}
\newcommand\dd[2]{\item[\texttt{#1}] #2}
\begin{document}
\begin{description}
\dd{bc}{text \texttt{\$(echo 5+4 $|$ bc)} text \texttt{\$((5+4))} text}
\end{description}
\end{document}

enter image description here

share|improve this answer
add comment

As already mentioned in comment, verbatim or its short form \verb is a different beast altogether. If you must pass verbatim stuff as arguments, you could consider a fancyvrb work-around.

Using fancyvbr you can save and restore your verbatim commands in the following way:

\usepackage{fancyvrb}% http://ctan.org/pkg/fancyvrb
\DefineShortVerb{\#}% # denotes verbatim opening/closing character
\SaveVerb{VerbA}#(echo 5+4 | bc)#
\SaveVerb{VerbB}#$((5+4))#

This stores (echo 5+4 | bc) as-is in VerbA, and $((5+4)) in VerbB, which can be used (restored) later by means of \UseVerb{VerbA} and \UseVerb{VerbB} respectively. Here is a minimal working example:

enter image description here

\documentclass{article}
\usepackage{fancyvrb}% http://ctan.org/pkg/fancyvrb
\newcommand\dd[2]{\item[\texttt{#1}] #2}
\begin{document}
\DefineShortVerb{\#}% # denotes verbatim opening/closing character
\SaveVerb{VerbA}#(echo 5+4 | bc)#
\SaveVerb{VerbB}#$((5+4))#
\begin{description}
  \dd{bc}{text \UseVerb{VerbA} text \UseVerb{VerbB} text}
\end{description}
\end{document}

Edit: A similar work-around exist using the verbdef package. It provides \verbdef{<command>}{<verbatim>} that defines \<command> with verbatim <verbatim> content. The following MWE produces the same output as above:

\documentclass{article}
\usepackage{verbdef}% http://ctan.org/pkg/verbdef
\newcommand\dd[2]{\item[\texttt{#1}] #2}
\begin{document}
\verbdef\VerbA{(echo 5+4 | bc)}
\verbdef\VerbB{$((5+4))}
\begin{description}
  \dd{bc}{text \VerbA\ text \VerbB\ text}
\end{description}
\end{document}
share|improve this answer
2  
I would prefer \verbdef from the verbdef package. –  Martin Scharrer Oct 30 '11 at 20:41
    
@MartinScharrer: Great simplification. I've added it as an edit to my answer. –  Werner Oct 30 '11 at 20:47
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.