(Re)Define a character within newcommand
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}
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}