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.
Asked
Active
Viewed 395 times
10
-
See also Can you interpret macro parameters as verbatim? - TeX - LaTeX Stack Exchange if you want catcode sanitization. – user202729 Dec 25 '21 at 09:40
2 Answers
12
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!
Aditya
- 62,301
5
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.
Lev Bishop
- 45,462
egreg
- 1,121,712
-
what about defining a command whose argument can be placed between a pair of arbitrary symbols? How is
\verbdefined so that\verb|spleen|,\verb+spleen+, etc. work? – Ian Thompson Jun 29 '11 at 23:39