# N first words command for citation style

I need a way to get to display three first words of the title in citations and if some words did not get displayed add dots. I am using Biblatex.

\nfirstwords[3]{I Like Apples and Oranges}{\dots}
\nfirstwords[3]{I Like Apples}{\dots}


Would give:

I Like Apples...
I Like Apples

-
Maybe this can be done with xstring. –  N.N. Nov 24 '11 at 7:12
xstring looks good, but I have not managed to compare \ StrCount output with a constant in a conditional. –  Andres Nov 27 '11 at 16:33

If you are using biber, you can do this while reading the .bib file by using this biber.conf file:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<sourcemap>
<maps datatype="bibtex" map_overwrite="1">
<map>
<map_step map_field_source="TITLE"
map_match="\A((?:[^\s]+\s+){2})([^\s]+).+"
map_replace="$1$2\\ldots"/>
</map>
</maps>
</sourcemap>
</config>


Note that this is the biber 0.9.8 config file format for which you'll need to be using the biber 0.9.8 beta. Obviously there are all sorts of regular expressions you could use to do this, the above is just an example.

-
I can not rely on availability of biber for now and will use the other solution. Still thanks. –  Andres Nov 30 '11 at 10:14

It might be tricky for arbitrary n, but if you know beforehand that you need 3 words (and that the string will not contain \\, although this is easily changed by substituting another control sequence in the following code), this MWE should do the trick:

\documentclass{article}

\usepackage{etoolbox}% I need \ifblank to put \dots if something nonblank gets deleted

\makeatletter

\def\@threefirstwords #1 #2 #3 #4\\#5{%
#1 #2 #3\ifblank{#4}{}{#5}%
}
\edef\threefirstwords#1#2{\noexpand\@threefirstwords #1\space\space\space\noexpand\\{#2}}

\makeatother

\begin{document}

\threefirstwords{This is a test}{\dots}

\threefirstwords{This is a}{\dots}

\threefirstwords{This is}{\dots}

\threefirstwords{This}{\dots}

\end{document}


Ah, and this seems to have nothing to do with biblatex or even bibliographies.

-
Thanks. This will do the trick. –  Andres Nov 27 '11 at 14:47
You're welcome. Notice that you might want to accept some answer (and to be sincere, the other one seems better to me, although it is biber-specific) as the "best" one. Also, you might want to look at my blog (link in my profile), where I elaborated a bit more on this solution. –  mbork Nov 28 '11 at 0:21
the original version seems to produce extra spaces when less than three words are present. Trial-and-error to rescue: \def\@threefirstwords #1 #2 #3 #4\\#5{% \ifblank{#1}{}{#1}\ifblank{#2}{}{ #2}\ifblank{#3}{}{ #3}\ifblank{#4}{}{#5}} \edef\threefirstwords#1#2{\noexpand\@threefirstwords#1\space\space\space\noexpan‌​d\\{#2}} –  Andres Jan 14 '12 at 19:36