# How do define a command that takes parameters like \verb?


-
I was actually planning to add a macro which allows you to define \verb like macros with this argument separators to either the verbdef package or my own newverbs package, but didn't had the time yet. –  Martin Scharrer Jun 30 '11 at 7:13

If you do not want catcode sanitization:

\newcommand\myverb[1]%
{\def\domyverb##1#1{ the argument was ##1'}%
\domyverb}


and then use it as follows.

\myverb|test|

\myverb+test+

\myverb!test!

-

It mostly depends on what you want to do. If all you need is to use the argument "as is", then

\def\mycmd|#1|{...#1...}


will do. This doesn't check whether \mycmd is defined; if you need the check, then use

\makeatletter
\@ifdefinable\mycmd{%
\def\mycmd|#1|{...#1...}%
}
\makeatother


If you need this to do "verbatim" things, the definition must be quite more complicated.

-
what about defining a command whose argument can be placed between a pair of arbitrary symbols? How is \verb defined so that \verb|spleen|, \verb+spleen+, etc. work? –  Ian Thompson Jun 29 '11 at 23:39

Technically you need an internal macro which has the character at the end of its parameter text, as Aditya already showed in her answer. I as well assume that you only want the argument delimiter but not actual verbatim mode.

If you want to be able to define such macros a lot then you can use a new \newcommand like macro which defines your macro to use such an internal but reusable macro which after reading the argument for you calls a second macro which holds your actual code. See my example below. However, it is limited to macro with a single mandatory argument and also doesn't provide a *-version of itself.

\documentclass{article}

\makeatletter
\newcommand{\newspcommand}[1]{%
\newcommand{#1}{\@getseparg{#1}}%
\expandafter\newcommand\csname \string#1@sp\endcsname[1]%
}
% ^ This defines both \foobar and \\foobar@sp where the latter holds the actual code
\def\@getseparg#1#2{%
\def\@@getseparg##1#2{\csname \string#1@sp\endcsname{##1}}%
\@@getseparg
}
\makeatother

\newspcommand{\mycommand}{Got this stuff (#1)}
\newspcommand{\mycom}{Got this stuff (#1)}

\begin{document}

\mycommand|test|

\mycommand+test+

\mycommand&test&

\mycom$test$

\mycom AtestA

\end{document}
`
-