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}
0

1 Answer 1

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

3
  • Very nice. I was mostly interested in the first case; The edit on the second version is much nicer in the code, and also seems like the spacing is better in typesetting.
    – cslstr
    Mar 20, 2014 at 2:29
  • Being defeated by a llama deserves a second upvote!
    – Chris H
    Mar 20, 2014 at 10:16
  • @ChrisH Well, it was either that, or else implement a [Chilean] package option. Mar 20, 2014 at 10:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .