xkeyval — read keys from file












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










share|improve this question





























    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










    share|improve this question



























      5












      5








      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










      share|improve this question
















      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







      expansion key-value xkeyval






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 5 hours ago









      Phelype Oleinik

      25.9k54691




      25.9k54691










      asked 9 hours ago









      NicolaF_NicolaF_

      563




      563






















          1 Answer
          1






          active

          oldest

          votes


















          5














          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}{deftowhom{#1}}
          define@key{my@keys}{goodnews}{defwho{#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







          share|improve this answer





















          • 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
            9 hours ago













          • 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
            9 hours ago








          • 1





            @PhelypeOleinik Because a V-type argument must be a single token

            – Joseph Wright
            9 hours ago











          • @JosephWright Thanks! Some day I'll learn how to use expl3 properly :)

            – Phelype Oleinik
            9 hours ago











          • @PhelypeOleinik I took the liberty of streamlining your code.

            – egreg
            5 hours ago












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "85"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f486074%2fxkeyval-read-keys-from-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          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}{deftowhom{#1}}
          define@key{my@keys}{goodnews}{defwho{#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







          share|improve this answer





















          • 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
            9 hours ago













          • 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
            9 hours ago








          • 1





            @PhelypeOleinik Because a V-type argument must be a single token

            – Joseph Wright
            9 hours ago











          • @JosephWright Thanks! Some day I'll learn how to use expl3 properly :)

            – Phelype Oleinik
            9 hours ago











          • @PhelypeOleinik I took the liberty of streamlining your code.

            – egreg
            5 hours ago
















          5














          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}{deftowhom{#1}}
          define@key{my@keys}{goodnews}{defwho{#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







          share|improve this answer





















          • 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
            9 hours ago













          • 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
            9 hours ago








          • 1





            @PhelypeOleinik Because a V-type argument must be a single token

            – Joseph Wright
            9 hours ago











          • @JosephWright Thanks! Some day I'll learn how to use expl3 properly :)

            – Phelype Oleinik
            9 hours ago











          • @PhelypeOleinik I took the liberty of streamlining your code.

            – egreg
            5 hours ago














          5












          5








          5







          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}{deftowhom{#1}}
          define@key{my@keys}{goodnews}{defwho{#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







          share|improve this answer















          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}{deftowhom{#1}}
          define@key{my@keys}{goodnews}{defwho{#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








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 9 hours ago









          egreg

          736k8919353261




          736k8919353261










          answered 9 hours ago









          Phelype OleinikPhelype Oleinik

          25.9k54691




          25.9k54691








          • 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
            9 hours ago













          • 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
            9 hours ago








          • 1





            @PhelypeOleinik Because a V-type argument must be a single token

            – Joseph Wright
            9 hours ago











          • @JosephWright Thanks! Some day I'll learn how to use expl3 properly :)

            – Phelype Oleinik
            9 hours ago











          • @PhelypeOleinik I took the liberty of streamlining your code.

            – egreg
            5 hours ago














          • 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
            9 hours ago













          • 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
            9 hours ago








          • 1





            @PhelypeOleinik Because a V-type argument must be a single token

            – Joseph Wright
            9 hours ago











          • @JosephWright Thanks! Some day I'll learn how to use expl3 properly :)

            – Phelype Oleinik
            9 hours ago











          • @PhelypeOleinik I took the liberty of streamlining your code.

            – egreg
            5 hours ago








          1




          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
          9 hours ago







          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
          9 hours ago















          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
          9 hours ago







          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
          9 hours ago






          1




          1





          @PhelypeOleinik Because a V-type argument must be a single token

          – Joseph Wright
          9 hours ago





          @PhelypeOleinik Because a V-type argument must be a single token

          – Joseph Wright
          9 hours ago













          @JosephWright Thanks! Some day I'll learn how to use expl3 properly :)

          – Phelype Oleinik
          9 hours ago





          @JosephWright Thanks! Some day I'll learn how to use expl3 properly :)

          – Phelype Oleinik
          9 hours ago













          @PhelypeOleinik I took the liberty of streamlining your code.

          – egreg
          5 hours ago





          @PhelypeOleinik I took the liberty of streamlining your code.

          – egreg
          5 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f486074%2fxkeyval-read-keys-from-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          數位音樂下載

          When can things happen in Etherscan, such as the picture below?

          格利澤436b