# (Re)Define a character within newcommand

I thought my issue was an easy one, but it doesn't work the way I want it to work. I want a certain (new) command be redefined within another (new) command. The MWE is clear, I think:

\documentclass[10pt]{article}
\usepackage{fontspec}
\newcommand{\transcr}[1]{\textit{#1}
\renewcommand{\schwa}{\textsuperscript{ə}}}
\newcommand{\schwa}{ə}

\begin{document}
In normal context, \schwa\ appears as it should.\\
In a different place it needs to be \transcr{sup\schwa rscript}, but it isn't\\
And then it doesn't appear as it should in normal context anymore: \schwa.
\end{document}


This gives the following result:


• Your example works as expected; \textit{\renewcommand\schwa{\textsuperscript{ə}}#1} should be the definition of \transcr. Oct 15 '17 at 12:52

I'd define \schwa with a conditional:

\documentclass[10pt]{article}
\usepackage{fontspec}

\newcommand{\transcr}[1]{%
\textit{\transcrtrue #1}%
}

\newif\iftranscr % starts out false
\DeclareRobustCommand{\schwa}{%
\iftranscr\textsuperscript{ə}\else ə\fi
}

\begin{document}
In normal context, \schwa\ appears as it should.

In a different place it needs to be \transcr{sup\schwa rscript}, but it isn't

And then it doesn't appear as it should in normal context anymore: \schwa.

\end{document}


Since \transcrtrue is issued in the group provided by \textit, its effect is confined to it.

A slightly different approach allows for directly using ə (but also \schwa, if you prefer):

\documentclass[10pt]{article}
\usepackage{fontspec}
\usepackage{newunicodechar}

\newcommand{\transcr}[1]{%
\textit{\transcrtrue #1}%
}

\newif\iftranscr % starts out false
\newunicodechar{ə}{%
\iftranscr\textsuperscript{ə}\else ə\fi
}
\newcommand{\schwa}{ə} % must be done _after_ \newunicodechar{ə}{...}

\begin{document}
In normal context, ə appears as it should.

In a different place it needs to be \transcr{supərscript}, but it isn't

And then it doesn't appear as it should in normal context anymore: ə.

\bigskip

In normal context, \schwa\ appears as it should.

In a different place it needs to be \transcr{sup\schwa rscript}, but it isn't

And then it doesn't appear as it should in normal context anymore: \schwa.

\end{document}


You can also nest conditionals:

\documentclass[10pt]{article}
\usepackage{fontspec}
\usepackage{newunicodechar}

\newcommand{\transcr}[1]{%
\textit{\transcrtrue #1}%
}
\newcommand{\phon}[1]{%
\textbf{\phontrue #1}%
}

\newif\iftranscr % starts out false
\newif\ifphon % starts out false
\newunicodechar{ə}{%
\iftranscr
\textsuperscript{ə}%
\else
\ifphon
\textsubscript{/ə/}%
\else
ə%
\fi
\fi
}
\newcommand{\schwa}{ə} % must be done _after_ \newunicodechar{ə}{...}

\begin{document}
In normal context, ə appears as it should.

In a different place it needs to be \transcr{supərscript}, and it is;
likewise it could be \phon{supərscript}.

And then it doesn't appear as it should in normal context anymore: ə.

\end{document}


• Great, that works! A step further: what if I want a character in two different groups? E.g., let's say that ə in \transcr should be superscript, but in \phon it should be subscript (not so realistic, but it works as an example, I think). Simply copying the code and replacing \transcr and \iftranscr with \phon and \ifphon gives an error. Oct 16 '17 at 11:41
• @Jopie I added a possible way, there are others. Oct 16 '17 at 11:53

You need to exchange the order of the instructions \textit{#1} and \renewcommand{\schwa}{\textsuperscript{ə}}. In addition, both instructions need to be enclosed in a TeX group to limit the scope of the redefinition of \schwa.

\newcommand{\transcr}[1]{%
{\renewcommand{\schwa}{\textsuperscript{ə}}%
\textit{#1}}}


A full MWE:

\documentclass{article}
\usepackage{fontspec}

\newcommand{\transcr}[1]{%
{\renewcommand{\schwa}{\textsuperscript{ə}}%
\textit{#1}}}
\newcommand{\schwa}{ə}

\begin{document}
In normal context, \schwa\ appears as it should.

In a different place it needs to be \transcr{sup\schwa rscript}, and now it does.

And when it reappears in normal context --- \schwa\ --- things are still OK.
\end{document}