4

This seems like a very basic question and I'm sure it's been answered somewhere, but I can't find anything on it. The closest I've gotten is this question, but neither of the solutions in the accepted answer help my code.

I'm trying to branch based on the value of a namespace parameter. If I replace the \namedordefaulteraparameter{#1}{formatdecoration} with a literal yes, I get "y" as I'd expect, and just printing the value of that macro also gives me the "yes" it should. However, when I pass the macro to the test, I always get "n". I'm guessing that this is due to \doifsamestringelse testing the values without expanding them, but no matter how many \expandafters I use or where I put them, I can't get the first result to be included. What am I missing?

\definenamespace[era][
    type=module,
    name=era,
    command=yes,
    setup=yes,
]

\setupera[
    formatdecoration=yes,
]

\define[2]\namedordefaulteraparameter{%
    \doifsomethingelse
        {\namederaparameter{#1}{#2}}
        {\namederaparameter{#1}{#2}}
        {\eraparameter{#2}}%
}

\def\eradecorated[#1]#2{%
    \doifsamestringelse{yes}{\namedordefaulteraparameter{#1}{formatdecoration}}
        {y}
        {n}%
}

I know I could do this pretty easily in Lua, but for something so simple, I don't see any reason to add that extra syntax.

3

There are several flaws in your example:

  1. \doifsamestringelse will detokenize both strings and then comapare the result. You have to use \doifelse instead.
  2. \namedordefaulteraparameter is not expandable, but for \doifelse to ever be true both operands must expand to the same result.
  3. \doifsomethingelse is not expandable. You have to use the expandable alternative \expdoifelse.

Implementing an expandable test and making everything in between expandable as well, we are ready to go.

\definenamespace[era][
    type=module,
    name=era,
    command=yes,
    setup=yes,
]

\setupera[
    formatdecoration=yes,
]

\def\namedordefaulteraparameter#1#2{%
    \expdoifelse{\namederaparameter{#1}{#2}}{}%
        {\eraparameter{#2}}%
        {\namederaparameter{#1}{#2}}%
}

\def\eradecorated[#1]#2{%
    \expdoifelse{yes}{\namedordefaulteraparameter{#1}{formatdecoration}}%
        {y}%
        {n}%
}

\starttext

\eradecorated[era]{}

\stoptext
  • 1
    You can replace \define[2]\... with \defineexpandable[2]\... to create a expandable command. – Wolfgang Schuster May 21 '17 at 8:47
  • @Metafox Which is probably equivalent to \def...#1#2. But thanks for the suggestion. – Henri Menke May 21 '17 at 8:53
  • Thanks! It's all working now. The only question that leaves me with is whether there's anywhere that explains all that: the issue with \define is touched upon on its wiki page, but the only place I saw \expdoifelse even mentioned was in the release notes, and that doesn't include any description of what it's for. Are there some other docs I should be looking at, or do I just have to read through the mailing list or something? – WovenTales May 21 '17 at 16:19
  • @WovenTales The matter with expandability is more fundamental than ConTeXt. To learn about it you need to read The TeXbook (the source is available on CTAN). It is definitely worth it! – Henri Menke May 21 '17 at 21:40
3

I am not sure why you are defining \namedordefaultparameter. The standard way to fall back on a default argument is as follows:

\definenamespace
  [era]
  [
    type=module,
    name=era,
    command=yes,
    setup=yes,
    parent=era, % NOTE: Added this!!
  ]

\setupera
  [
    formatdecoration=yes,
  ]

\defineera[test1][formatdecoration=no]
\defineera[test2]

\starttext

\namederaparameter{test1}{formatdecoration}
\namederaparameter{test2}{formatdecoration}


\stoptext

which gives

no yes

Based on this, you can define \eradecorated macro as follows:

\def\eradecorated[#1]%
    {\doifelse{\namederaparameter{#1}{formatdecoration}}{yes}
      {y}{n}}

and use it as

\eradecorated[test1]
\eradecorated[test2]

which gives

n y

Note that, strictly speaking, the above macro is not equivalent to the one that you defined. If I set \defineera[test2][formatdefinition=], then my macro gives the result n n, while you macro still gives the result n y. I don't know whether you care about the difference or not.

  • I could have sworn that I tried that and found it didn't fall back on the default for some reason. You're right though, it's working now. Thanks for the catch! And I had a note that I needed to fix my macro to give the equivalent of n n, so that difference is actually desirable. – WovenTales Jun 5 '17 at 6:56

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.