{optional-arguments} concerns commands and environments that accept optional arguments. For example the \sqrt command can be used as \sqrt[3]{x}.

learn more… | top users | synonyms

58
votes
5answers
6k views

Different command definitions with and without optional argument

I want to define a command that expands to two completely different things depending on whether I supply, or not, an optional argument. For example, \mycmd{normal} -> something(normal) ...
45
votes
5answers
7k views

More than one optional argument for newcommand

The \newcommand macro allows to use an optional argument for the first parameter #1 with: \newcommand{\mycommand}[3][defaultfor1]{blah blah blah} Is it possible to have more than one option with ...
27
votes
1answer
3k views

How can I make a package that accepts an optional argument?

I am writing a package that has a bunch of values that are going to be the same most of the time, but that I might want to change in select cases. I want to do this by giving this package an optional ...
26
votes
5answers
1k views

How can I use one argument as the default value for another?

I'd like to define a command that takes 1, 2 or 3 arguments, and where the 2nd gets the same value as the 1st if it is not defined. I tried \newcommand\codefrom[3][#1][Matlab]{ ... } but I get an ...
24
votes
3answers
2k views

How can I use \patchcmd with commands with optional arguments?

This question led to a new package: xpatch This is likely a simple question, but I'm getting stuck on the small details. I have an internal command, defined by a package as ...
21
votes
1answer
628 views

] inside an optional argument

I accidentally discovered (here) that a closing square bracket within an optional argument delimited by [ ] can cause problems. Here is example code illustrating the issue: \documentclass{article} ...
21
votes
2answers
6k views

Escaping square braces

I want to use some text as the optional parameter to some command. The text includes square braces ([ and ]), but since the optional parameter goes within square brackets, unexpected things happen. ...
17
votes
2answers
319 views

Why do some commands use []s, some use {}s, and some use both?

