# Extract key-value pairs from a string

I want to extract key value pairs from a string. I need this to define a new glossary style and want to pass some additional information through a existing parameter.

\newglossarystyle{meinglossar}{%
\renewenvironment{theglossary}{\begin{description}}{\end{description}}%
\renewcommand*{\glossaryentryfield}[5]{%
\item[\glstarget{##1}{##2}:]%
\dotfill ##5%
\item
\ifthenelse{\equal{##4}{\relax}}{}{\space (##4)}%
\space ##3 % THIS IS THE LINE THAT MATTERS
}%
}


In the almost last line I get the argument ##3 which contains a string like parameter_1=xyz,parameter_2=ztu. What I want to do is to access the values of this parameters to build a table:

\begin{tabular}{l l}
First Parameter & \parameter_1 \\ % \parameter_1 should contain "xyz"
...
\end{tabular}


What is the best way to do this?

Here is an example input:

\documentclass[12pt]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[]{glossaries}

\newglossarystyle{meinglossar}{%
\renewenvironment{theglossary}{\begin{description}}{\end{description}}%
\renewcommand*{\glossaryentryfield}[5]{%
\item[\glstarget{##1}{##2}:]% Eintragsname:
\dotfill ##5% ...Seitenzahl
\item
\gdef\parameters{##3}
% parameters contains now the key value pairs in the form of "name={lorem ipsum...},ref={lorem ipsum}" and i want to extract them one by one to insert them to a table
\begin{tabular}{l l}
Name & <name> \\%nameparameter
Reference & <ref> %refparameter
\end{tabular}
}%
}

%format
\renewcommand*{\glstextformat}{\textit}

% glossary
\newglossaryentry{workstation}{
name=Workstation,
description={
name={Lorem Ipsum},
ref={Chapter 1}
}
}

\newglossaryentry{Test}{
name=Test,
description={
name={Lorem Ipsum},
ref={Chapter 1}
}
}

\makeglossaries

\begin{document}

\section*{Test}
Now i will use \gls{workstation} and the \gls{Test} entries of the glossary.

\printglossary[style=meinglossar]

\end{document}

-

## migrated from stackoverflow.comJun 25 '12 at 14:32

This question came from our site for professional and enthusiast programmers.

You can write the contents of these arguments to a macro inside \glossaryentryfield, \gdef\parone{##3}, and then use \parone. – Werner Jun 25 '12 at 3:24
Hej Werner, thank you for your answer. Unfortunately I do not know how to access the key-value pairs, after storing them in \parone. I mean, the answer you gave is to store the string containing the key value pairs ("key_1=value1,key_2=value2"). I do not know how i can now extract the value of "key_1". What is the best way to do this? Thx! – SebasZil Jun 25 '12 at 6:47
In order to be able to give an answer, some more context is needed. Please, try and make the code into a minimal example with the call to a document class and the necessary packages (glossaries, probably), with a couple of glossary entries. Moreover the possible keys are needed. – egreg Jun 25 '12 at 15:50
I'd say that is exactly what the keyval package is for. – canaaerus Jun 25 '12 at 16:15

You can quite easily use keyval for this task:

\documentclass{report}
\usepackage{glossaries}
% Edit by A.M.:
% Which package loads xkeyval.sty before here? To find it requires
% putting some search code on top of \documentclass. Certainly xkeyval
% has been loaded before here.
% \ifcsname ver@xkeyval.sty\endcsname\undefined\fi
% \usepackage{keyval}

\makeatletter
\define@key{glossparam}{name}{\def\seb@name{#1}}
\define@key{glossparam}{ref}{\def\seb@ref{#1}}

\newglossarystyle{mainglossary}{%
\renewenvironment{theglossary}{\begin{description}}{\end{description}}%
\renewcommand*{\glossaryentryfield}[5]{%
\item[\glstarget{##1}{##2}:]%
\dotfill ##5%
\item\setkeys{glossparam}{##3}
\begin{tabular}{l l}
Name & \seb@name \\ % name parameter
Reference & \seb@ref % ref parameter
\end{tabular}
}%
}
\makeatother

\let\glstextformat\textit
\newglossaryentry{workstation}{%
name=Workstation,
description={%
name={Lorem Ipsum}, ref={Chapter 1}%
}%
}
\newglossaryentry{Test}{%
name=Test,
description={%
name={Lorem Ipsum}, ref={Chapter 1}%
}%
}

\makeglossaries
\begin{document}
\section*{Test}
Now I will use \gls{workstation} and the \gls{Test} entries of the glossary.

\printglossary[style=mainglossary]
\end{document}

-
Thank you very much! This was exactly what i needed!!! – SebasZil Jun 30 '12 at 11:05