Active1 year, 5 months ago
Selected LaTeX Math Symbols. Note: there is another version of this document featuring HTML entities for math symbols, as well as LaTeX commands.
How do you create a command with optional arguments in LaTeX?Something like:
Then, I can call it like
Will Robertson34.3k3030 gold badges8787 silver badges109109 bronze badges
VerhogenVerhogen10.7k2828 gold badges7474 silver badges106106 bronze badges
6 Answers
JesseTG96211 gold badge1212 silver badges3535 bronze badges
mikumiku139k3838 gold badges258258 silver badges282282 bronze badges
The general idea behind creating 'optional arguments' is to first define an intermediate command that scans ahead to detect what characters are coming up next in the token stream and then inserts the relevant macros to process the argument(s) coming up as appropriate. This can be quite tedious (although not difficult) using generic TeX programming. LaTeX's
@ifnextchar
is quite useful for such things.The best answer for your question is to use the new
xparse
package. It is part of the LaTeX3 programming suite and contains extensive features for defining commands with quite arbitrary optional arguments.In your example you have a
sec
macro that either takes one or two braced arguments. This would be implemented using xparse
with the following:The argument
Will RobertsonWill Robertson{ m g }
defines the arguments of sec
; m
means 'mandatory argument' and g
is 'optional braced argument'. IfNoValue(T)(F)
can then be used to check whether the second argument was indeed present or not. See the documentation for the other types of optional arguments that are allowed.34.3k3030 gold badges8787 silver badges109109 bronze badges
All of the above show hard it can be to make a nice, flexible (or forbid an overloaded) function in LaTeX!!! (that TeX code looks like greek to me)
well, just to add my recent (albeit not as flexible) development, here's what I've recently used in my thesis doc, with
Start the command, with the 'optional' command set blank by default:
I then have the macro set a temporary variable, temp{}, differently depending on whether or not the optional argument is blank. This could be extended to any passed argument.
![Latex Define Command Latex Define Command](/uploads/1/2/4/7/124711658/446569923.png)
Then I run the macro using the temp{} variable for the two cases. (Here it just sets the short-caption to equal the long caption if it wasn't specified by the user).
In this case I only check for the single, 'optional' argument that newcommand{} provides. If you were to set it up for, say, 3 'optional' args, you'd still have to send the 3 blank args... eg.
which is pretty silly, I know, but that's about as far as I'm going to go with LaTeX - it's just not that sensical once I start looking at TeX code... I do like Mr. Robertson's xparse method though, perhaps I'll try it...
DemisDemis
Thomas13.4k44 gold badges3737 silver badges6666 bronze badges
Alexey MalistovAlexey Malistov19.4k1212 gold badges5656 silver badges8282 bronze badges
I had a similar problem, when I wanted to create a command,
dx
, to abbreviate ;mathrm{d}x
(i.e. put an extra space before the differential of the integral and have the 'd' upright as well). But then I also wanted to make it flexible enough to include the variable of integration as an optional argument. I put the following code in the preamble.Then
gives dx with optional argument
csarcsar
Here's my attempt, it doesn't follow your specs exactly though. Not fully tested, so be cautious.
dreamlaxdreamlax80.6k2828 gold badges148148 silver badges201201 bronze badges
![Latex Latex](/uploads/1/2/4/7/124711658/531249415.png)