这个问题的前奏是我之前关于扩展 renewcommand 中的宏。
这接受的答案运行良好,现在我想定义一个以不同方式扩展数学模式和文本模式的宏。
我具体想要实现的是:
$$\Hamiltonian$$ --> $$\glssymbol{Hamiltonian}\index{Hamiltonian}$$
和
\Hamiltonian --> \gls{Hamiltonian}\index{Hamiltonian}
我有一个工作示例(实际上与我上面链接的已接受答案相同),我正在使用\ifmmode .. \else .. \fi
。像这样:
\newcommand{\MyCmd}{%
\ifmmode
\glssymbol{...}\index{...}
\else
\glssymbol{...}\index{...}
\fi
}
% ************* R E D E F I N E -- D E F I N I T I O N S ****************** %
参见示例中的模式。
我应该注意这个解决方案的缺点吗?有没有更好的方法来实现预期的结果?
例子
排版命令:
filename=asdf.tex
pdflatex ${filename}
makeglossaries -l ${filename%%.*}
makeindex ${filename/.*/.idx}
pdflatex ${filename}
pdflatex ${filename}
LaTeX 文件:
\documentclass[b5paper]{memoir}
%
\title{This is a test}
\author{Ola}
% ************** N O T A T I O N -- D E F I N I T I O N ********************* %
\usepackage{amsmath}
%% H
\newcommand{\Hamiltonian}{\ensuremath{\mathcal{H}}}% <-------------- Definition
\let\OldHamiltonian\Hamiltonian% <--------------------- Used in glossary symbol
%% L
\newcommand{\Lagrangian}{\ensuremath{\mathcal{L}}}% <--------------- Definition
\let\OldLagrangian\Lagrangian% <----------------------- Used in glossary symbol
%% P
\newcommand{\vecGeneralMomentum}{\ensuremath{\mathbf{p}}}% <------- Definition
\let\OldVecGeneralMomentum\vecGeneralMomentum% <----- Used in glossary symbol
%% Q
\newcommand{\vecGeneralPosition}{\ensuremath{\mathbf{q}}}% <-------- Definition
\let\OldVecGeneralPosition\vecGeneralPosition% <------- Used in glossary symbol
% ************** G L O S S A R Y -- D E F I N I T I O N ********************* %
\usepackage{hyperref}
\usepackage[nopostdot,nonumberlist,toc,section,acronym,]{glossaries}
%% H
\newglossaryentry{Hamiltonian}{%
type=main,%
name={Hamiltonian function},%
symbol={\OldHamiltonian},%
description={Hamiltonian function},%
}%
%% L
\newglossaryentry{Lagrangian}{%
type=main,%
sort={Lagrangian},%
name={Lagrangian function},%
symbol={\OldLagrangian},%
description={Lagrangian function},%
}%
%% P
\newglossaryentry{vecGeneralPosition}{%
type=main,%
sort={Pos},%
name={position vector},%
symbol={\OldVecGeneralPosition},%
description={Generalised position vector},%
}%
%% Q
\newglossaryentry{vecGeneralMomentum}{%
type=main,%
name={momentuum vector},%
symbol={\OldVecGeneralMomentum},%
description={Generalised momentum vector},%
}%
% ... Prettify the glossary table...
\newglossarystyle{mylong2col}{%
% put glossary in a longtable
\renewenvironment{theglossary}%
{%
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\begin{longtable}[l]{@{\extracolsep{\fill}}l|p{\glsdescwidth}@{}}%
}%
{\end{longtable}}%
% Set the table header
\renewcommand*{\glossaryheader}{%
\bfseries Symbol &%
\bfseries Description
\\ \hline \endhead}%
\renewcommand*{\glsgroupheading}[1]{}%
\renewcommand*{\glossentry}[1]{%
\glsentryitem{##1}% Entry number if required
\glstarget{##1}{\glossentrysymbol{##1}}% Symbol
& \glossentrydesc{##1}% Description
\tabularnewline % end of row
}%
\renewcommand*{\glsgroupskip}{}%
}
%
\makeglossaries
\makeindex
%
% ************* R E D E F I N E -- D E F I N I T I O N S ****************** %
\newcommand{\redefinethestuff}[1]{%
\expandafter\renewcommand\csname#1\endcsname{%
\ifmmode
\glssymbol{#1}\index{\glsentryfirst{#1}}
\else
\gls{#1}\index{\glsentryfirst{#1}}
\fi
}%
}
\forallglsentries[main]{\glslabel}{%
\expandafter\redefinethestuff\expandafter{\glslabel}%
}%
% ===================== B E G I N D O C U M E N T ======================== %
\begin{document}
%
% ----------------------- F R O N T - M A T T E R --------------------------- %
\frontmatter
%
\maketitle
%
\printglossary[style=mylong2col]
%
% ------------------------ M A I N - M A T T E R ---------------------------- %
%
\mainmatter
%
\chapter{Classical Mechanics}
$$
\Hamiltonian := \mathrm{d}\left( \vecGeneralPosition \vecGeneralMomentum \right) - \Lagrangian
$$
\Hamiltonian
% -------------------------- A P P E N D I X -------------------------------- %
\appendix
%
% ------------------------- B A C K - M A T T E R --------------------------- %
\backmatter
\printindex
%
\end{document}