I'm currently writing a tutorial for some software, and I'd like to refer to the shortcuts used in this program. These shortcuts should be notable, i.e. displayed in a different color or font style.

Basically what I'm trying to do is create a new command, with only one argument -- a string of words, separated by some delimiter (comma). Each word (or segment, since in this case words would be substrings like Ctrl, Shift, Enter or just S, O) should be displayed in the notable style (let's say \textbf{}), and between each two words there should be a + (or some other symbol) using the regular font style. Note that this isn't necessarily the same delimiter used in the argument (the comma)!

In pseudo code:

\newcommand{\shortcut}[1]{
For every word in the argument #1:
- Display in notable font style
- Add + in regular font style (except for last word)
}

So \shortcut{Ctrl,Shift,Z} should result in \textbf{Ctrl}+\textbf{Shift}+\textbf{Z}.

Any recommendations for packages to use, or can this be done using relatively simple LaTeX?

It seems more complicated, but it's really very similar. The input is split at the commas and then a cycle is performed. The first element is popped off and treated specially (not very much, actually, in this case).

Bonus With this solution you can easily print a comma, by choosing a different delimiter as optional argument. Of course also

\shortcut{Ctrl,{,}}

would have worked, but this strategy can be used also in more difficult cases.

Thanks! The first (why is this called the 'old' one by the way) solution is indeed easier to read/understand. I take it the @tempswa flag is just a predefined boolean flag? And @tempswatrue and @tempswafalse set the value to respectively true and false?
–
AilurusJun 14 '12 at 21:50

The 'new style' looks interesting too, never saw this used before (but then again I'm clearly not that experienced writing my own commands).
–
AilurusJun 14 '12 at 21:51

1

@Ailurus \if@tempswa is a "scratch conditional" defined by the LaTeX kernel for tasks like this, where its value (true or false) is needed for a short job; one should always set it to true or false before starting the job, as its value can be any.
–
egregJun 14 '12 at 22:04

One other comment: when using the 'old style' method, an additional space is added after the shortcut. This doesn't happen when I rewrite the \newcommand{} without linebreaks. Apparently I have to add a % after the if statement in your code, but why?
–
AilurusJun 14 '12 at 22:25

@Ailurus Yes, sorry, a % was missing. The end of line became part of the definition (and is converted to a space), so it was added at each call of the macro. The "new style" method doesn't suffer of this problem because all spaces and end-of-lines are ignored between \ExplSyntaxOn and \ExplSyntaxOff.
–
egregJun 14 '12 at 22:30

Very concise, thanks! But how does the \def\scsep{} part work? I don't understand why the + isn't printed the first time.
–
AilurusJun 14 '12 at 21:43

2

The first time \scsep doesn't print anything it just defines \scsep to print. It's a trick way of avoiding a + on the first time. If you look in the current associated forum you'll see @egreg scurrilously commenting it's not the first time I've used that idiom on this site:-)
–
David CarlisleJun 14 '12 at 21:53

EDIT: As @PeterGrill pointed out, this will produce a bold face +. To avoid this, as the request of the OP was (in case this is necessary), the code should be modified like this, without sacrificing any comfort: