You can split the input at each token and then deliver them separated by periods; with \initformat
you can choose the formatting, the argument should be a one parameter macro such as \MakeUppercase
or \textsc
.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\init}{m}
{
\tanh_init:n { #1 }
}
\NewDocumentCommand{\initformat}{m}
{
\cs_set_eq:NN \tanh_init_format:n #1
}
\seq_new:N \l_tanh_init_seq
\cs_new_protected:Nn \tanh_init:n
{
\seq_set_split:Nnn \l_tanh_init_seq { } { #1 }
\tanh_init_format:n { \seq_use:Nn \l_tanh_init_seq { . } .\@ }
}
\ExplSyntaxOff
\initformat{\MakeUppercase} % initialize
\begin{document}
\init{abc}
\initformat{\textsc}
\init{abc}
\end{document}
If you want to remove the trailing period when the initialism is followed by a period, you can check for it and take a decision. In the example I used \xspaceskip
to make the effect more visible.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\init}{m}
{
\tanh_init:n { #1 }
}
\NewDocumentCommand{\initformat}{m}
{
\cs_set_eq:NN \tanh_init_format:n #1
}
\seq_new:N \l_tanh_init_seq
\cs_new_protected:Nn \tanh_init:n
{
\seq_set_split:Nnn \l_tanh_init_seq { } { #1 }
\tanh_init_format:n { \seq_use:Nn \l_tanh_init_seq { . } }
\tanh_init_period:
}
\cs_new_protected:Nn \tanh_init_period:
{
\peek_charcode:NTF . { \@ } { \tanh_init_format:n { . } \@ }
}
\ExplSyntaxOff
\xspaceskip=20pt
\initformat{\MakeUppercase} % initialize
\begin{document}
\init{abc} whatever
\init{abc}. A period!
\initformat{\textsc}
\init{abc} whatever
\init{abc}. A period!
\end{document}
If you want to preserve kerning, you need to make the test about .
beforehand:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\init}{m}
{
\tanh_init:n { #1 }
}
\NewDocumentCommand{\initformat}{m}
{
\cs_set_eq:NN \tanh_init_format:n #1
}
\seq_new:N \l_tanh_init_seq
\cs_new_protected:Nn \tanh_init:n
{
\seq_set_split:Nnn \l_tanh_init_seq { } { #1 }
\tanh_init_period:
}
\cs_new_protected:Nn \tanh_init_period:
{
\peek_charcode_remove:NTF .
{
\tanh_init_format:n { \seq_use:Nn \l_tanh_init_seq { . } . } \spacefactor\sfcode`.~
}
{
\tanh_init_format:n { \seq_use:Nn \l_tanh_init_seq { . } . } \@
}
}
\ExplSyntaxOff
\xspaceskip=20pt
\initformat{\MakeUppercase} % initialize
\begin{document}
\init{abp} whatever
\init{abp}. A period!
\initformat{\textsc}
\init{abp} whatever
\init{abp}. A period!
\textsc{a.b.p.} A period!
\end{document}