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.

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
share|improve this question
3  
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
add comment

2 Answers 2

up vote 12 down vote accepted

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
    
Thanks. This will do the trick. –  Andres Nov 27 '11 at 14:47
1  
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
1  
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
add comment

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.