1

I would like to make a command that produces the following results

\myquantity[Q]
\myquantity[Q][i]
\myquantity[Q][][j]
\myquantity[Q][i][j]
\myquantity[Q][i][j][\bar]
\myquantity[Q][][][\bar]
\myquantity[Q][][][\tilde]
\myquantity[Q_c][i][j][\bar]

Result

My current solution is the following

\newcommandx{\myquantity}[4][1=, 2=, 3=, 4=]{
  \des[#4]{#1}{#3}(\lambda_{#2})}  

where \des is the command in the supplement of this answer.

Is there a simpler way to obtain the same result?

  • If you really want this use xparse. But 4 optional arguments are probably not the best markup. – TeXnician Aug 3 '18 at 7:12
  • 1
    Did you redefine \lambda for that to work? If so, why? – Skillmon Aug 3 '18 at 7:28
  • If one of the answers below solves your problem, could you mark that one as accepted? Also see What should I do when someone answers my question?. – Max Aug 4 '18 at 13:38
  • @Skillmon It was a typo. I edited the code now. – Simone Gaiarin Aug 5 '18 at 14:25
  • @TeXnician Using newcommandx is something I understand as opposite to writing the intricated code proposed in the other answers, that is why I choose it. How can I let the first argument not be optional? – Simone Gaiarin Aug 5 '18 at 14:27
6

I would like to argue that having four optional arguments is not very semantic. I would use a command with a key=value optional argument.

\myquantity[Q_c][i][j][\bar]

is a lot less readable than

\myquantity[accent=\bar,subscript={c,j},lambda subscript=i]{Q}

although the latter might be using a bit too long keys, I hope you agree it is easier to understand.

The following MWE gives an example of your desired command, using a key-value argument. If you indeed think the keys are too long, you can always change them what you find descriptive enough.

\documentclass{article}

\usepackage{amsmath}
\usepackage{pgf}

\makeatletter
\newif\if@myquantity@subscript
\newif\if@myquantity@lambdasubscript
\pgfkeys{
    my quantity/.cd,
        accent/.code={\let\@myquantity@accent#1},
        accent=\relax,
        subscript/.code={\def\@myquantity@subscript{#1}\@myquantity@subscripttrue},
        lambda subscript/.code={\def\@myquantity@lambdasubscript{#1}\@myquantity@lambdasubscripttrue},
}
\newcommand{\myquantity}[2][]{%
    \@myquantity@subscriptfalse
    \@myquantity@lambdasubscriptfalse
    \pgfkeys{my quantity/.cd,#1}%
    \if@myquantity@subscript%
        \@myquantity@accent{#2}_{\@myquantity@subscript}%
    \else%
        \@myquantity@accent{\mathbf{#2}}%
    \fi%
    (\lambda%
    \if@myquantity@lambdasubscript
        _{\@myquantity@lambdasubscript}%
    \fi)%
}
\makeatother

\begin{document}
    \begin{equation*}
        \begin{array}{lll}
            \myquantity{Q} &
            \myquantity[lambda subscript=i]{Q} &
            \myquantity[subscript=j]{Q}\\
            \myquantity[subscript=j,lambda subscript=i]{Q} &
            \myquantity[accent=\bar,subscript=j,lambda subscript=i]{Q} &
            \myquantity[accent=\bar]{Q} \\
            \myquantity[accent=\tilde]{Q} &
            \myquantity[accent=\bar,subscript={c,j},lambda subscript=i]{Q}
        \end{array}
    \end{equation*}
\end{document}

Resulting in

enter image description here

  • Thanks for the answer. Where can I find a reference of the commands used in your answer? – Simone Gaiarin Aug 5 '18 at 14:29
  • @SimoneGaiarin the keys can be defined with the pgfkeys package which is automatically loaded when you're using Tikz or pgf. You can find more information on the key management in chapter 82 (page 875) of the Tikz/PGF manual . – Max Aug 5 '18 at 14:41
4

Four optional arguments require too much care in input. Besides, one of them is mandatory and the last one refers to the mandatory argument, so it should be specified first.

My idea is

\myq[<optional decoration>]{<variable>}[<optional coordinate>](<optional subscript)

so there is no need to specify empty optional arguments.

What do the macros do? First I look for _ in the optional argument, because this should not be part of \mathbf when no coordinate appears. For this purpose, \regex_split:nnN is available: if no _ is found, the sequence will have just one item, otherwise it will have two (what comes before _ and what comes after).

Then it's a matter of checking whether the coordinate is specified, in order to decide for \mathbf or not.

The <decoration> should be a math accent command; it defaults to \use:n, which means “output the argument” (the classical \@firstofone).

\documentclass{article}
\usepackage[leqno,fleqn]{amsmath} % the options are just to better show the output
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\myq}{
  O{\use:n} % decoration for the main variable
  m         % main variable
  o         % coordinate
  d()       % subscript to \lambda
 }
 {
  % separate off the possible subscript to the variable;
  % item 1 contains the variable, item 2 the subscript
  \regex_split:nnN { \cD_ } { #2 } \l_simone_myq_var_seq
  % make the subscript
  \int_compare:nTF { \seq_count:N \l_simone_myq_var_seq = 1 }
   {% no subscript in #2
    \IfNoValueTF{#3}
     { #1 { \mathbf{#2} } }
     { #1 { #2 } \sb { #3 } }
   }
   {% subscript in #2
    \IfNoValueTF{#3}
     {
      #1 { \mathbf{\seq_item:Nn \l_simone_myq_var_seq { 1 }} } % base
      \sb { \seq_item:Nn \l_simone_myq_var_seq { 2 } }
     }
     {
      #1 { \seq_item:Nn \l_simone_myq_var_seq { 1 } } % base
      \sb { \seq_item:Nn \l_simone_myq_var_seq { 2 } , #3 }
     }
   }
  ( \lambda\IfValueT{#4}{\sb{#4}} )
 }

\seq_new:N \l_simone_myq_var_seq

\ExplSyntaxOff

\begin{document}

\begin{gather}
\myq{Q} \\
\myq[\bar]{Q} \\
\myq{Q}[i] \\
\myq{Q}(j) \\
\myq{Q}[i](j) \\
\myq[\tilde]{Q}(j) \\
\myq[\tilde]{Q}[i](j) \\
\myq{Q_c} \\
\myq[\bar]{Q_c} \\
\myq{Q_c}[i] \\
\myq{Q_c}(j) \\
\myq{Q_c}[i](j) \\
\myq[\tilde]{Q_c}(j) \\
\myq[\tilde]{Q_c}[i](j)
\end{gather}

\end{document}

enter image description here

  • Thanks for the answer. Where can I find a reference of the commands used in your answer? – Simone Gaiarin Aug 5 '18 at 14:30
  • @SimoneGaiarin texdoc expl3 and texdoc interface3 – egreg Aug 5 '18 at 14:47

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.