# Tag Info

66

If you read deep in the LaTeX code you can find lots of examples where this is done. Any command that has optional arguments actually does this already: there are actually two different commands and which one is called depends on whether or not it is called with an optional command. The TeX trick is to use \@ifnextchar[. For example, \def\mycmd{\@...

60

Try the LaTeX 3 package xparse. Example \documentclass{article} \usepackage{xparse} \DeclareDocumentCommand{\foocmd}{ O{default1} O{default2} m }{#1~#2~#3} \begin{document} \foocmd{foo} \par \foocmd[nondefault1]{foo} \par \foocmd[nondefault2][notfoo2]{foo} \par \end{document} You may read the documents for more information.

45

The LaTeX3 xparse package is designed to aid the construction of macros that take complex optional arguments. In this case, you'd write \usepackage{xparse} .... \DeclareDocumentCommand \foo { o m } {% \IfNoValueTF {#1} {% \something {#2}% }{% \someotherthing {#1} {#2}% }% } Internally, this does the same thing as Andrew's answer, but the ...

36

LaTeX's optional arguments viz TeX's macro arguments (delimited and undelimited) The LaTeX concept of optional arguments (i.e., arguments that may or may not been used) is a concept that is not directly supported by TeX's parsing and execution. TeX macros always expect the same number of arguments with the same syntax for delimiting the argument. Optional ...

32

Add a space before #1 in the environment's definition, and specify \unskip (which will remove the space) as the optional argument's default value. \documentclass{article} \newenvironment{argument}[1][\unskip]{% \par \noindent \textbf{Argument #1:} \noindent} {} \begin{document} \begin{argument} Some text. \end{argument} \begin{argument}[A] Some text. \...

31

I want to thank for all of the other answers received. But lately I've turned into a huge fan of the etoolbox package, which provides a great abstraction to do exactly what I wanted at the LaTeX level (with no low level TeX trickery): \usepackage{etoolbox} \newcommand\mycmd[2][]{% \ifstrempty{#1}{% % something with #2 }{% % some other thing ...

31

You've got answers for other approaches, so here is the basics using the kernel only. You'll need to define your macros by hand, something like \makeatletter \def\mycommand{% \@ifnextchar[% {\mycommand@i} {\mycommand@i[<default-for-1>]}% } \def\mycommand@i[#1]{% \@ifnextchar[% {\mycommand@ii{#1}} {\mycommand@ii{#1}[<...

27

\documentclass[parskip]{scrartcl} \usepackage[margin=15mm]{geometry} \usepackage{xifthen} \newcommand{\test}[1][]{% \ifthenelse{\equal{#1}{}}{omitted}{given}% } \begin{document} The optional argument was \test[]. The optional argument was \test[shubidu]. \end{document} Which results in

26

group the citation... \documentclass{article} \begin{document} \begin{itemize} \item[{[GMR85]}] ... \end{itemize} \end{document} The Beamer package makes things a little bit more difficult. \documentclass{beamer} \begin{document} \def\braces#1{[#1]} \begin{frame}{frame title} \begin{itemize} \item[\braces{GMR95}] ... \end{itemize} \end{frame} \...

25

If you need to pass a ] inside an optional argument, just enclose the argument in figure braces: \begin{theorem}[{\cite[p.~90]{bibitem}}] Theorem body. \end{theorem} Without this, it's the \cite[p.~90 (everything up to the first ]) that gets passed as the argument, while the remaining text is typeset afterwards.

24

The kvoptions package gives you the possibility to define key=value style options. It "connects package keyval with LATEX's package and class options" (quote from the manual). You can then declare options like margin using: \DeclareStringOption [<init>]{<key>}[<default>] were <init> is the initial value (also used when the option ...

23

I'd use the xparse package to do this, as everything is then 'pre-packaged': \documentclass{article} \usepackage{color,xparse} \NewDocumentCommand\MyTextColor{m+g}{% \IfNoValueTF{#2} {\color{#1}} {\textcolor{#1}{#2}}% } \begin{document} \MyTextColor{green}{stuff} \MyTextColor{red} Some text \end{document} The same can of course be done using \@...

23

Just like whenever you want to put a special character (such as a space, or something else) after a command, you can use the {} to suppress the next character. \item{}[your random text

22

I'd prefer Will Robertson's solution, that's more manageable. For a "pure" LaTeX solution: \makeatletter \def\ifemptyarg#1{% \if\relax\detokenize{#1}\relax % H. Oberdiek \expandafter\@firstoftwo \else \expandafter\@secondoftwo \fi} \makeatother \newcommand{\mycmd}[1][]{% \ifemptyarg{#1} {<code for empty argument>} {<code ...

22

\newenvironment{whatever}[4][??] {...} {...} \begin{whatever}{one}{two}{three} is #1= {??} #2= one #3= two #4= three \begin{whatever}[zero]{one}{two}{three} is #1= zero #2= one #3= two #4= three \newenvironment{whatever}[4][foo] {...} {...} \begin{whatever}{one}{two}{three} is #1= foo % preset to foo, if missing #2= one ...

22

When TeX reads arguments, then TeX only checks for matching curly braces (characters with catcode 1 and 2). Square brackets are not special in this sense. The first ] that is not hidden inside curly braces is taken as the end of the optional argument. Therefore an additional set of braces is the usual solution: \foo[{\bar[...]}]{...} It is only a bug, if ...

21

You can get \\foo@ using \csname\string\foo@\endcsname. The space shown behind it with \show is not part of the macro name (but could be added using a \space before \endcsname). So you can use: \expandafter\patchcmd\csname\string\foo@\endcsname{hook}{\label{#1}hook}{success}{failure} Full example: \documentclass{article} \usepackage{etoolbox} \begin{...

20

Mask the content of thm' s optional argument with an additional set of braces. EDIT: For what it's worth, with ntheorem loaded one needs two additional sets of braces. \documentclass{article} \usepackage{ntheorem} \newtheorem{thm}{Theorem} \begin{document} \begin{thm}[{{\cite[page 10]{ABC2011}}}] A theorem. \end{thm} \begin{thebibliography}{9} \...

20


19


19

To elaborate a bit on Joel's answer, there are a few additional points. Both Knuth's plain TeX and LaTeX use { and } as grouping tokens. They are then used to delimit mandatory argument, for example \section{A section title} LaTeX then uses [ and ] to indicate optional arguments, so for example \section[Short title]{A section title} The idea is that ...

19

NOTE: There are some minor weaknesses in the code shown below, so it's best to use the corresponding package. The following macros define the commands \xpatchcmd, \xpretocmd and \xapptocmd that recognize robust commands (defined by \DeclareRobustCommand or \newrobustcmd) and also if they have an optional argument. \usepackage{etoolbox,xparse} \...

18

The primitive \def does not understand optional arguments. That is provided in LaTeX only. What you have defined is a delimited argument: \def\sqrt[#1]{...} which means that the input must always be provided as [...], you could have written anything instead of []. To have an optional argument you need to define it in LaTeX using the following method: ...

18


17

You can't "overload" macros in TeX like functions in other programming languages. You can either define the macro to use a normal optional argument for one of the two parameters or define a special macro which looks ahead if a opening brace follows. The xparse package can help you defining one: \documentclass{article} \usepackage{amsmath} \usepackage{...

17

The xparse package provides an easy user interface to specify optional arguments to environments (in various orders) including commands/macros. For this you define your environment using \NewDocumentEnvironment, while regular macros use \NewDocumentCommand. The former has the following syntax: \NewDocumentEnvironment{<env>}{<arg spec>}{<beg ...

17

Here are all the ways I know of to do what you want: First, you could use \pgfkeyslet together with a macro, defined through \newcommand, that takes an optional argument (Marc's approach, simplified a bit but somewhat less functional). The downside: first, you have to remember to take care of the \pgfkeyseov that is inserted, and also, if you only pass ...

17

There are several ways to do this: 1. define a new command (as carsten said already) \newcommand{\trimmedgraphic}[2][]{% \includegraphics[trim = 1cm 2cm 1cm 2cm,clip,width=1\textwidth,#1]% {#2}% } You can define the command with an optional argument to be able to pass additional options to \includegraphics. You may add the {figure} stuff if ...

16

Note: Without a MWE I will remove \end{tabular} from the definition of your macro and ignore the fact that you declare two argments (one optional, one mandatory) but use only #1. An optional argument behaves quite different from normal arguments and groups as it is catched by TeX with the help of the [/] delimiters. With a macro is defined with \...

15

LaTeX kernel solution \makeatletter \newcommand{\mycommand}{\@dblarg\@mycommand} \def\@mycommand[#1]#2{\textbf{#1} #2} \makeatother See this answer for a description of \@dblarg. xparse solution \usepackage{xparse} \NewDocumentCommand{\mycommand}{o m} {\textbf{\IfValueTF{#1}{#1}{#2}} #2} \IfValueTF{#1}{A}{B} looks whether the optional argument was ...

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