# Defining default arguments for a macro?

Quick question, is there a way to define default arguments for a macro? For example, I have a page filled with these:

\TextField[width=2cm,height=10pt,bordercolor=stuff]{Question 1}
\TextField[width=4cm,height=10pt,bordercolor=stuff]{Question 2}
\TextField[width=3cm,height=10pt,bordercolor=stuff]{Question 3}
\TextField[width=5cm,height=10pt,bordercolor=stuff]{Question 4}


Can I tell it the height and bordercolor somewhere so I don't have to repeat those at every line?

• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Most packages provide global settings somehow, but it really depends on the specific case. – yo' Nov 10 '14 at 15:01
• Well it's \TextField I use.. so from the \usepackage{hyperref} – Dendory Nov 10 '14 at 15:09
• If you were to define your macro, e.g. \newcommand{\MyText}[2]{\TextField[width=2cm,height=10pt,#1,bordercolor=stuff]{#2}} I think the you could pass the heighta second time, i.e. \MyText{height=15pt}{Question 1}. I believe the last definition will be used. I can't test this now... Can you give that a try? – Markus Nov 10 '14 at 15:09
• @Dendory And what if you use amsmath as well which modifies the behaviour of \@ifnextchar, which can modify the behaviour of \TextField? As well, by not providing the MWE, you force any of the volunteers to make it themselves, which costs their time. However, it is you who want help, not them. – yo' Nov 10 '14 at 15:13
• You could also define a macro using \newcommand{\pete}[1]{\Textfield[width=2cm,height=10pt,bordercolor=stuff]{#1}} then use \pete{Question 1} – John Kormylo Nov 10 '14 at 15:41

A version with either the standard \@ifnextchar way and the more sophisticated \NewDocumentCommand from xparse package.

Please note that in the \MakeMyTextField[width=10cm]{} way, the default argument is overruled of course, as well as in \MyTextField.

A better approach would use keyval or xkeyval or (if really needed ;-)) pgfkeys

\documentclass{article}

\usepackage{xcolor}
\usepackage{xparse}%
\usepackage{hyperref}
\makeatletter

\newcommand{\MakeMyTextField@@opt}[2][width=2cm,height=10pt,bordercolor=blue]{%
\TextField[#1]{#2}%
}%

\newcommand{\MakeMyTextField@@noopt}[1]{
\MakeMyTextField@@opt{#1}%
}%

\newcommand{\MakeMyTextField}{%
\@ifnextchar[{\MakeMyTextField@@opt}{\MakeMyTextField@@noopt}
}%

\NewDocumentCommand{\MyTextField}{+O{width=2cm,height=10pt,bordercolor=blue}+m}{%
\TextField[#1]{#2}%
}%

\makeatother

\begin{document}
\begin{Form}
\begin{enumerate}
\item \MakeMyTextField{Question 1}

\item \MakeMyTextField{Question 2}

\item \MakeMyTextField[width=10cm]{Question 3}

\item \MyTextField{Question 4}
\end{enumerate}
\end{Form}
\end{document}


You can simply preset new default values; it's still possible to override them locally.

\documentclass{article}

\usepackage{xcolor}
\usepackage{hyperref}

\setkeys{Field}{height=10pt,bordercolor=green}

\begin{document}
\begin{Form}
\begin{enumerate}
\item \TextField{Question 1}

\item \TextField{Question 2}

\item \TextField[width=10cm]{Question 3}

\item \TextField[height=30pt,bordercolor=red]{Question 4}

\item \TextField{Question 5}
\end{enumerate}
\end{Form}
\end{document}


I took @Markus's comment and modified it slightly:

\newcommand{\q}[2]{\TextField[width=#1,height=10pt,bordercolor=stuff]{‌​#2}}
...
\q{3cm}{Question}