How to parse </sub> and </sup> in bibliography

I use Mendeley to generate the .bib file and this results in articles with <sub> and <sup> tags in the title.

@article{Testtest2016,
author = {Testtest},
title = {{Why O{\textless}sub{\textgreater}2{\textless}/sub{\textgreater} and O{\textless}sup{\textgreater}2-{\textless}/sup{\textgreater}?}},
year = {2016}
}


Currently they are compiled in the bibliography as [2] Testtest. Why O<sub>2</sub> and O<sup>2-</sup>? 2016. whereas I'd like O2 and O2-

Can I make LaTeX replace the tags with $_{...}$ and $^{...}$ ? If not I'll find another way (e.g. editing bib.bib on-the-fly) but I'd prefer LaTeX doing it automatically. I do not want to change the title inside Mendeley as the titles are coming from the DOI search.

I'm sorry if this has already been adressed but I can't find any answer.

• Welcome to TeX.SX! A tip: You can use backticks  to mark your inline code as I did in my edit. (By the way, we usually don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you.) – ebosi Jul 12 '16 at 8:33
• Just to clarify: do you have <sub>...</sub> and <sup>...<\sup> tags, or only <sub> ones? – ebosi Jul 12 '16 at 8:38
• Welcome, i don't think it is the right way to make LaTeX understand HTML/XML, but instead make Mendeley produce a correct bib file. Or likely, to provide Mendeley with a decent input. Most automatic bib generators provide some faulty stuff. – Johannes_B Jul 12 '16 at 8:54
• I changed the OP to make it clearer – René Jul 12 '16 at 8:55
• The entry produced by Mendeley is completely wrong. If it produced the tags, something could be done, but instead it produces code for printing the tags as text. – egreg Jul 12 '16 at 16:43

You should edit the entries and tell the maintainers of Mendeley to stop outputting such rubbish.

Here's a workaround that assumes you don't need \textless anywhere else and that the tags are just <sup>...</sup> and <sub>...</sub>, without nesting.

\begin{filecontents*}{\jobname.bib}
@article{Testtest2016,
author = {Testtest},
title = {{Why O{\textless}sub{\textgreater}2{\textless}/sub{\textgreater} and O{\textless}sup{\textgreater}2-{\textless}/sup{\textgreater}?}},
year = {2016}
}
\end{filecontents*}

\documentclass{article}

\makeatletter
% get rid of the closing braces
\def\textless{\afterassignment\textless@\let\next= }
% get the tag type
\def\textless@#1#{\@nameuse{textless@#1}}
% code for <sub>
\def\textless@sub#1#2/sub#3{%
\ensuremath{_{\let\textless\relax#2}}%
\egroup % matches the first brace
}
% code for <sub>
\def\textless@sup#1#2/sup#3{%
\ensuremath{^{\let\textless\relax#2}}%
\egroup % matches the first brace
}
\makeatother

\begin{document}

\cite{Testtest2016}

\bibliographystyle{plain}
\bibliography{\jobname}

\end{document}
`