Extract first letter of each word, also after a special character like a dash
The datatool
package provides \DTLinitials
. For example:
\documentclass{article}
\usepackage{datatool-base}
\begin{document}
\DTLinitials{This is a test of the Emergency Broadcast System.
This-Test. for sample. This T.}
\end{document}
This automatically inserts a period after each initial, but that can be prevented by redefining \DTLafterinitials
, \DTLbetweeninitials
and \DTLafterinitialbeforehyphen
to do nothing.
\documentclass{article}
\usepackage{datatool-base}
\renewcommand*{\DTLbetweeninitials}{}
\renewcommand*{\DTLafterinitials}{}
\renewcommand*{\DTLafterinitialbeforehyphen}{}
\begin{document}
\DTLinitials{This is a test of the Emergency Broadcast System.
This-Test. for sample. This T.}
\end{document}
If you need the initials in an expandable context, you first need to use \DTLstoreinitials
, which will save the initials in the command provided in the second argument:
\DTLstoreinitials{This is a test of the Emergency Broadcast System.
This-Test. for sample. This T.}{\initials}
\initials
Edit: if you also want to remove the hyphen from the initials, just redefine \DTLinitialhyphen
to do nothing as well:
\renewcommand*{\DTLinitialhyphen}{}
Edit2: Note that \DTLinitials
is designed primarily for names (its original purpose was for use with the abbreviated bibliography style provided by databib
) so it assumes its argument is a series of letters separated by spaces or hyphens. Additionally from the manual:
Be careful if the initial letter has an accent. The accented letter needs to be placed in a group, if you want the initial to also have an accent, otherwise the accent command will be ignored.
So, as per your comment below:
\DTLinitials{{\"{O}}zg\"{u}r}
Or use XeLaTeX or LuaLaTeX with UTF-8 characters. This is similar to the limitations on \makefirstuc
(from mfirstuc
)
Also from the datatool
manual:
In fact, any command which appears at the start of the name that is not enclosed in a group will be ignored.
This means that, say
\DTLinitials{\MakeUppercase{m}ary ann}
will produce m.a. not M.a.
Updated to remove the dash from the display. (the [ ..
]` are there just for illustration. Can be easily removed also if not needed)
\documentclass[11pt]{scrartcl}
\usepackage{luacode}
\begin{luacode*}
function firstletters(arg)
local i;
local str="";
for word in string.gmatch(arg,"%S+") do
str=str.."["..string.sub(word, 1, 1).."]";
i=string.find(word,"%-%S+");
if i ~= nil then
str=str.."["..string.sub(word, i+1, i+1).."]"
end
end
tex.print(str);
end
\end{luacode*}
\newcommand{\firstletters}[1]{\luadirect{firstletters(\luastring{#1})}}
\begin{document}
\firstletters{This is a test of the Emergency Broadcast System. This-Test. for sample. This T.}
\end{document}
Original solution
a lualatex solution (I kept the dash there in the output, but it can easily be not displayed if not needed)
\documentclass[11pt]{scrartcl}
\usepackage{luacode}
\begin{luacode*}
function firstletters(arg)
local i;
local str="";
for word in string.gmatch(arg,"%S+") do
str=str.."["..string.sub(word, 1, 1).."]";
i=string.find(word,"-%S+");
if i ~= nil then
str=str.."["..string.sub(word, i, i+1).."]"
end
end
tex.print(str);
end
\end{luacode*}
\newcommand{\firstletters}[1]{\luadirect{firstletters(\luastring{#1})}}
\begin{document}
\firstletters{This is a test of the Emergency Broadcast System. This-Test. for sample. This T.}
\end{document}
Here is a solution based on classical TeX only:
\def\firstletters{\bgroup \catcode`-=10 \catcode`(=10 \filA}
\def\filA#1{\filB#1 {\end} }
\def\filB#1#2 {\ifx\end#1\egroup \else#1\expandafter\filB\fi}
\firstletters{This is a test of the Emergency Broadcast System.
This-Test. for sample (per se). This T.}
\bye