# “Optional” package accept variable argument

\documentclass[12pt]{article}
\usepackage[test]{optional}

\begin{document}
\def\optname{test}
\optname  \\  % Prints "test"
\opt{\optname}{
TEST    % does nothing
}\\
\opt{test}{
TEST 2    % behaves as expected
}
\end{document}


The above minimal example uses the "optional" package to compile different Versions of the same document. Now I'd like to expand it that the \opt command uses a predefined variable to determine in which version which part of the document gets compiled. The Problem is, that it does not accept the variable. I feel it's some kind of expansion issue, but i haven't found any solution yet.

• Welcome to TeX.SX! Please make your example compilable. – TeXnician Nov 19 '17 at 11:19

The argument of \opt can be expanded by \expandafter. Then the macro will see test directly instead of the option hidden in the macro \optname:

\documentclass[12pt]{article}
\usepackage[test]{optional}

\begin{document}
\def\optname{test}
\optname  \\  % Prints "test"
\expandafter\opt\expandafter{\optname}{
TEST
}\\
\opt{test}{
TEST 2    % behaves as expected
}
\end{document}