[Tex/LaTex] How to parse and in bibliography

bibliographiessubscriptssuperscripts

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.

Best Answer

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}

enter image description here

Related Question