3

Short version: how can I get maths/subscripts working with title case in biblatex without errors?


This question and its answer describe making sentence case titles using biblatex (handy when your .bib file is collated from multiple sources). The important part of the code is in my MWE below.

Maths mode in article (etc.) titles is covered by this answer -- basically wrap the $math$ in an extra pair of braces.

Combining the two leads to errors in my case:

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.bib}
    @article{a_paper,
        author = "N. E. Body and A. N. Other",
        title = "An article about {Al}$_x${Ga}$_{1-x}${N}, {GaN}, {In}{$_x$}{Ga}{$_{1-x}$}{N} and other compounds with Annoying Names {$\omega$}",
        journal = "Journal of tricky examples"
    }
\end{filecontents*}
\documentclass{article}
\usepackage{biblatex}
\addbibresource{\jobname.bib}

%sentence case for biblatex
\DeclareFieldFormat{sentencecase}{\MakeSentenceCase{#1}}
\renewbibmacro*{title}{%
  \ifthenelse{\iffieldundef{title}\AND\iffieldundef{subtitle}}
    {}
    {\ifthenelse{\ifentrytype{article}\OR\ifentrytype{inbook}%
      \OR\ifentrytype{incollection}\OR\ifentrytype{inproceedings}%
      \OR\ifentrytype{inreference}}
      {\printtext[title]{%
        \printfield[sentencecase]{title}%
        \setunit{\subtitlepunct}%
        \printfield[sentencecase]{subtitle}}}%
      {\printtext[title]{%
        \printfield[titlecase]{title}%
        \setunit{\subtitlepunct}%
        \printfield[titlecase]{subtitle}}}%
     \newunit}%
  \printfield{titleaddon}}

\begin{document}
    Test the complete \texttt{.bib} file: \nocite{*}.
    \printbibliography
\end{document}

(with thanks to this answer) gives

! Missing { inserted.
<to be read again> 
                   }
l.35 \end
         {document}
? 
! Missing } inserted.
<inserted text> 
                }
l.35 \end
         {document}
? 

and outputs: output of above MWE

Note that without the braces around the first $_{1-x}$ the subscript doesn't work, so they're definitely needed. The braces to protect the cases of the element symbols aren't the problem either - I get the same error without them (but then of course I get incorrect output). The $\omega$ is error-free (as is the $_x$ in fact). The issue seems to be braces within maths within braces.

Despite the errors the output is fine, but I really don't want to be skipping over all errors in case of others, so how can I get maths/subscripts working with title case in biblatex?

A couple of requirements that got me to this point:

I need the sentence case stuff, or hand edit 200+ entries to make them consistent - journal publishers' .bib output is inconsistent not just between journals but with a journal's own formatting.

Obviously getting the subscripts correct in the titles is necessary.

What's really odd is that I'm sure this was working until recently - I normally compile with a shell script that runs in batch mode but aborts on errors. Yesterday's edits on another machine didn't affect any of the relevant bib entries or the preamble, and it compiled fine.

  • I get correct output with {Al$_x$Ga$_{1-x}$N} – egreg May 13 '14 at 15:54
  • @egreg, so do I on my MWE - there are so many permutations, I don't think I tried this one on the real .bib but I will straight away. – Chris H May 13 '14 at 15:58
  • Braced material is not subject to case change; why splitting it into small pieces, when just one chunk does the job? – egreg May 13 '14 at 16:04
  • @egreg because I originally braced just the elements, and had the maths just in $s (when I created the file for use with bibtex). Then I added the extra braces for biblatex by find+replace (I have a very limited set of subscripts that recur frequently attached to slightly different elements, as in my example). Had I been thinking biblatex from the start I hope I would have done it your (right) way - but somehow I doubt it. If you's care to post an answer I can accept it and tidy up the question. – Chris H May 13 '14 at 18:39
3

With that syntax, LaTeX gets confused in the procedure for doing sentence case. If I add \errorcontextlines=100 to the top of the document, the real source for the error can be spotted:

(./chrisbib.aux) (./chrisbib.bbl)
! Missing { inserted.
<to be read again> 
                   }
\blx@tempa ...ercase {$_x$}{Ga}\MakeLowercase {$_}
                                                  {1-x}\MakeLowercase {$}{N}...
<argument> ...{\csname abx@field@title\endcsname }
                                                  \blx@endunit 
\@secondoftwo #1#2->#2

<argument> \printfield [sentencecase]{title}
                                            \setunit {\subtitlepunct }\print...

which at least clearly shows where LaTeX chokes.

If I change the bib entry into

@article{a_paper,
  author = "N. E. Body and A. N. Other",
  title = "An article about {Al$_x$Ga$_{1-x}$N}, {GaN}, {In$_x$Ga$_{1-x}$N} 
           and other compounds with Annoying Names {$\omega$}",
  journal = "Journal of tricky examples"
}

I get no error. The rule is that braced material is not subject to case changing and it seems that this style doesn't upset biblatex.

I tried the same with BibTeX and it again produces the expected and correct result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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