I don't know if already exists a question like this, but after 20 minutes I haven't found one.

There are similar ones, like How to define a command that bolds the first letter of each word in the input argument?, but it's not the same as mine.

I'm looking for a command \shortenthis{…} which extracts the first letter of each word in the command. \shortenthis{Leonard Euler} will give us LE. In case of \shortenthis{Pierre de Fermat} it should give PdF.

The idea is to shorten names.

I guess this is “easy” with LaTeX3 (although I'm a newbie, I looked in the documentation, and examples in this site, and couldn't do it), but any solution would be accepted.

  • Already found an answer. If someone marks this as a duplicate, will delete it. If not, may be it's good for the community to have it here.
    – Manuel
    Mar 1 '14 at 12:15
  • Which question do you think this is a duplicate of?
    – egreg
    Mar 1 '14 at 12:16
  • I don't know, but my memory keeps saying that I have already seen something like this.
    – Manuel
    Mar 1 '14 at 12:16
  • Why don't you use some acronym facility instead of re-inventing things?
    – user11232
    Mar 1 '14 at 12:17
  • I have an answer ready, shoot if you still need it. :) Mar 1 '14 at 12:20

Almost a copy of cgnieder's solution in the link I posted.

\NewDocumentCommand\shortenthis{ m }
        \shorten_this:n { #1 }

\seq_new:N \l_shorten_seq
\cs_new_protected:Npn \shorten_this:n #1
        \seq_set_split:Nnn \l_shorten_seq { ~ } { #1 }
        \seq_map_inline:Nn \l_shorten_seq
                \tl_head:n { ##1 }

\shortenthis{Pierre de Fermat}
\shortenthis{Leonard Euler}
  • Unfortunately this does not work if the text is in a variable, e.g. \newcommand{\abc}{Abc} \shortenthis{\abc} outputs "Abc".
    – ScumCoder
    Feb 11 '19 at 23:40
  • Add \cs_generate_variant:Nn \shorten_this:n { V, o, x, e } and use any of the variants in the definition of \shortenthis, i.e. \shorten_this:V #1 (this only works for single tokens in the argument, like \abc), \shorten_this:o { #1 } (this may be the more generic), \shorten_this:x { #1 } (this expands everything), or \shorten_this:e { #1 } this expands everything too.
    – Manuel
    Feb 12 '19 at 7:09

In this answer, the macro \getargsC extracts each word of the argument into the macros \argi, \argii, etc. Then I set up a loop to grab the first letter of each word with a simple \def

    \edef\nextword{\csname arg\romannumeral\theindex\endcsname}%
\firstletters{This is a test of the Emergency Broadcast System.}

enter image description here

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.