4

I want to make macro which do not require curly brackets to accepts an argument. Lets invent an example. Say I want a macro which accepts an optional argument f and an argumentx:

\dd[f]{x}

And which is equivalent to writing:

\frac{\mathrm{d} f }{\mathrm{d} x }

The question is, can make a macro such that I dont have to write the brackets, but which will still accept the argument?

It would be very conventient if I could avoid using the brackets. Something like this would be excellent:

\dd f,x

Is there any way of avoiding writing the brackets?

  • 1
    Just want to go on record to say this is generally a bad idea. When I was trying to make my syntax more readable (to me at the time), I often simply caused far more problems than I solved -- problems that I had no idea how to really fix (since I didn't really understand expansion). Would a syntax like \dd[f, x] be acceptable as an alternative? – Sean Allred Dec 12 '14 at 14:25
  • 1
    @SeanAllred -- thanks for the comments. using \def is definitely worthwhile in some situations, and preferable to braces, but this is not one of them. – barbara beeton Dec 12 '14 at 14:44
12

this kind of macro is a plain tex construction, and is called a "delimited macro". it is discouraged in latex, at least partly because it's easy to make mistakes.

here's an example that does what you ask:

\documentclass{article}
\def\dd #1,#2 {\frac{\mathrm{d} #1 }{\mathrm{d} #2 }}
\begin{document}
\thispagestyle{empty}
$\dd f,x $
\end{document}

output of example code

observe that if you omit the space after the second argument in the input

$\dd f,x$

you will get an error:

! Missing } inserted.
<inserted text> 
                }
l.5 $\dd f,x$

however, not having a definitive "closer" limits the recognition of the argument to a single input token, which could be very limiting, although not for this example.

edit: in response to a request for a similar delimited macro in which arguments can be optional, here is the same one, modified for that purpose. in this particular case, all that is required is for every argument to be clearly delimited.

\documentclass{article}
\def\dd #1,#2;{\frac{\mathrm{d} #1 }{\mathrm{d} #2 }}
\begin{document}
\[\dd f,x; \quad \dd ,x; \quad \dd ,; \]
\end{document}

output of example code

in this case, where each argument is likely to be only one letter or control sequence (e.g. \phi), omission of an argument simply omits it from the output. (just don't type a period instead of a comma, as i did when setting up the example.)

in more complicated situations, where multi-character strings would be the arguments, the possible presence of punctuation in an argument means that the choice of delimiters must be made with much more care; in such cases, one could also enclose affected arguments in braces to make them appear as a single tokan to (la)tex, as suggested in a comment by @SeanAllred.

and, if this macro is to be used in text, rather than in math, be very careful about spaces in the expansion.

  • 2
    Note to the OP: you define the closer as the space in #2 {. If you want a different closer, perhaps a ;, just use #2;{. If you do not use a closer, TeX will only use the first character (or {brace group}) as #2, something that may or may not be acceptable for your uses. – Sean Allred Dec 12 '14 at 14:28
  • I've done similarly many times :) You're very welcome. – Sean Allred Dec 12 '14 at 14:52
  • @barbara , thanks for this reply. I want to accept this as the final answer. However, is it possible to make one, or a few of the arguments optional, with a default value? Thanks so much for your time! – Mikkel Rev Dec 12 '14 at 17:55
  • @MariusJonsson -- it's possible, but not trivial, to make arguments optional. if arguments are to be made optional, every one has to be unmistakably identifiable, which is equivalent to saying that every one has to be delimited in some distinct way; spaces won't work. a comma-separated list, including a final delimiter, is possible, and of course, you are limited to 9 arguments. propose a context and a desired result, and i'll see what i can come up with. (it won't pass the latex "scratch test".) – barbara beeton Dec 12 '14 at 18:39
  • @barbarabeeton, thanks you are a star. Something like $\dd f,x;$ where f is optional and x is not. ; is closer. The default argument if no f an empty empty string. Is that possible? Or something very similar? Thanks. – Mikkel Rev Dec 12 '14 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.