82

\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


59

\newcommand{\add}[2]{\textit{#1} {`#2'}} For more reading material on this, see LaTeX help with \newcommand; and/or LaTeX/Macros wiki


54

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. \...


49

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 ...


44

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 ...


30

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 throw an error: \def\b[#1]#2{.#2.\bf #1} \b two ! Use of \b doesn't match its definition. l.5 \b t wo ? because when you define a command with \def\b[...


26

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 ...


26

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 ...


22

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 ...


22

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.


20

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 ...


20

There is a disadvantage using two or more optional arguments. If the user wants to change the default for the second argument, then he must specify the default value of the first argument explicitly. As an example, the optional depth argument of \raisebox should be set, but the resulting height should not be changed. That makes the first optional argument ...


20

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 \...


20

Since you're already loading the enumitem package, it makes sense to use the in-built command newlist that it provides for exactly this purpose \newlist{renumerate}{enumerate}{3} \setlist[renumerate]{label=\arabic*,before=\raggedright} This sets up the renumerate environment to be based on the standard enumerate environment. The {3} at the end specifies ...


19

I'm going to suggest a very different approach which might seem like a bit of overkill in this particular situation but can pay off in the long run. I would suggest using keys to accomplish what you want. So, I would set up keys as follows: \documentclass{article} \usepackage{pgfkeys} \makeatletter \pgfkeys{/jeroen/entities/.cd, title/.initial=, ...


19

From source2e.pdf, page 479 (CTAN page or texdoc source2e in a terminal):


18

One more resolution for several optional arguments problem. \documentclass{article} \usepackage{xargs} \newcommandx{\foo}[4][1=defaultFirstArg, 3=defaultThirdArg]{#1~#2~#3~#4} \begin{document} \foo{B}{D} \foo[A]{B}{D} \foo{B}[C]{D} \foo[A]{B}[C]{D} \end{document} I hope there is nothing to comment here. Also \renewcommandx, \...


18

You need to update the way the theorem header is set, since it includes ( ) by default (taken from amsclass.dtx): \def\thmhead@plain#1#2#3{% \thmname{#1}\thmnumber{\@ifnotempty{#1}{ }\@upn{#2}}% \thmnote{ {\the\thm@notefont(#3)}}} \let\thmhead\thmhead@plain Note the use of (#3) above. So, we copy-and-paste the above definition with the adjustment: \...


18

\makeatletter \newcommand{\mycommand}[1][\@nil]{% \def\tmp{#1}% \ifx\tmp\@nnil no argument \else argument #1 \fi} \makeatother \mycommand zzzz \mycommand[hello] zzz


17

You can do it easily with xparse: \documentclass{article} \usepackage{xparse} \NewDocumentCommand{\mycommand}{o}{% % <code> \IfNoValueTF{#1} {code when no optional argument is passed} {code when the optional argument #1 is present}% % <code> } \begin{document} \mycommand \mycommand[HERE] \end{document} This will print ...


16

There is more than one problem with such a definition. The \def instruction has the following syntax: \def<cs><parameter text><left brace><balanced text><right brace> where <cs> is the control sequence or active character to define; <left brace> and <right brace> stand for explicit braces (character tokens ...


16

There are several approaches you can take. The easiest approach is to use xparse Then you can define a command as \documentclass{article} \usepackage{xparse} \NewDocumentCommand{\mycommand}{ mO{a} }{#2 \rightarrow #1} \pagestyle{empty} \begin{document} $\mycommand{A}$ vs $\mycommand{A}[b]$ \end{document} Alternatively you can use \def and \@ifnextchar[...


15

I would use the enumitem package for this- it eases the syntax and does the heavy lifting for us The first part is to setup a new list environment \newlist{newenv}{enumerate}{5} \setlist[newenv]{label=\arabic*.} which has a default label of 1., 2., etc We then setup a new key, columns \SetEnumitemKey{columns}{before=\begin{multicols}{#1}, ...


15

You've found a dark corner in the alignment process. Let's see what happens. When starting a cell in an alignment, TeX expands tokens in order to see whether \omit appears (it's used, for example, in \multicolumn for spanning columns). The expansion of \optal[&=] is \@protected@testopt \optal \\optal {=}[&=] (note that \\optal is a single control ...


15

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 look for any sequence of tokens in the form A{x}B{y}C{z}, but the order is arbitrary, so A{x}B{y}C{z} B{y}A{x}C{z} C{z}B{y}A{x} will result in the same token ...


15

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 argument: \foo{xyz} Optional argument: \foo[abc]{xyz} \end{document} With xparse it's easier: \documentclass{article} \usepackage{xparse} \...


14

Here is a \IfNoValueOrEmpty macro: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \DeclareExpandableDocumentCommand{\IfNoValueOrEmptyTF}{mmm} { \IfNoValueTF{#1}{#2} { \tl_if_empty:nTF {#1} {#2} {#3} } } \ExplSyntaxOff \NewDocumentCommand{\mymacro}{ooo}{% \IfNoValueOrEmptyTF{#1}{\typeout{\#1 is empty}}{\typeout{\string#1 is `#1'}}%...


14

The definition of a new environment can be done by the basic LaTeX command \newenvironment. But I'd like to recommend the package xparse which provides also commands and utilities to define new environments; for instance, it provides a simple test to check if the optional argument is empty or not. \documentclass{scrreprt} \usepackage{xparse} \ExplSyntaxOn ...


14

Add an "empty" optional argument to the environment: \documentclass{article} \usepackage{enumitem,xcolor}% http://ctan.org/pkg/{enumitem,xcolor} \newenvironment{renumerate}[1][,]{\begin{enumerate}[#1]\raggedright}{\end{enumerate}} \begin{document} \begin{renumerate}[label=\color{blue}10.\theenumi] \item blah \item blah \end{renumerate} \begin{renumerate}...


14

listings' caption key allows you to use the same syntax as \caption[short title]{long title} but you have to wrap braces around the value that you pass to the caption key, in order to "hide" the closing bracket that comes after the optional short-title argument: \lstinputlisting[ % ... caption = {[Short and sweet!]Sample code from Matlab with a ...


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