2

The prelude to this question is my previous question regarding expanding macros inside renewcommand.

The accepted answer works beautifully, and now I want to define a macro which expands differently math-mode and text-mode.

Specifically what I want to achieve is this:

$$\Hamiltonian$$ --> $$\glssymbol{Hamiltonian}\index{Hamiltonian}$$

and

\Hamiltonian --> \gls{Hamiltonian}\index{Hamiltonian}

I have a working example (effectively the same as the accepted answer I linked to above) where I am using \ifmmode .. \else .. \fi. Like this:

\newcommand{\MyCmd}{%
\ifmmode
\glssymbol{...}\index{...}
\else
\glssymbol{...}\index{...}
\fi
}

See under the patttern % ************* R E D E F I N E -- D E F I N I T I O N S ****************** % in the example.

Is there any drawbacks I should be aware of with this solution and is there a better way to achieve the desired result?

Example

Typeset command:

filename=asdf.tex
pdflatex ${filename}
makeglossaries  -l ${filename%%.*}
makeindex  ${filename/.*/.idx}
pdflatex ${filename}
pdflatex ${filename}

LaTeX file:

\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}
  • Add \relax in front of \ifmmode – egreg Dec 28 '16 at 18:19
  • And please don't use $$ ...$$ ... that's deprecated, I used \[ ...\] in my solution to the linked answer – user31729 Dec 28 '16 at 18:33
  • Thank you for the comment I was not aware of this, I have always been consistent in avoiding "["... – Sigve Karolius Dec 28 '16 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.