# Can't redefine \val from polyglossia

At some recent package update (not sure which one), polyglossia seems to define \val to contain the value given to the variant key.

In one of my documents I have defined \val, which I don't want to rename because I used it several times in my document. But polyglossia override my definition of \val. I tried with \AtBeginDocument but this doesn't work.

\documentclass{article}

\usepackage{polyglossia}
\setmainlanguage[variant=british]{english}

\show\val

\AtBeginDocument{
\DeclareDocumentCommand{\val}{}{some new definition} % Does not work
}
\begin{document}
\val % prints british
\end{document}

• you need \AfterEndPreamble. – Ulrike Fischer Feb 9 '20 at 20:52
• @UlrikeFischer That worked. Care to write that as an answer? – Simon Feb 9 '20 at 21:23
• The problem is in the usage of xkeyval. I'll open an issue. – egreg Feb 9 '20 at 21:31
• See github.com/reutenauer/polyglossia/issues/390. Every time a choice key is evaluated, \val and \nr are redefined. So doing the definition of \val at begin document is not really safe. – egreg Feb 9 '20 at 21:41
• On the other hand, it's quite likely that the issue will appear again whenever a package using choice keys from xkeyval is loaded and a choice is performed, because of bad documentation of the package that leads people to think that \val\nr is mandatory usage. – egreg Feb 9 '20 at 21:46

## 1 Answer

You're in a tough situation, which stems from history.

The few examples in the documentation of xkeyval for \define@choicekey all use [\val\nr] and this has instilled the idea that these are mandatory tokens for this case.

They aren't. Any two control sequences are allowed and they are used as scratch macros. Package writers should use their own control sequences; so the example in the documentation

\define@choicekey*{fam}{align}[\val\nr]{left,center,right}{%
\ifcase\nr\relax
\raggedright
\or
\centering
\or
\raggedleft
\fi
}


adapted to the package foo should better be

\define@choicekey*{fam}{align}[\foo@val\foo@nr]{left,center,right}{%
\ifcase\foo@nr\relax
\raggedright
\or
\centering
\or
\raggedleft
\fi
}


and the code in the polyglossia definition files should be

\define@choicekey*+{welsh}{date}[\xpg@val\xpg@nr]{long,short}[short]{%
\ifcase\xpg@nr\relax
% long:
\welsh@formaldatetrue
\or
% accented:
\welsh@formaldatefalse
\fi
\xpg@info{Option: Welsh, date=\xpg@val}%
}{\xpg@warning{Unknown date value #1'}}


Unfortunately, most packages or classes using xkeyval and choice keys adopted \var\nr. These scratch control sequences are not set inside a group, so any time one performs a choice key using them they are redefined and their final value is unpredictable.

What can you do? Avoid \val as the name of a personal macro.

I filed a issue report for polyglossia and I'll do what's possible to inform authors of other packages. But it will take long until the situation improves.

• uff. I'm using [\@tempval\@tempnr]` in chessboard - which isn't completly perfect but not too bad either ;-) – Ulrike Fischer Feb 9 '20 at 22:59
• @UlrikeFischer You're among the few who understood the package, I guess. – egreg Feb 9 '20 at 23:31
• I probably simply had the healthy idea not to use document commands and so tried it. But I'm not quite sure about the numbers - I can see quite a number of packages using \val\nr (among these datatools and \datetime) but finding the packages avoiding it is a bit more challeging. – Ulrike Fischer Feb 9 '20 at 23:42