从字符串中提取键值对

从字符串中提取键值对

我想从字符串中提取键值对。我需要它来定义新的词汇表样式,并希望通过现有参数传递一些附加信息。

\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 
}%
}

在最后一行,我得到了##3包含类似字符串的参数parameter_1=xyz,parameter_2=ztu。我想要做的是访问此参数的值来构建一个表:

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

做这个的最好方式是什么?

以下是示例输入:

\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}

答案1

您可以轻松使用以下方法keyval完成此任务:

\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 [email protected]\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}

相关内容