7

How can I typeset (print) just the first letter of a group of text?

For instance, how does biblatex or bibtex determine the first initials of a name?

MWE to get started:

\documentclass[]{article}

\begin{document}

This prints \firstinit{just the first letter}.

My hero is \firstinit{John} \firstinit{Paul} Jones.

\end{document}
Guido
  • 30,740
cslstr
  • 6,545

1 Answers1

7

The routine \justfirst needs its argument not in braces, but terminated by a known quantity. Thus, \firstinit conspires with \justfirst, unbeknownst to the user, to provide it just so, with a \relax as the terminator, because it is unlikely to show up in user text.

\documentclass[]{article}
\def\firstinit#1{\justfirst#1\relax}
\def\justfirst#1#2\relax{#1}

\begin{document}

This prints \firstinit{just the first letter}.

My hero is \firstinit{John} \firstinit{Paul} Jones.

\end{document}

enter image description here


Of course, if you meant for multi-worded phrases to output the first letter of each word, that is slightly more difficult. REVISED to take advantage of recursion; RE-REVISED to eliminate use of packages. RE-RE-REVISED to prevent defeat by a "llama".

\documentclass[]{article}
\def\firstinit#1{\justfirst#1 \relax\relax}
\def\justfirst#1#2 #3\relax{#1\if\relax#3\else{} \justfirst#3\relax\fi}
\begin{document}
This prints \firstinit{just the first llama in the list}.

My hero is \firstinit{John} \firstinit{Paul} Jones.
\end{document} 

enter image description here