Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

1 Answer 1

up vote 8 down vote accepted

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

share|improve this answer
    
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 at 2:29
    
Being defeated by a llama deserves a second upvote! –  Chris H Mar 20 at 10:16
    
@ChrisH Well, it was either that, or else implement a [Chilean] package option. –  Steven B. Segletes Mar 20 at 10:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.