我希望能够在文档中间切换将索引写为下标和在括号内,例如x_t vs x(t)
。我已经编写了一些代码来执行此操作,如下所示。一切都取决于 \subParen 的设置,它会选择\newif
名为的宏的一个或另一个分支\ifstrcmp
。如果我写,\def\subParen{sub}
我会得到下标,\def\subParen{parens}
我会得到括号。我希望能够在文档内使用\let\subParen\parens
和来回切换\let\subParen\sub
。但是,\let
在这种情况下不会执行通常的操作。以下示例说明了这一点。在示例中,我成功使用调用\let
来更改名为的辅助变量\whichIndex
,但我确实需要更改该变量\subParen
才能使所有变量正常工作。
我非常希望得到一些关于如何让命令\let\subParen\parens
发挥作用的建议,谢谢
\documentclass{minimal}
\usepackage{pdftexcmds}%duplicating so that \isstrcmp below will work
\makeatother
\newif\ifstrcmp
\makeatletter
\def\isstrcmp#1#2{
\ifnum\pdf@strcmp{#1}{#2}=0
\strcmptrue
\else
\strcmpfalse
\fi
}
\makeatother
%The setting on \subParen should determine whether to use subscripts or parens
\def\subParen{sub}
\isstrcmp{\subParen}{sub}
\ifstrcmp
\def\whichIndex{\subIndex}
\else
\def\whichIndex{\parenIndex}
\fi
\def\subIndex#1{_{{#1}}}
\def\parenIndex#1{({#1})}
%e.g., \toppedVar{x}{\hat}{t} is
% \hat{x}_{t} if \whichToppedIndex is set to \toppedSubIndex
% \hat{x}(t) if \whichToppedIndex is set to \toppedParenIndex
\def\toppedVar#1#2#3{#2{#1}\whichIndex{#3}}
%e.g., \besideVar{x}{\ast}{t} is
% {x}_{t}^\ast if \whichBesideIndex is set to \besideSubIndex
% {x}(t)^\ast if \whichBesideIndex is set to \besideParenIndex
\def\besideVar#1#2#3{
\ifstrcmp
#1\whichIndex{#3}^{#2}
\else
#1^{#2}\whichIndex{#3}
\fi
}
\def\xStar#1{\besideVar{x}{\ast}{#1}}
\def\xHat#1{\toppedVar{x}{\hat}{#1}}
\begin{document}
Should be subscripts: $\xStar{t} \xHat{t}$\\
\let\subParen\nothing
Should be parens: $\xStar{t} \xHat{t}$\\
\let\whichIndex\parenIndex
Should be parens with asterisk before parens: $\xStar{t} \xHat{t}$\\
\end{document}
~
答案1
感谢@jfbu 的评论,我在序言中添加了以下 \def 语句。
\def\useParens{\isstrcmp{\subParen}{parens} \ifstrcmp \def\whichIndex{\subIndex} \else \def\whichIndex{\parenIndex}\fi}
\def\useSubs{\isstrcmp{\subParen}{sub} \ifstrcmp \def\whichIndex{\subIndex} \else \def\whichIndex{\parenIndex} \fi}
现在,如果我在正文中输入\useParens
或,\xStar{t} 将分别返回或,这正是我想要的。再次感谢@jfbu。\useSubs
x^ast(t)
x_t^\ast