I find it confusing to remember which commands take {} arguments, and which ones take [] arguments. Is there a rule-of-thumb or semantic difference between these two? If so, what? (Because they seem ...
15
votes
2answers
2k views

New command with optional argument being first argument [duplicate]

I know I can create a new command with an optional parameter with \newcommand{\mycommand}[2][Hello]{\textbf{#1} #2} I would like to have the command such that there is an optional parameter which ...
15
votes
1answer
1k views

Define same command with different amount of parameters

I want to define two versions of the command \Set, depending if I provide one or two parameters. I.e. like this: \newcommand{\Set}[1]{\bigl\{ #1 \bigr\}} \newcommand{\Set}[2]{\bigl\{ #1 \bigm| #2 ...
14
votes
3answers
223 views

How to define a new command based on \section with its optional argument?

I've defined a new command called \mysec like the below: ‎‎‎‎‎‎ \documentclass{article} \newcommand{\mysec}[1]{\section{#1}} \begin{document} \mysec[Foo foo]{Bla bla bla} \end{document} But in the ...
14
votes
1answer
199 views

How to read a stream of tokens up to one of two different delimiters

The core question How can I process text delimited by one of two terminators? For instance, if I want to process text up to the next \A, I can just write \def\CmdA#1\A{...} But suppose that \A ...
13
votes
3answers
7k views

Creating a new environment with one optional argument

I want to create a new environment with one optional argument in LaTeX. Here is what I used: \newenvironment{argument}[1][]{% \par \noindent \textbf{Argument#1:} \noindent} {} When there is no ...
13
votes
2answers
2k views

How do you define your environment such as to use […] for some parameter(s)?

In an environment, say \newenvironment{whatever}[3]{...}{...} you instantiante it (if that's the word for it in LaTeX) by \begin{whatever}{first}{second}{third} ... \end{whatever} But how do ...
13
votes
2answers
5k views

A macro with either one or two arguments

I am trying to define a macro with variable number of arguments. For example I want to define a macro with two arguments that can work with one argument as well. So the same macro works in two ways. ...
13
votes
4answers
3k views

Optional arguments in \def

I want to re-define the \sqrt command (as detailed in Nice-looking p-th roots), and so far I have the following: \documentclass{article} \usepackage{fouriernc} % use the New Century Schoolbook font ...
13
votes
2answers
304 views

How can one put a “[” after \item?

The following does not compile because of the "[" after the \item. I presume it is because LaTeX expects a "]" as per the \item[option] syntax. However, I just want to insert the "[" character into ...
13
votes
2answers
312 views

TeX Parameter Processing imitating key-value pairs

I am thoroughly confused by this answer to How to generate n points on a circumference and connect all of them while having constraints on the image size as I don't understand how the parameter ...
13
votes
5answers
251 views

Two independent optional arguments for new commands

I'm trying to adapt the solution provided in Different command definitions with and without optional argument to change the behavior of my new command. I'd like to use basic command to avoid some non ...
12
votes
1answer
380 views

Bug? LaTeX misparses nested optional arguments

LaTeX seems to be having problems handling nested optional arguments. I've included a minimal code example that generates the error: \documentclass[12pt,oneside]{book} \begin{document} ...
11
votes
3answers
173 views

Cannot use ampersand as argument in user defined command with optional arguments

I'm trying to produce a command for an equation that may or may not be used inside an align element, and therefore sometimes needs a & next to the = to align the equation. I therefore defined the ...
11
votes
2answers
1k views

A new environment based on enumerate

I have defined the following simple environment: \newenvironment{renumerate}{\begin{enumerate}\raggedright}{\end{enumerate}} I wonder how the above can be modified to use with, say, the following: ...
11
votes
1answer
2k views

Cite in theorem environment argument

When I try to cite a book in such a way \begin{theorem}[\cite[p.~90]{bibitem}] Theorem body. \end{theorem} I've got an error because of multiple use of [ and ]. Is it possible to cite in such a way ...
11
votes
1answer
96 views

Processing document class options with l3keys

How can I parse (that is, access) the entire option list given to the current document class with l3keys? This would obviously need some sort of \keys_define:nn { document-class-?? } { default ...
11
votes
1answer
146 views

Parsing commands with multiple optional arguments

Some LaTeX commands accept multiple optional arguments. Here are some examples: \makebox[<width>][<pos>]{<text>} \framebox[<width>][<pos>]{<text>} ...
11
votes
3answers
988 views

How to pass an optional argument to an environment with verbatim content?

This is an attempt to make clear what happened in this question of xport (now deleted, sorry). The now deleted question included code that tried to make my answer to this question of xport work for ...
10
votes
3answers
332 views

Defining a list environment with multiple optional arguments

I'm trying to define a list environment with multiple optional arguments and with a default values for those arguments. For example, creating a new environment with a name newenv, which accepts two ...
10
votes
2answers
176 views

Flexible parenthesing command

I would like to create a smart parenthesing command without using the xparse package. \p(1) prints (1) \p[1] [1] \p{1} {1} \p(1|2) (1|2) I tried this code but it ...
10
votes
2answers
217 views

Can I define a command with optional argument coming after the mandatory ones?

Is it possible to define new commands having the following form: \command{<marg>}[<oarg>] I have seen commands of this form in the fontspec package. But not sure how to achieve this.
10
votes
2answers
2k views

Citing inside theorem header

How do I create something like this without getting any errors? \begin{thm}[\cite[page 10]{ABC2011}] ... \end{thm} I tried putting various things into {...} brackets, but not eliminating any ...
10
votes
1answer
240 views

New command with default does not work

Hello I have the following new command (a bit simplified for readability): \newcommand{\xz}[1][0]{x_{#1}} I'm expecting the following behavior: $\xz$ outputs as $x_0$ $\xz{1}$ outputs as $x_1$ ...
10
votes
1answer
161 views

Optional argument problem

This example works but if I try to put an optional argument changing \newcommand\TBLdiag[5]{% in \newcommand\TBLdiag[5][\tabcolsep]{% it does not work anymore. Of course I call \TBLdiag with ...
9
votes
3answers
749 views

Optional arguments in pgfkeys?

I need an optional argument in a pgfkey? Something like the following: \pgfkeys{example/.code = (Argument: #2, optional argument: #1)} \pgfkeys{example=[other]{some}} should result in (Argument: ...
9
votes
2answers
180 views

Defining a macro with three optional arguments in the form \newmacro{a}{b}[c]{d}[e][f] and \newmacro{a}{b}[c]{d}*[f]

I am defining a macro in LaTeX2e for writing function declarations. I want that the macro has tree compulsory arguments: the name of the function (v.g f), a depiction of the type of variable it takes ...
9
votes
2answers
522 views

How to use a macro inside \includegraphics options?

I have to do several figures like this: \begin{figure}[h!] \centering \includegraphics[trim = 9.55cm 1.8cm 10.25cm 4.05cm, ...
9
votes
3answers
300 views

Create macro with several options

I struggle to create a macro that can take several options. What I want to create is for \mycommand[up,middle]{Macro} to output Macro with 'up' and 'middle' specified. Using etoolbox in the MWE below ...
9
votes
3answers
668 views

Are commands defined by \newcommand[.][.]{.} robust?

This question is from the comments at the foot of Abbreviations for AM, PM (small caps) for use within \textbf. Readers will get the remaining gist from there. With ...
9
votes
2answers
1k views

creating beamer themes with options

Is it possible to create beamer themes that take options? I have three themes that are essentially the same yet each beamertheme<name>.sty overrides certain aspects of the inner, outer and ...
9
votes
1answer
109 views

Optional arguments: xparse vs. xargs

To implement a macro that takes one or more optional arguments that do not have to be placed in the first position, xargs can be used as well as xparse. The latter seems to have more recent updates ...
9
votes
1answer
1k views

TikZ: \newcommand with optional TikZ options

I want to create a new command with some default TikZ options that you can override using an optional argument. If working as intended the code below would generate a red line between A and B, and a ...
8
votes
4answers
214 views

Special macro definition (2opt, 1mand)

How can you define a macro \cmd in TeX primitives using two optional arguments (and a mandatory one) that fulfills the property that calling \cmd[opt]{mand} is equivalent to \cmd[][opt]{mand}. Yet, I ...
8
votes
2answers
1k views

LaTeX: optional arguments with square brackets

LaTeX is trying to add a missing $ that I don't want to the following code: EDIT: replaced stricken lines from original question with minimal working example: \documentclass[letter,12pt]{article} ...
8
votes
1answer
194 views

How can I make a custom theorem for a definition?

Looking at the wikibooks article on theorems has me wondering... I'd like to do something, if possible, similar to the default "proof" theorem style, whereby I can add a custom name to each theorem as ...
8
votes
1answer
97 views

Automatically Insert Environment Arguments

How can I automatically insert [<+->] arguments to the itemize environment when I'm in a beamer frame so I get pretty incremental display during presentation?
8
votes
2answers
104 views

Connecting multiple arguments

I recently discovered Latex and is using TexMaker to edit my documents, in one of my university courses we will be performing a number of tests on a hydraulic rig. Since the tests will be conducted by ...
8
votes
1answer
106 views

Defining a command that scans every arguments' macro “looking for” a token

Knowing that every command declaration with conditionally optional arguments, as \foo{...} expanding into \foo[default]{...} can be defined by \newcommand\foo[1][default]{...} or the safer version of: ...
8
votes
1answer
66 views

Moving the optional argument

When I create macros using \newcommand, I usually provide default variants setting some of the first arguments in a sort of partial application style \newcommand\five[5]{#1,#2,#3,#4,#5} ...
7
votes
3answers
500 views

xparse empty arguments

I have defined a command using xparse as \DeclareDocumentCommand\mymacro{o o o} and I want to call it with various numbers of arguments (up to 3) \mymacro, \mymacro[test][two], \mymacro[][][test] ...
7
votes
3answers
178 views

Adjusting command behavior based on argument type

I have a following command in my latex document: \newcommand{\ApplyGradient}[1]{ \pgfmathsetmacro{\PercentColor}{100.0*(#1+0.2)/1.3} \textcolor{black!\PercentColor}{#1}} ...
7
votes
2answers
135 views

Using a required argument as a default for an optional one in \newcommand

I'd like to create a new subsection command with the same calling structure like the original one, but I have no idea on how to handle the case when the optional argument is omitted. Basically I want ...