TransWikia.com

Presetting xkeyval keys to thetitle or theauthor

TeX - LaTeX Asked by Groctel on June 22, 2021

I’m writing a LaTeX template and am almost done (!!!) but I’m having a very hard time understanding xkeyval‘s presetkeys behaviour.
I’ve managed to set some defaults, but when it comes to setting them to theauthor and thetitle the keys are set to empty strings even though they’re set after the author{} and title{} commands.

Here’s a mwe:

my_mwe.cls

RequirePackage{expl3}

ProvidesExplClass{my_mwe}
                  {2020/09/08}
                  {1.0}
                  {Minimal working example}

LoadClass{report}

RequirePackage{xkeyval}
RequirePackage{xparse}

AtBeginDocument{%
    define@key{my_mwe} {author} [] {defmy_mwe@author{#1}}
    define@key{my_mwe} {title}  [] {defmy_mwe@title{#1}}
    presetkeys{my_mwe} {author}    {author={theauthor}}
    presetkeys{my_mwe} {title}     {title=thetitle}
}

NewDocumentCommand{MWECommand}{O{}}{%
    setkeys{my_mwe}{author, title, #1}%

    Here's~the~output:
    my_mwe@title 
    my_mwe@author
}

mwe.tex

documentclass[10pt, a4paper]{my_mwe}

usepackage{titling}
    author{The poor crying author}
    title{A sad mwe}

begin{document}

MWECommand%

MWECommand[title=thetitle, author=theauthor]

end{document}

What I’m expecting is that the basic call to MWECommand does the same as the argumented one, but instead the theauthor and thetitle strings are only printed in the second one.
What am I doing wrong?

One Answer

You're explicitly setting the default value, which is used if you pass in the author or title keys without a value, to be empty with

define@key{my_mwe}{author}[]{defmy_mwe@author{#1}}

The value specified in brackets would be the default (which in this case is empty). The presetkeys macro sets an initial value, that is a value which will be used if the key is not explicitly set at all.

Now in your macro you put author, title, #1 in your setkeys, so the default not the initial values will be used. And the defaults are empty.

So the easiest way would be to remove author, title from your definition (I've also removed the empty line in the definition, as I doubt you want to have a par there, which would be the outcome):

RequirePackage{expl3}

ProvidesExplClass{my_mwe}
                  {2020/09/08}
                  {1.0}
                  {Minimal working example}

LoadClass{report}

RequirePackage{xkeyval}
RequirePackage{xparse}

AtBeginDocument{%
    define@key{my_mwe} {author} [] {defmy_mwe@author{#1}}
    define@key{my_mwe} {title}  [] {defmy_mwe@title{#1}}
    presetkeys{my_mwe} {author}    {author={theauthor}}
    presetkeys{my_mwe} {title}     {title=thetitle}
}

NewDocumentCommand{MWECommand}{O{}}{%
    setkeys{my_mwe}{#1}%
    Here's~the~output:
    my_mwe@title 
    my_mwe@author
}

Correct answer by Skillmon on June 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP