[Tex/LaTex] Nomenclature Sorting: Correct sorting of math while using groups

nomenclaturesorting

I am using LyX to write a report. The nomenclature is divided in groups. Within the "Roman Symbols" group the entries which start with math are sorted before all the non-math entries. See the image at the end of the question.

For example, the entry below should be sorted with the "R"s.

\nomenclature[R]{$\hat{R}$}{Dis}

Starting with the second to last entry, the sorting works fine. After many correctly sorted entries (not shown in the image), the last two entries (also not shown) are out of place. They should be sorted with the "D"s. They are written below.

\nomenclature[R]{${\Delta}Z_0$}{Lig}

\nomenclature[R]{${\Delta}t$}{Snap}

I have tried the solution here:
Make nomenclature sort math correctly

This did not work for me. It might be because I use nomgroups. I do not know. Any ideas? Thanks.

I have written the preamble and the nomenclature entries for the file below. I am rather new to LaTeX so let me know if you need me to post more information.

Preamble:

% Preview source code

%% LyX 2.1.3 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[letterpaper]{geometry}
\geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{color}
\usepackage{babel}
\usepackage{amsmath}
\usepackage[authoryear]{natbib}
\usepackage{nomencl}

% the following is useful when we have the old nomencl.sty package
\providecommand{\printnomenclature}{\printglossary}
\providecommand{\makenomenclature}{\makeglossary}
\makenomenclature
\usepackage[unicode=true,
 bookmarks=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=3,
 breaklinks=true,pdfborder={0 0 0},backref=false,colorlinks=true]
 {hyperref}
\hypersetup{pdftitle={Title Here},
 pdfauthor={Author Here},
 pdfsubject={Subject Here},
 pdfkeywords={Keywords Here},
 citecolor=blue}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\numberwithin{equation}{section}
\numberwithin{figure}{section}
\numberwithin{table}{section}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{tocloft}
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}}
\renewcommand{\headrulewidth}{0pt}
\fancypagestyle{plain}{
\fancyhf{}
\fancyfoot[R]{\thepage}
\renewcommand{\headrulewidth}{0pt}
}

%For matlab2tex support
\usepackage{alltt}
    \usepackage{color}
    \definecolor{string}{rgb}{0.7,0.0,0.0}
    \definecolor{comment}{rgb}{0.13,0.54,0.13}
    \definecolor{keyword}{rgb}{0.0,0.0,1.0}


%For nomenclature
\usepackage{etoolbox}
\renewcommand\nomgroup[1]{%
  \item[\large\bfseries
  \ifstrequal{#1}{A}{Acronyms}{%
  \ifstrequal{#1}{R}{Roman Symbols}{%
  \ifstrequal{#1}{G}{Greek Symbols}{%
  \ifstrequal{#1}{U}{Subscripts}{%
  \ifstrequal{#1}{X}{Other Symbols}{}}}}}]%
  }
\makenomenclature

\makeatother

\begin{document}

Nomenclature Entries:

\printnomenclature{}

%Roman Symbols (Sort as R)

%With math

\nomenclature[R]{$\mathbf{C}$}{DM}

\nomenclature[R]{$\mathbf{A}$}{DM}

\nomenclature[R]{$\tilde{A}$}{DM}

\nomenclature[R]{$\vec{R}$}{Par}

\nomenclature[R]{$\vec{r}$}{Par}

\nomenclature[R]{$\hat{R}$}{Dis}

\nomenclature[R]{$\bar{I}$}{Loc}

\nomenclature[R]{$\left\Vert u_{i}\right\Vert $}{Ve}

\nomenclature[R]{$\overline{v}$}{Ave}

\nomenclature[R]{$\left\Vert \overline{u}_{i}\right\Vert $}{Ave}

\nomenclature[R]{$\overline{u}$}{Aver}

\nomenclature[R]{${\Delta}Z_0$}{Lig}

\nomenclature[R]{${\Delta}t$}{Snap}

%Without Math

\nomenclature[R]{$A$}{DM}

\nomenclature[R]{$a$}{PO}

enter image description here

Best Answer

Nomenclature package pastes your prefix and entry to form the sorting label. Since $\Delta$ starts with backslash, makeindex sorts the entry \nomenclature[R]{${\Delta}Z_0$}{Lig} as R${\Delta$}, and the entry \nomenclature[R]{$D$}{Letter D} as R$D$. If you want \Delta to be sorted as D, use \nomenclature[R$D$]{${\Delta}Z_0$}{Lig} as well as \nomenclature[R$D$]{$\mathbf{C}$}{DM}, \nomenclature[R$A$]{$\tilde{A}$}{DM} etc.

In general if your math entry does not start with a letter, use R$LETTER$ as prefix.