Skip to main content
Share Your Experience: Take the 2024 Developer Survey
31 votes
Accepted

Are all commands with an optional argument fragile?

The command you defined does not take an optional argument, it takes a delimited argument. If you do: \def\b[#1]#2{.#2.\bf #1} \b[one]two it will work fine, however if you remove the [one] TeX will ...
Phelype Oleinik's user avatar
22 votes
Accepted

Any difference between \newcommand{\foo}{foo} and \newcommand{\foo}[0]{foo}?

there is no difference, in fact [0] is the default value for the optional argument so after the first scan for the optional argument they follow identical code paths.
David Carlisle's user avatar
19 votes
Accepted

Why do people specify date as an option for \usepackage?

From source2e.pdf, page 479 (CTAN page or texdoc source2e in a terminal):
Phelype Oleinik's user avatar
17 votes
Accepted

Xparse's new e-type argument (replacement for k-type argument)

The k argument type is not available any longer. There is a big difference between the old k and the new e type. With e{ABC} (here A, B and C represent any three distinct tokens) the macro will ...
egreg's user avatar
  • 1.1m
17 votes
Accepted

Using the compulsory argument for the optional argument?

The classical approach for this is to use \@dblarg: \documentclass{article} \makeatletter \newcommand{\foo}{\@dblarg\ah@foo} \def\ah@foo[#1]#2{#1 foo(#2)} \makeatother \begin{document} No optional ...
egreg's user avatar
  • 1.1m
14 votes
Accepted

Is there a LaTeX recommended standard for commands with optional arguments?

Even standard latex commands have optional arguments before or after the mandatory arguments, for example \usepackage{zzzz}[1884/01/01] what is true is that the simple interface to defining optional ...
David Carlisle's user avatar
14 votes

argc argv in LaTeX

The command line design for running TeX is very old, so you can find it very atypical. The command switchers (like -ini, -jobname) are read first. If next object is not - nor \ then it is assumed that ...
wipet's user avatar
  • 75.4k
13 votes
Accepted

Patch 2016 xparse to support k-type argument

With the current xparse release (2017/12/16), the following will re-add the k-type argument: \documentclass{article} \usepackage{xparse} \usepackage{xcolor} \ExplSyntaxOn \tl_if_exist:NF \...
Joseph Wright's user avatar
  • 261k
13 votes

Defining optional parameters in \newcommand

An optional parameter is defined by adding a [<opt>] after defining the number of arguments: \documentclass{article} \usepackage{xcolor,amsmath} \newcommand{\TM}[2][red]{\textcolor{#1}{$\...
Werner's user avatar
  • 605k
13 votes
Accepted

Tabular ignoring optional parameter to \\

\\ inside a table is defined via \@tabularcr. If there is an optional argument with positive length, the macro takes the default depth of the line, adds the length and sets an invisible rule with the ...
Heiko Oberdiek's user avatar
13 votes
Accepted

How to pass parameters to \tikzstyle?

Everything about keys is detailed in section "87 Key Management" in pgf documentation. But about your code: once you define a style with parameters, use style=parameter_value to change it. You ...
Ignasi's user avatar
  • 137k
13 votes

Add optional argument to newcommand for integration dx

I'd prefer something like \newcommand{\dx}[1][x]{\mathop{}\!d#1} that does the trick much better than adding \, explicitly. But if you just define a macro for d, you get easier input: \newcommand{\...
egreg's user avatar
  • 1.1m
12 votes

Are all commands with an optional argument fragile?

The information on that page is wrong (or at least outdated, all commands in latex2.09 that had an optional argument were fragile, but latex2e has been available since 1993...) the example in the ...
David Carlisle's user avatar
12 votes
Accepted

Pass 3 optional arguments in newcommand

I see two ways to do it. With xparse you can use \NewDocumentCommand to create a new command with multiple optional arguments. Once you get beyond two optional arguments the usage becomes a bit clumsy ...
LaTeXereXeTaL's user avatar
11 votes
Accepted

How to define a command with two optional arguments?

You should start from examining #2 rather than #1, because #2 only can have a value if #1 has, too. \documentclass{article} \usepackage{xparse} \NewDocumentCommand \C { o o m } {% \IfNoValueTF{#2} ...
egreg's user avatar
  • 1.1m
11 votes
Accepted

How to define a comma separated list of arguments for includegraphics

Just define new keys: \documentclass{article} \usepackage{graphicx} \makeatletter \define@key{Gin}{settingsA}[]{\setkeys{Gin}{width=\textwidth, angle=90}} \define@key{Gin}{settingsB}[]{\setkeys{Gin}{...
egreg's user avatar
  • 1.1m
11 votes

Why do people specify date as an option for \usepackage?

Phelype Oleinik pointed you at the LaTeX sources but there is also a user guide called usrguide.pdf texdoc usrguide will open the document in texlive. Section 3.2 on preamble commands has the ...
David Carlisle's user avatar
11 votes
Accepted

\newcommand: Using one parameter as the default for the other

Good news: you can do it very easily with xparse: \documentclass{article} \usepackage{xparse} \NewDocumentCommand{\foo}{O{#2}m}{% Optional=``#1'', mandatory=``#2''\par } \NewDocumentCommand{\oof}{...
egreg's user avatar
  • 1.1m
10 votes
Accepted

A transparent optional agrument macro

Unlike a macro argument, a _ does not pick up the next token if it is not braced, it expands to find the next non expandable token or brace group so after \newcommand\foo{abc} then \fbox\foo ...
David Carlisle's user avatar
10 votes
Accepted

xparse's 's' argument returns \Gamma and \Delta, instead of \BooleanFalse and \BooleanTrue

\BooleanFalse and \BooleanTrue are boolean variables not meant for typesetting. You can only use them in a \IfBooleanTF test: \documentclass{article} \usepackage{xparse} \NewDocumentCommand{\myfuncB}...
Phelype Oleinik's user avatar
10 votes
Accepted

Is there a way to add optional and/or named argument to command

Classically the LaTeX2e solution was the keyval package (as used by \includegraphics[height=2cm, scale=3]{...}) but recent LaTeX releases include a keyval parser built in, based on l3keys. @Campa's ...
David Carlisle's user avatar
9 votes

Xparse k argument to parse options at end of environment

The problem with \AfterEndEnvironment is that there's an extra \if@ignore\@ignorefalse\ignorespaces\fi bit of code after it. So the arguments k_k^ never reach their intended sub- and super-scripts. ...
Manuel's user avatar
  • 27.2k
9 votes

How to write a macro that takes a variable number of arguments

Of course, the syntax uses the standard LaTeX convention of optional arguments in square brackets, in this case for both optional arguments. Also, the OP implied that the command should be used not ...
Steven B. Segletes's user avatar
9 votes
Accepted

LaTeX Nesting \verb in \section

That's because verbatim content always causes you trouble. The \section command takes one argument, which in the first case, is \textit{mySec} and in the second it is \verb|mySec|. Everything by now ...
Phelype Oleinik's user avatar
9 votes
Accepted

Euler and minus sign

Something like this? \documentclass[a4paper,12pt,fleqn]{article} \def\imaginaryunit{j} % the imaginary unit, i for mathematician and theoretical physicist, j for the rest of the ...
Steven B. Segletes's user avatar
9 votes

\newcommand: Using one parameter as the default for the other

You can use xparse to easily condition on whether or not an optional argument was present or not, and supply the appropriate combination to another (auxiliary) function. Here's an example: \...
Werner's user avatar
  • 605k
9 votes

How LaTeX "understands" optional arguments?

LaTeX is written in TeX, and TeX has no built-in concept of an optional argument. What is does have is the ability to 'look ahead' for a token, and a way to grab delimited arguments. So the way LaTeX ...
Joseph Wright's user avatar
  • 261k

Only top scored, non community-wiki answers of a minimum length are eligible