Tag Info

Hot answers tagged

45

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


39

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.


30

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


29

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


23

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


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


23

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


22

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


21

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


20

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


20

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


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


18

\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 #2= one #3= two #4= three ...


18

\usepackage{twoopt} \newcommandtwoopt{\xyz}[3][Def1][Def2]{Something with #1, #2 and #3} There are also \renewcommandtwoopt and \providecommandtwoopt. However the xparse package (part of the LaTeX3 package tree, but works also with LaTeX2e) provides \DeclareDocumentCommand that gives great flexibility in defining commands with optional arguments in every ...


18

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


17

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


17

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


17

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


16

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


16

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.


16

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


15

You can't accomplish this with one \newcommand. There you can define one optional parameter, and it has to be the first one. There is the twoopt package that defines \newcommandtwoopt for two optional parameters, but I don't think it can handle what you want to achieve. I first thought you would need to use TeX's \def, but here's a solution that's completely ...


14

Typically {} are required arguments while [] are optional ones. Read more about that wherever you learn to define commands, for example here. When confused, it probably is {}. Are there some cases that seem especially odd? Note that TikZ really don't count as they are parsed totally differently (though with similar mnemonics).


14

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


14

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


14

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


13

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


13

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


13

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


13

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



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