我以为我的问题很简单,但它并没有按照我想要的方式工作。我希望某个(新)命令在另一个(新)命令中重新定义。我认为 MWE 很清楚:
\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}
我尝试交换 \newcommand 和 \renewcommand 命令,但没有得到好结果。我做错了什么?
答案1
我会\schwa
用条件来定义:
\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}
由于\transcrtrue
是在 提供的群组内发布的\textit
,因此其效果仅限于该群组内。
稍微不同的方法允许直接使用ə
(但\schwa
如果您愿意,也可以):
\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}
您还可以嵌套条件:
\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}
答案2
需要交换指令\textit{#1}
和的顺序\renewcommand{\schwa}{\textsuperscript{ə}}
。另外,这两条指令都需要包含在 TeX 组中,以限制 重定义的范围\schwa
。
\newcommand{\transcr}[1]{%
{\renewcommand{\schwa}{\textsuperscript{ə}}%
\textit{#1}}}
完整的 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}