1

This sounds like some basic stuff, and yet I cannot manage to find a solution, packaged or native.

I have documents with people who have from 2 to 4 names (typical Danish name could be Søren Jens Sørensen Hjortshøj).

Can someone suggest a command that would extract the initials (first letter) of each of those words ? I know of the package xstring to extract the first letter. The trouble is looping on the words, as I don't know the number in advance.

So the command \initials would act like this :

\NewDocumentCommand{\initials}{m} {
Some code
}
...
\ingenior{Søren Jens Sørensen Hjortshøj}

....

\initials{\ingenior} is SJSH.

  • 2
    \initials{\author} will never work, unless you completely redefine \author. – campa Mar 07 '24 at 08:28
  • @campa : why is that ? Could I use another variable that is not part of the LaTeX standard ?

    (I tend to define \ingenior{Søren Sørensen}, I can use that \ingenior everywhere and then later I assign \author{\ingenior})

    – 22decembre Mar 07 '24 at 08:31
  • @22decembre, if you just have a handful of names, just code as many \newcommand-s to just supply the initials, which you code by hand. – MS-SPO Mar 07 '24 at 08:33
  • Usually \author is the command to set the author (usually by redefining the internal \@author). \author never returns the author. So \initials{\author} does not make sense. – cabohah Mar 07 '24 at 09:00
  • @22decembre The \author command doesn't contain the author name but set the variable \@author. You should rewrite your example with \initials{\ingenior}. – jlab Mar 07 '24 at 09:02
  • @22decembre Take a look how \author been defined. Try \ShowCommand\author and check the log file, the result is > \author =macro: #1->\gdef \@author {#1}. – Tom Mar 07 '24 at 09:06
  • 1
    I modified my request as to not refer to \author. – 22decembre Mar 07 '24 at 09:09
  • How do you recommend the initials for Jean le Rond D'Alembert be written or Jan van Riebeeck? You should be more specific for what you want to achieve. – yannisl Mar 07 '24 at 09:47

1 Answers1

1

This is a modified version of my answer at https://tex.stackexchange.com/a/705530/. The idea is to create a recursive function that prints the first character, splits the string on the first space, and calls itself on the rest if the rest is not empty.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xstring}

\NewDocumentCommand{\initials}{m}{% \StrLeft{#1}{1}% % print first character \StrBehind{#1}{ }[\rest]% % copy everything after first space into \rest \IfStrEq{\rest}{}{}{% % if rest is empty (first {}) do nothing (second {}), % which will stop the recursive loop. \initials{\rest}% % else: call the function again on the rest }% }

\def\ingenior{Søren Jens Sørensen Hjortshøj}

\begin{document}

The initials of \ingenior\ are \initials{\ingenior}.

\end{document}

Result:

enter image description here

Marijn
  • 37,699
  • this will break if the name starts with a non-ascii char, try e.g. with Émile. – Ulrike Fischer Mar 07 '24 at 09:58
  • @UlrikeFischer Also initials for Jean le Rond D'Alembert or Jan van Riebeeck! At minimum one should give an optional parameter. Also better to move to l3 which can handle strings and iteration better. Names are cultural specific so a method to handle exceptions should be provided. – yannisl Mar 07 '24 at 10:04
  • There is a serious chance that a dane will have a non-ascii character as an initial (ø,æ and å are regular letters of the danish alphabet, they have correspondences in other nordic languages.) – 22decembre Mar 07 '24 at 10:10
  • @Ulrike It didn't break, I tried (with pdfLaTeX) \def\ingenior{Émile Søren Jens Sørensen Hjortshøj} and I got the correct result ÉSJSH. – Marijn Mar 07 '24 at 11:03
  • @22decembre also \def\ingenior{Øren Åjens Sørensen Hjortshøj} and \def\ingenior{Æmile Øren Åjens Sørensen Hjortshøj} work fine and produce the correct initials, in pdfLaTeX as well as XeLaTeX and LuaLaTeX. – Marijn Mar 07 '24 at 11:07
  • Not with pdflatex, sorry. That can't be. – Ulrike Fischer Mar 07 '24 at 11:57
  • @UlrikeFischer sorry to disappoint you but I'm looking at the result right now :) Maybe a version difference? I used This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2022/dev/Debian) (preloaded format=pdflatex) and Package: xstring 2021/07/21 v1.84. – Marijn Mar 07 '24 at 12:29
  • @UlrikeFischer it does fail with even more exotic characters though, such as āēīǎ, as noted in https://tex.stackexchange.com/a/646864/. – Marijn Mar 07 '24 at 12:36
  • your system is too old ... In a current LaTeX utf8 chars are protected and it won't work. – Ulrike Fischer Mar 07 '24 at 13:28
  • @UlrikeFischer so your system is too new then :) – Marijn Mar 07 '24 at 14:02
  • Actually, I use LuaLaTeX and it works.

    For all I care, it's enough as of now.

    – 22decembre Mar 07 '24 at 14:48
  • @22decembre even with lualatex you can get problems if the initial uses an combining accent. – Ulrike Fischer Mar 07 '24 at 14:55