# test if a listings or glossaries style is valid

I want to test in my template if a call of

\lstinputlisting[style=mystyle,...


or

\printglossary[style=mystyle,...


is valid or will fail because of an unkown style.

However I could no find out in the sty files how these styles are saved and checked.

-

glossaries defines an internal macro named \@glsstyle@<style> for each style <style>.

In case of listings it's a little bit trickier than that. Basically listings defines a macro \lststy@<style>$ for each style. However, \lstdefinestyle has an undocumented optional argument whose purpose is not quite clear to me. \lstdefinestyle[foo]{mystyle} would define a macro \lststy@mystyle$foo. I have never seen a use case for the optional argument before, though, and my (very superficial) tests didn't reveal how to load such a style, anyway.

Maybe something like the following would work for you:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{listings,glossaries}

% ignore the optional argument:
\newcommand*\ifdeflststyle[1]{%
\ifcsdef{lststy@#1\$}}

\newcommand*\ifdefglsstyle[1]{%
\ifcsdef{@glsstyle@#1}}

\begin{document}

\lstdefinestyle{mystyle}{}
\ifdeflststyle{mystyle}{true}{false}     % => true
\ifdeflststyle{myotherstyle}{true}{false}% => false

\ifdefglsstyle{list}{true}{false}   % => true
\ifdefglsstyle{mystyle}{true}{false}% => false
\newglossarystyle{mystyle}{}
\ifdefglsstyle{mystyle}{true}{false}% => true

\end{document}

-