[Tex/LaTex] Nomenclature with glossaries line break in name field

glossariesline-breakingnomenclature

I'm using the glossaries package to generate a nomenclature for my document. The problem is that some of my entries have very long functions within the name field. How can I add a manual line break at the desired position? So e.g. after the first or second comma within the function of the second entry.
What I don't want is to change the definition of \glssetwidest as the width fits well for most of the entries.

MWE:

\documentclass[12pt,a4paper]{report}

\usepackage{amsmath}
\usepackage{longtable}
\usepackage[acronym,toc,nopostdot]{glossaries}

\glssetwidest[0]{...................}

\newglossaryentry{first_entry}{
  name = $\omega_i$ ,
  description = Description
}

\newglossaryentry{second_entry}{
  name = {$f(epq_{i}, tlz_{i+1}, xy)$} ,
  description = Description
}


\makeglossaries

\begin{document}
\glsaddall

\printglossary[nonumberlist, style = alttree, title=Nomenclature]

\end{document}

Edit:
If the description is long (e.g. three lines) and the symbol spans over two lines then there is a blank line within the description. How can I change this behaviour?

MWE of Edit:

\documentclass[12pt,a4paper]{report}

\usepackage{amsmath}
\usepackage{longtable}
\usepackage[acronym,toc,nopostdot]{glossaries}

\glssetwidest[0]{...................}

\newglossaryentry{first_entry}{
  name = $\omega_i$ ,
  description = Description
}

\newglossaryentry{second_entry}{
  name = {$f(epq_{i}, tlz_{i+1},\newline xy)$},
  text = {$f(epq_{i}, tlz_{i+1}, xy)$},
  description = {Very long description lines with span several lines and gets broken by a blank line if the symbol span two line. Very long description lines with span several lines and gets broken by a blank line if the symbol span two line.}
}

\makeatletter
\newglossarystyle{alttree2}{%
 \setglossarystyle{alttree}%
  \renewcommand{\glossentry}[2]{%
    \ifnum\@gls@prevlevel=0\relax
    \else
       \settowidth{\glstreeindent}{\glstreenamefmt{\@glswidestname\space}}%
    \fi
      \hangindent\glstreeindent
      \parindent\glstreeindent
    \makebox[0pt][r]{\parbox[t]{\glstreeindent}{\raggedright
       \glsentryitem{##1}\glstreenamefmt{\glstarget{##1}{\glossentryname{##1}}}}}%
    \ifglshassymbol{##1}{(\glossentrysymbol{##1})\space}{}%
    \glossentrydesc{##1}\glspostdescription \space ##2\par
    \def\@gls@prevlevel{0}%
  }%
}
\makeatother

\makeglossaries

\begin{document}
\glsaddall

\printglossary[nonumberlist, style = alttree2, title=Nomenclature]

\end{document}

Best Answer

The alttree style uses \makebox which won't allow line breaks. You could make a new style based on the alttree style that uses a \parbox instead. For example:

\documentclass[12pt,a4paper]{report}

\usepackage{amsmath}
\usepackage{longtable}
\usepackage[acronym,toc,nopostdot]{glossaries}

\glssetwidest[0]{...................}

\newglossaryentry{first_entry}{
  name = $\omega_i$ ,
  description = Description
}

\newglossaryentry{second_entry}{
  name = {$f(epq_{i}, tlz_{i+1},\newline xy)$},
  text = {$f(epq_{i}, tlz_{i+1}, xy)$},
  description = {Description}
}

\makeatletter
\newglossarystyle{alttree2}{%
 \setglossarystyle{alttree}%
  \renewcommand{\glossentry}[2]{%
    \ifnum\@gls@prevlevel=0\relax
    \else
       \settowidth{\glstreeindent}{\glstreenamefmt{\@glswidestname\space}}%
    \fi
      \hangindent\glstreeindent
      \parindent\glstreeindent
    \makebox[0pt][r]{\parbox[t]{\glstreeindent}{\raggedright
       \glsentryitem{##1}\glstreenamefmt{\glstarget{##1}{\glossentryname{##1}}}}}%
    \ifglshassymbol{##1}{(\glossentrysymbol{##1})\space}{}%
    \glossentrydesc{##1}\glspostdescription \space ##2\par
    \def\@gls@prevlevel{0}%
  }%
}
\makeatother

\makeglossaries

\begin{document}
\glsaddall

\printglossary[nonumberlist, style = alttree2, title=Nomenclature]

\end{document}

This produces:

enter image description here

Edit:

The \parbox's height affects the first line of the description, which I'd forgotten about. This can be eliminated by using \smash like this:

\newglossarystyle{alttree2}{%
 \setglossarystyle{alttree}%
  \renewcommand{\glossentry}[2]{%
    \ifnum\@gls@prevlevel=0\relax
    \else
       \settowidth{\glstreeindent}{\glstreenamefmt{\@glswidestname\space}}%
    \fi
      \hangindent\glstreeindent
      \parindent\glstreeindent
    \makebox[0pt][r]{\smash{\parbox[t]{\glstreeindent}{\raggedright
       \glsentryitem{##1}\glstreenamefmt{\glstarget{##1}{\glossentryname{##1}}}}}}%
    \ifglshassymbol{##1}{(\glossentrysymbol{##1})\space}{}%
    \glossentrydesc{##1}\glspostdescription \space ##2\par
    \def\@gls@prevlevel{0}%
  }%
}

This modification to the above MWE now produces:

Image of glossary

However, since this removes the \parbox's height, if the description has fewer lines than the name this may cause the excess lines to clash with the next entry. If this is likely to happen, it may be better to use a tabular-like style. Another possibility you might want to consider is to allow the long name to overflow and start the description on the next line, like this:

\documentclass[12pt,a4paper]{report}

\usepackage{amsmath}
\usepackage{longtable}
\usepackage[acronym,toc,nopostdot]{glossaries}

\glssetwidest[0]{...................}

\newglossaryentry{first_entry}{
  name = $\omega_i$ ,
  description = Description
}

\newglossaryentry{second_entry}{
  name = {$f(epq_{i}, tlz_{i+1}, xy)$},
  description = {\newline Very long description lines with span several lines and gets broken by a blank line if the symbol span two line. Very long description lines with span several lines and gets broken by a blank line if the symbol span two line.}
}

\makeglossaries

\begin{document}
\glsaddall

\printglossary[nonumberlist, style = alttree, title=Nomenclature]

\end{document}

This produces:

Image of glossary