[texhax] Recording options with pgfkeys

Joseph Wright joseph.wright at morningstar2.co.uk
Fri May 7 00:02:59 CEST 2010


On 06/05/2010 21:53, José Romildo Malaquias wrote:
> Probably I was not clear about what I want to. Let me try again.
>
> I am writing a LaTeX package to highlight surce code snippets in a LaTeX
> document. An external program (Pygments) will be used to do the actual
> highlighting.
>
> So I have defined an environment that collects all the code snippets
> (with the help of the fancyvrb package) and saves them in an auxiliary
> file.
>
> Then the external program pygmentex (that I have written using the
> Pygmentex Python module) should be run to process this auxiliary
> file. Its results are saved in another file containing LaTeX code
> suitable to be input in the original document. My package automatically
> does that. Each highlighted source code snippet is saved in a macro .
>
> Back to the LaTeX environment, after saving the original source code
> snippet to the auxiliary file, it calls the macro containing the
> highlighted source code snippet defined by the external program,
> including it into the document.
>
> The environment accepts many options that control the highlighting. Some
> options are dealt with by the external program, while other options are
> used in the generated LaTeX macro. I need to separate the options passed
> to the environment into two sets: one for the external program and
> another to the LaTeX macro.
>
> So I want a mechanism to define the options using pgfkeys, and easily
> recording them in a macro for future use.
>
> I am attaching a small document as an example of what I want to do. But
> it is not fully functional. It always uses the last key of the last
> option defined, instead of the current option.
>
> Any help?
>
> Romildo

I don't quite see the issue with what you had, but I also would approach 
things slightly differently.  Something like:

\documentclass{article}
\usepackage{color,pgfkeys}
\makeatletter
\newcommand*\test at color{}
\newcommand*\test at fcolor{}
\newcommand*\test at sep{}
\newcommand*\test at main@opts{}
\newcommand*\test at other@opts{}
\newcommand\test at add[2]{%
   % This assumes e-TeX.
   % You can do the same using two toks without e-TeX,
   %   but e-TeX has been around for many years!
   \ifx\@empty#1\@empty
     \def#1{#2}%
   \else
     \edef#1{\unexpanded\expandafter{#1},\unexpanded{#2}}%
   \fi
}
\pgfkeys{
   /test/.cd,
   color/.code  = \def\test at color{#1}\test at add\test at main@opts{color = {#1}},
   fcolor/.code = \def\test at fcolor{#1}\test at add\test at main@opts{fcolor = 
{#1}},
   sep/.code    = \def\test at sep{#1}\test at add\test at other@opts{sep = {#1}},
   color        = red,
   fcolor       = black,
   sep          = \fboxsep,
}
\newcommand\test[2][]{%
   \begingroup
     \pgfkeys{/test/.cd,#1}%
     \fboxrule 3 pt\relax
     \fboxsep\test at sep\relax
     \fcolorbox{\test at fcolor}{\test at color}{#2}\\
     Regular options: \showtest{\test at main@opts}\\
     Extra options: \showtest{\test at other@opts}%
   \endgroup
}
\newcommand\showtest[1]{%
   \texttt{\detokenize\expandafter{#1}}%
}
\makeatother
\begin{document}

\definecolor{shadecolor}{rgb}{0.9,0.9,0.9}

\test[color=shadecolor]{Bar}

\test[fcolor=blue]{Baz}

\test[sep=5mm,color=yellow,fcolor=cyan]{Good}

\test[color=green]{End}

\makeatletter

\vspace{1cm}

\noindent Regular options: \showtest{\test at main@opts}\\
\noindent Extra options: \showtest{\test at other@opts}%

\makeatother
\end{document}

would be clearer to me. This stores the options themselves without any 
expansion and without adding anything to the user input. Please indicate 
where the problem is with this, something like:

   Test should show "foo, bar, baz" but gives \test[...]{...}

-- 
Joseph Wright


More information about the texhax mailing list