3

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.

Manuel
  • 27,118
  • 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 01 '14 at 12:15
  • Which question do you think this is a duplicate of? – egreg Mar 01 '14 at 12:16
  • I don't know, but my memory keeps saying that I have already seen something like this. – Manuel Mar 01 '14 at 12:16
  • Why don't you use some acronym facility instead of re-inventing things? –  Mar 01 '14 at 12:17
  • I have an answer ready, shoot if you still need it. :) – Paulo Cereda Mar 01 '14 at 12:20
  • @PauloCereda Already posted mine (almost a copy of cgnieder solution in the question I linked). But if you have something post it, I will accept any other answer but mine. – Manuel Mar 01 '14 at 12:25
  • @HarishKumar I don't know. I thought it was more advanced and I don't require anything else than I posted. May be it's easier, but I thought it wasn't worth it learning how to use it. – Manuel Mar 01 '14 at 12:26
  • No you are wrong. :) It is easy an worthy spending time on. –  Mar 01 '14 at 23:38

2 Answers2

5

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

\documentclass{scrartcl}
\usepackage{xparse}
\ExplSyntaxOn
\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 }
            }
    }

\ExplSyntaxOff
\begin{document}
\shortenthis{Pierre de Fermat}
\shortenthis{Leonard Euler}
\end{document}
Manuel
  • 27,118
  • 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 07:09
4

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

\documentclass{article}
\usepackage{readarray}
\usepackage{ifthen}
\newcounter{index}\setcounter{index}{0}
\def\firstletters#1{%
  \getargsC{#1}%
  \whiledo{\theindex<\narg}{%
    \stepcounter{index}%
    \edef\nextword{\csname arg\romannumeral\theindex\endcsname}%
    \expandafter\getfirst\nextword\relax%
  }%
}
\def\getfirst#1#2\relax{#1}
\begin{document}
\firstletters{This is a test of the Emergency Broadcast System.}
\end{document}

enter image description here