The xparse
package allows for some really cool syntax stuff.
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\funcF{d()}{%
\IfValueTF{#1}{f(#1)}{f}}
% This is a version that follows more popular LaTeX syntax conventions.
\NewDocumentCommand\NormalFuncF{o}{%
\IfValueTF{#1}{f(#1)}{f}}
\begin{document}
\[ \funcF(2) = 4 \]
\[ \funcF \]
\[ \NormalFuncF[2] = 4 \]
\[ \NormalFuncF \]
\end{document}
Attention: there is an error in your code. You missed closing brace }
before [2]
.
The code
\newcommand{\minitab}[2][c]{\begin{tabular}{#1}#2\end{tabular}}
defines the command \minitab
which has two arguments.
The 1st is optional and has default value c
(which will be used if you don't use another value). It is used to determine the alignment of the single column of tabular
. So it could be also r
ight or l
eft.
The second argument is obligatory and it is in fact the content for the tabular
.
So you can use the command as
\minitab{foo and foo}
or
\minitab[l]{foo and foo}
Recall that options for a command are passed in brackets and mandatory arguments in braces. \command[<optional>]{<mandatory>}
.
Edit:
command name: \minitab
number of arguments: [2]
(so the #1
is optional; the #2
is mandatory)
default value for #1
: [c]
(will be overridden if you pass another valid value)
what to do with the arguments? use #2
inside the tabular
as its contents; use #1
for the column alignment in tabular
.
(!) so you have to use {#1}
in the definition. Also, for example, if you want to insert a right border you should use {#1|}
. The braces are important to group.
Recall that the right way to use a tabular
is:
\begin{tabular}{cols}
content...
\end{tabular}
In resume: \minitab[l]{a lot of text}
will be the same as
\begin{tabular}{l}
a lot of text
\end{tabular}
Best Answer
You could use the
xparse
package which offers great possibilities for defining user commands:My First attempt:
Egregs simpler solution with
IfValueT
m
stands for mandatory argument,o
for optional.IfValueT{argument}{true code}
checks if the argument was given and calls eithertrue code
or does nothing.IfNoValueTF{argument}{true code}{false code}
checks if the argument was not given and calls either {true code} or {false code}Result (You should maybe add a negative math space
\!
if you have an A matrix.:\mymat{A}[\!\top]
):