3

The usrguide section 2.9 outtlines the =short text method for picking up optional arguments as either keys or text, such as the short toc title in commands like caption etc. I need some code to grab these arguments and put them in either booleans or macros. There is a case that does not seem to be covered by the method provided, how to capture both the short title text and a set of keys, as for example:

\mycaption[=short title{,key1,key2=value etc}]{A very long title}

This type of to work both with and without a star. With a star I will provide a key to save or not save the short title in the toc. I would appreciate some help here. In the code below, the red result is the short title. It reacts correctly (never had any doubt!), except in the combined case of having a short title and some option key-list.

\documentclass{article}
\usepackage{xcolor}
\def\savetitle#1{
\def\savedtitle{{\bfseries #1}}
}
\def\saveshorttitlekeyval#1{
 \def\savedshorttitleaux##1=##2;{{\color{red}##2}}
  \def\savedshorttitle{
    \savedshorttitleaux#1;
  }
}
\ExplSyntaxOn
\NewDocumentCommand
\mycaption{s ={short-mytext} +O{#3} +m }
{%
  \IfBooleanTF{#1}{
  {hasstar ~} \saveshorttitlekeyval{#2} 
  }{nostar~   \saveshorttitlekeyval{#2}}
  
  \savetitle{#3}
 }
 \ExplSyntaxOff
\begin{document}
1. \mycaption[=some short title]{long title}\savedtitle  \savedshorttitle

2. \mycaption[some short title]{long title}\savedtitle  \savedshorttitle

3. \mycaption[={size=10pt,color=blue}]{long title}\savedtitle \savedshorttitle

4. \mycaption{Only my caption}\savedtitle       \savedshorttitle             

5. \mycaption*{Starred my caption}\savedtitle   \savedshorttitle            % 

6. \mycaption*{Starred short title and long title} \savedtitle \savedshorttitle

\end{document}
10
  • 'Sheep and goats' - either you have a non-keyval short value, or a full set of keyvals where you give the short title explicitly with a key - I'm guessing this does not come across in the examples?
    – Joseph Wright
    Commented Aug 15 at 13:18
  • @JosephWright I need both sheep for the kids and goats to make halloumi cheese! It sort of came through in the examples in the guide, but I was hoping if someone can add some extra parsing.
    – yannisl
    Commented Aug 15 at 13:25
  • 1
    why not simply document to the user that that is the input? why do you need a different input to parse to that? Commented Aug 15 at 13:32
  • 2
    I would rather provide an extra key short that is exactly what this system does you define a key say short and for all internal processing treat it as a normal keyval ist,. but (for legacy compatiblity) if the option has no key=value then the whole option is read as short=the value Commented Aug 15 at 13:38
  • 1
    you could treat an unknown key as a value to pass to short, but you can't then pick up if a user actually tries to use an unknown key. (this doesn't have anything to do with the = syntax.)
    – cfr
    Commented Aug 15 at 14:46

2 Answers 2

3

The = is just part of the declaration, not the user syntax. The intention is that the command definition always sees a keyval list whether or not the user explicitly uses keyval.

Here if no key is used short= is assumed.

enter image description here

\documentclass{article}
\usepackage{xcolor}


\ExplSyntaxOn
\keys_define:nn{abc}{
  short .tl_set:N =\savedshorttitle,
  key1 .tl_set:N =\keyone,
  size .tl_set:N =\keysize,
  color .tl_set:N =\keycolor
  }
\NewDocumentCommand
\mycaption{s ={short} O{#3}  +m }
{%
  \def\savedtitle{#3}
  \keys_set:nn{abc}{#2}
  \IfBooleanTF{#1}
  {hasstar ~ }
  {nostar~}
  }
 \ExplSyntaxOff
\begin{document}

1. \mycaption[some short title]{long title}\savedtitle,  \savedshorttitle

2. \mycaption[size=10pt,color=blue]{long title}\savedtitle, \savedshorttitle

3. \mycaption[short=something,size=10pt,color=blue]{long title}\savedtitle, \savedshorttitle

4. \mycaption{Only my caption}\savedtitle,      \savedshorttitle             

5. \mycaption*{Starred my caption}\savedtitle   \savedshorttitle            % 

6. \mycaption*{Starred short title and long title} \savedtitle, \savedshorttitle

\end{document}
4

This is by-design. The ltcmd syntax is there to allow existing usage

\section[A short title]{A much longer title that we don't always want}

while if you are adding keys then this also covers the sort title

\section[short-title = A short title, label = mysec, include-in-toc = true]
  {A much longer title that we don't always want}

As such the expectation is that users will either have unmodified input that does not know about keys, or updated input that does: not some halfway situation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .