5

I want to read xkeyval options from an external file, like:

\setkeys{my@keys}{\input{conf}}

I tried several combinations of \edef and \expandafter, but to no avail. Any ideas?

Best regards,

Nicolas

6

You can't \edef an \input because it isn't expandable. You have to first read the contents of the file to a temporary variable and then pass that to \setkeys.

Here's an expl3 implementation of \setkeysFile. It's the same as the standard \setkeys except that the second argument is a file that (should) contain a comma separated list of key-value pairs.

\RequirePackage{filecontents}
\begin{filecontents*}{conf.tex}
hello = world :D,
goodnews = everyone!,
\end{filecontents*}
\documentclass{article}
\usepackage{xkeyval}
\makeatletter
\define@key{my@keys}{hello}{\def\towhom{#1}}
\define@key{my@keys}{goodnews}{\def\who{#1}}
\makeatother

\usepackage{xparse}
\ExplSyntaxOn
\tl_new:N \l__nicolas_setkeys_tl
\NewDocumentCommand \setkeysFile { m m }
  {
    \file_get:nnNTF {#2} {} \l__nicolas_setkeys_tl
      { \exp_args:NnV \setkeys {#1} \l__nicolas_setkeys_tl }
      { \__kernel_msg_error:nnn { kernel } { file-not-found } {#2} }
  }
\ExplSyntaxOff

\begin{document}

\setkeysFile{my@keys}{conf}

Hello \towhom

Good news \who

\end{document}

this prints:

enter image description here

  • 1
    \exp_args:NnV \setkeys{#1} \l__nicolas_setkeys_tl already supplies the braces and is more correct code anyhow. The \ior_open:Nn business is useless. – egreg Apr 22 at 17:32
  • Thanks :-) But why more correct? Oh, nevermind, it's because V gets the value of a register, and a register is an N type. Am I correct? – Phelype Oleinik Apr 22 at 17:35
  • 1
    @PhelypeOleinik Because a V-type argument must be a single token – Joseph Wright Apr 22 at 17:38
  • @JosephWright Thanks! Some day I'll learn how to use expl3 properly :) – Phelype Oleinik Apr 22 at 17:43
  • 1
    Thanks, that did the trick. My texlive distrib did not have \get_file used catchfile's CatchFileEdef instead. – NicolaF_ Apr 23 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.