-1

I intended to have a command to print a coloured QED using a user-defined symbol. The advice is not to make commands with two consecutive optional arguments. What is one to do, is there a neat way to accomplish this ?

\documentclass[a4paper,12pt]{article}

\usepackage{xcolor}

\ExplSyntaxOn

\definecolor{dblue}{RGB}{0,0,139} \colorlet{celestk}{dblue}

\NewDocumentCommand{\wvQed}{O{celestk}oO{$\nabla$}} { \begingroup \IfNoValueTF{#2}{\color{#1}}{\color[#1]{#2}} \hfill{#3} \endgroup }

\ExplSyntaxOff

\begin{document}

Fundador is the Spanish Brandy \wvQed[$\alpha$]

\end{document}

Miyase
  • 2,544
  • 2
  • 12
  • 26
  • 1
    Yet another account? Why? Celest is not even suspended. – cfr Oct 16 '23 at 02:45
  • 1
    Your command can't be called in a way which specifies a non-default symbol without specifying all three arguments. This is why you were advised not to create commands with two consecutive optional arguments. – cfr Oct 16 '23 at 02:49
  • 4
    If you stuck to the account you were using, I'd answer, but I don't see the point in answering here. – cfr Oct 16 '23 at 03:40
  • If it helps, multiple adjacent options are functionally mandatory. A set of options implies usage of key-value pairs. – Cicada Oct 16 '23 at 05:28

1 Answers1

2

What would key-value pairs look like?

example

Note that, sensibly, the settings persist across incantations if not overwritten.

MWE

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\setmainfont{NotoSerif}

\definecolor{dblue}{RGB}{0,0,139} \colorlet{celestk}{dblue}

\ExplSyntaxOn

\tl_new:N \l_wvqed_model_tl \tl_new:N \l_wvqed_colour_tl \tl_new:N \l_wvqed_symbol_tl

\keys_define:nn { wvqed } {

colour-model .tl_set:N = \l_wvqed_model_tl,
colour-model .default:n = {},
colour-model .initial:n = {},

colour .tl_set:N = \l_wvqed_colour_tl,
colour .default:n = {celestk},
colour .initial:n = {celestk},

symbol .tl_set:N = \l_wvqed_symbol_tl,
symbol .default:n = {$\nabla$},
symbol .initial:n = {$\nabla$},

}

\NewDocumentCommand { \wvQED } { o } {

    \tl_if_novalue:nF{#1}
    { \keys_set:nn { wvqed } { #1 } } 

    \group_begin:

        \tl_if_empty:NTF
            \l_wvqed_model_tl
            {
                \color { \l_wvqed_colour_tl }
            }
            {
                \color [ \l_wvqed_model_tl ] { \l_wvqed_colour_tl }
            }

        \hfill
        \l_wvqed_symbol_tl

    \group_end:

}

\ExplSyntaxOff

\begin{document}

$x=y$ \wvQED

$x_2$ \wvQED[colour=red]

$x_3$ \wvQED[colour={0,200,200},colour-model=RGB]

$x_4$ \wvQED[symbol={\Large !!}]

Fundador is the Spanish Brandy \wvQED[symbol=$\alpha$]

Fundador is the Spanish Brandy \wvQED[colour=black,colour-model={}]

\end{document}

Cicada
  • 10,129
  • This is clearly the nicest solution. Anyways, you can also use different delimiters... o d() d<> for example (was it d? Can't check right now...). – Rmano Oct 16 '23 at 05:48
  • 1
    @Rmano using non-standard delimiters is discouraged. () should be used to give coordinate-arguments (optional or mandatory), and <> is usually just beamer-overlays. Both is not the case here, they should all three use [] from a semantic viewpoint. – Skillmon Oct 16 '23 at 07:34
  • @Skillmon yes, I know, but it's fun ;-). I use () to show the anchor positions in component descriptions, for example... – Rmano Oct 16 '23 at 07:58
  • 2
    "Note that, sensibly, the settings persist across incantations if not overwritten" that's actually not that sensible, imho, because now your macro is depending on the previous usage, which might change when the document is edited. I'd include the \keys_set:nn in the group. If you want to change the settings outside the macro, do so with a second setup macro. – Skillmon Oct 16 '23 at 09:25
  • @Skillmon Yes. Out of curiosity, what would a style use-case be for having a document element in >1 styles? (Alternative phrasing, same question: per document, should there not be one and only one setup step (or macro-wrapper)?) Similar to a font setup, I suppose. Grouping is good for independent styling. The design presumed one style (or macro wrapper) per logical document element (but, then, there would be no (logical) need to re-initialise it. (A meta-document would have different styles,though.) – Cicada Oct 17 '23 at 03:43
  • @Cicada assume you have examples and proofs. In proofs you want the default symbol, in examples you want a red star. Because of that you use \newcommand\exampleqed{\wvQED[symbol=$*$, color=red]} and now for some reason your proofs have a different QED symbol than you wanted... Never assume users won't break things! – Skillmon Oct 17 '23 at 06:27