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 would like to define a newcommand that takes one required parameter, but instead of passing it in using {...} as normal, to use a different parameter delineator like the \verb command does (e.g., \mycmd|args|). Trying to do this with a normal \newcommand definition seems to behave strangely.

share|improve this question
    
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

3 Answers 3

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!
share|improve this answer

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.

share|improve this answer
    
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}
share|improve this answer

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.