add tag
निरंजन
See this example -

```
\documentclass{article}
\begin{filecontents}{test.sty}
\ProvidesPackage{test}[2021-06-10 v0.1 Test package]
\RequirePackage{xkeyval}
\DeclareOptionX{foo}[foo]{\newcommand{\foo}{#1}}
\ProcessOptionsX
\end{filecontents}
\usepackage[foo=bar]{test}

\begin{document}
\foo
\end{document}
```

This currently returns bar in the output document. I want a mechanism with this package which can preset `\foo` to foobar **when option foo is not used**. Currently I have two alternatives -

1. `\usepackage[foo]{test}`; where `\foo` will output `foo`
2. `\usepackage[foo=bar]{test}` where `\foo` will output `bar`

I want a third alternative which is `\usepackage{test}` where `\foo` shall output `foobar`. How to do it?
Top Answer
samcarter
You could put in a default definition for `\foo` and then overwrite this if the package option is used:

```
\documentclass{article}

\begin{filecontents*}[overwrite]{test.sty}
\ProvidesPackage{test}[2021-06-10 v0.1 Test package]
\RequirePackage{xkeyval}
\newcommand*\foo{foobar}
\DeclareOptionX{foo}[foo]{\def\foo{#1}}
\ProcessOptionsX
\end{filecontents*}

%\usepackage[foo=bar]{test} % -> bar
%\usepackage[foo]{test} % -> foo
%\usepackage[]{test} % -> foobar
\usepackage{test} % -> foobar

\begin{document}
\foo
\end{document}
```

(Thanks @Skillmon for the optimisation with `\def`)
Answer #2
Skillmon
Another option (though inferior to [@samcarter's answer](https://topanswers.xyz/tex?q=1823#a2054)) could be to define a macro in the option, and after parsing the option list checking whether the macro is really defined (in LaTeX the macro `\providecommand` does that, check whether a macro is defined, and if it isn't sets the definition you provide there).

```
\documentclass{article}
\begin{filecontents}{test.sty}
\ProvidesPackage{test}[2021-06-10 v0.1 Test package]
\RequirePackage{xkeyval}
\DeclareOptionX{foo}[foo]{\newcommand*{\foo}{#1}}
\ProcessOptionsX

\providecommand*\foo{foobar}
\end{filecontents}
\usepackage{test}

\begin{document}
\foo
\end{document}
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.