Macro expanding in an undesired fashion

I’m working on my thesis which is (of course) written in (Xe)LaTeX and which extensively uses the natbib package and bibtex for citations, including features such as \citeauthor{reference}. Since my thesis will be in German, I ran into a problem as described in this question (replace BibTeX’s standard and output with a native word) and independently arrived at a solution along the lines of this question’s answer — copy the .bst file, search and replace the function that outputs and.

I thought I could take this a step further. Rather than adding a plain text German und into the function, I thought about putting a macro there: \undbefehl (German for and command). First tests were successful, the macro expanded properly as I defined it in the preamble of my document. The initial definition was \newcommand\undbefehl{und\ }.

Now my thesis’ abstract is supposed to be written in English, for which I intend to use polyglossia and its language selection features. I came across this question, which suggests the code \iflanguage{english}{and\ }{und\ }. Unfortunately, the results are not what I anticipated. Compare the following MWE:

\documentclass[12pt,a4paper,twoside]{scrreprt}
\usepackage{xunicode}
\usepackage{polyglossia} % for languages
\setmainlanguage[babelshorthands=true,spelling=new]{german} % sets the main language
\setotherlanguage[variant=british]{english} % sets the other language
\usepackage[square, super, sort&compress, comma, numbers]{natbib} % for citations
\setmainfont[Mapping=tex-text]{LiberationSerif} % set a font
\newcommand\undbefehl{\iflanguage{english}{and\ }{und\ }} % create the \undbefehl command

\begin{document}
The first paper was written by \citeauthor{MyersAlkylierung}.\\
The second paper was written by \citeauthor{Gieseler_PBu3}.\\
\undbefehl.

\begin{english}
The first paper was written by \citeauthor{MyersAlkylierung}.\\
The second paper was written by \citeauthor{Gieseler_PBu3}.\\
\undbefehl.
\end{english} % commented out in the second MWE

\bibliographystyle{myangew2} % derived from angew.bst as supplied by the rsc package.
\bibliography{literatur} % includes my literatur.bib
\end{english} % commented out in the first MWE
\end{document}


The first MWE compiles to give this output:

The second one gave this output:

Clearly, \undbefehl gets interpreted at the level of \bibliography{literatur}. I assume that the \citeauthor macro grabs the expanded text to enter into the document itself.

Is there any way in which I can modify the macro, either by adding stuff to the .bst file or by modifying the definition accordingly, so that it can evaluate the language environment is in and expand to and or und depending on the language?

The two relevant entries of the literatur.bib file (the actual file paths are redacted due to privacy):

@article{MyersAlkylierung,
author = {Myers, Andrew G and Yang, Bryant H and Chen, Hou and McKinstry, Lydia and Kopecky, David J and Gleason, James L},
doi = {10.1021/ja970402f},
file = {(redacted)},
issn = {0002-7863},
journal = {J. Am. Chem. Soc.},
month = {jul},
number = {28},
pages = {6496--6511},
title = {{Pseudoephedrine as a Practical Chiral Auxiliary for the Synthesis of Highly Enantiomerically Enriched Carboxylic Acids, Alcohols, Aldehydes, and Ketones}},
url = {http://pubs.acs.org/doi/abs/10.1021/ja970402f},
volume = {119},
year = {1997}
}
@article{Gieseler_PBu3,
abstract = {The first total synthesis of angiolam A has been accomplished in 18 steps. Key steps include vinylogous Mukaiyama aldol reactions of aldehyde-derived dienol ethers, conjugate reduction of the resulting double bond followed by diastereoselective protonation and the Witzeman protocol for macrolactamization. Comparison of the optical rotation of the synthesized material with the isolation data established that the absolute configuration of angiolam A is opposite from the proposed structure.},
author = {Gieseler, Marc Timo and Kalesse, Markus},
doi = {10.1021/ol403423r},
file = {(redacted)},
issn = {1523-7060},
journal = {Org. Lett.},
month = {jan},
number = {2},
pages = {548--551},
pmid = {24341445},
title = {{Synthesis of Angiolam A}},
url = {http://pubs.acs.org/doi/abs/10.1021/ol403423r},
volume = {16},
year = {2014}
}


The file myangew2.bst is a copy of angew.bst as supplied by the rsc package. I modified exactly one line:

FUNCTION {bbl.and}
{ "and" }


became

FUNCTION {bbl.and}
{ "\undbefehl" }

• Please fix your MWE. There are quite a few typos and misspellings here. – Runar Jun 26 '16 at 20:51
• It would help to have the .bib entries, too. – jon Jun 26 '16 at 20:53
• No way we can answer without knowing what myangew2.bst does; please, also supply a bib entry. – egreg Jun 26 '16 at 20:53
• @RunarTrollet That’s what you get for hand-write-copying it from the other PC’s screen … I hope I caught them all >.<' – Jan Jun 26 '16 at 20:56
• Add relevant parts of your .bib-file using filecontents See example here: Problems with bibliography filecontents – Runar Jun 26 '16 at 21:01

I fixed the issues in your code; apart from typos, it's wrong to load xunicode and fontspec should be loaded instead.

The problem is in untimely expansion of \undbefehl, which should be robust. I used \newrobustcmd that's provided by etoolbox, which is loaded by polyglossia.

Also, the change in the .bst file should be

FUNCTION {bbl.and}
{ "\undbefehl{}" }


and I changed the definition of \undbefehl accordingly.

\documentclass[12pt,a4paper,twoside]{scrreprt}

\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage[square, super, sort&compress, comma, numbers]{natbib}

\setmainlanguage[
babelshorthands=true,
spelling=new,
]{german}
\setotherlanguage[
variant=british
]{english}

\setmainfont{LiberationSerif}

\newrobustcmd\undbefehl{\iflanguage{english}{and}{und}}

\begin{document}

The first paper was written by \citeauthor{MyersAlkylierung}.

The second paper was written by \citeauthor{Gieseler_PBu3}.

\undbefehl.

\begin{english}
The first paper was written by \citeauthor{MyersAlkylierung}.

The second paper was written by \citeauthor{Gieseler_PBu3}.

\undbefehl.
\end{english}

\bibliographystyle{myangew2} % derived from angew.bst as supplied by the rsc package.
\bibliography{literatur} % includes my literatur.bib

\end{document}


A more flexible interface for \undbefehl could be obtained with xparse:

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\undbefehl}{}
{
\str_case_x:nn { \languagename }
{
{english}{and}
{german}{und}
}
}
\ExplSyntaxOff


which can easily augmented for taking care of more than two languages.

• Thanks a lot and special thanks for the more flexible interface! – Jan Jun 26 '16 at 21:38
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8108491897583008, "perplexity": 5405.8520355818}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-05/segments/1579251687725.76/warc/CC-MAIN-20200126043644-20200126073644-00487.warc.gz"}