1

I'm trying to rewrite the option graphic from showexpl package, I have reviewed the source code showexpl.sty but, fail to find the modification, the package use keyval (or xkeyval) and listings for definition of graphic command (too confuse for me). My idea is this, the default definition is:

graphic={[option]path/to/image}

and I want to change it to:

graphic={option}{path/to/image}

Is it possible to change this?

Saludos,

Pablo

EDIT: These are the lines from latest showexpl.sty I have tried to modify:

% lines 66 - 72
\newcommand*\SX@graphicname{}%
\newcommand*\SX@graphicparam{}%
\lst@Key{graphic}{}[]{%
  \lstKV@OptArg[width=\linewidth]{#1}{%
    \edef\SX@graphicparam{##1}\edef\SX@graphicname{##2}%
  }%
}%

I tried to modify @ Key ... did not work

% lines 170 - 189
\lstnewenvironment{LTXexample}[1][]
{%
  \@temptokena{#1}%
  \begingroup
    \advance\c@ltxexample\@ne \advance\c@lstlisting\@ne
    \expandafter\lstset\expandafter{\SX@explpreset,#1}%
    \edef\x{\endgroup
      \def\noexpand\SX@codefile{\SX@codefile}%
      \def\noexpand\SX@graphicname{\SX@graphicname}%
      \def\noexpand\SX@graphicparam{\SX@graphicparam}}%
  \x
  \xdef\SX@@explpreset{\the\@temptokena,codefile=\SX@codefile,
    graphic={[\SX@graphicparam]{\SX@graphicname}}}%
  \setbox\@tempboxa=\hbox\bgroup% Warum noetig?
  \lst@BeginWriteFile{\SX@codefile}%
}
{%
  \lst@EndWriteFile\egroup
  \SX@put@code@result
}

change the line graphic={\SX@graphicparam}{\SX@graphicname}} does not work ... And MWE (for aclaration):

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{showexpl}
\begin{document}
\begin{LTXexample}[graphic={[width=5cm]demo}]
change 
graphic={[width=5cm]demo}
to 
graphic={width=5cm}{demo}
\end{LTXexample}
\end{document}
1
  • 1
    Could you provide an MWE of your trials? – user31729 Apr 21 '14 at 3:26
3

Although it is possible (see below), I'm not sure why you would want to change the syntax of the graphic key... With the syntax you suggest, what used to be an optional argument will become mandatory. Are you sure that's really what you want?


First, to make the graphic key accept its arguments as

graphic={option}{path/to/image}

instead of

graphic={[option]path/to/image}

you need to redefine that key using the listings internal macro called \lstKV@TwoArg instead of \lstKV@OptArg:

\lst@Key{graphic}{}[]{%
  \lstKV@TwoArg{#1}{%
    \edef\SX@graphicparam{##1}\edef\SX@graphicname{##2}%
  }%
}%

Then, because the old syntax of the graphic key is used in the definition of the LTXexample environment, you must redefine the latter to work with the new syntax instead.

  1. The listings package does not provide any macro to redefine an environment; therefore, you must undefine the LTXexample environment "manually":

    \let\LTXexample\undefined
    \let\LTXexample@\undefined
    
  2. You can now redefine the latter to work with your new syntax for the graphic key:

    ...
    \xdef\SX@@explpreset{\the\@temptokena,codefile=\SX@codefile,
      graphic={\SX@graphicparam}{\SX@graphicname}}%
    ...
    

enter image description here

Complete code

\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{showexpl}

\makeatletter

% redefinition of showexpl's graphic key
% so that the syntax be {...}{...} instead of {[...]{...}}
\lst@Key{graphic}{}[]{%
  \lstKV@TwoArg{#1}{%
    \edef\SX@graphicparam{##1}\edef\SX@graphicname{##2}%
  }%
}%

% `undefinition' of LTXexample environment
\let\LTXexample\undefined
\let\LTXexample@\undefined

% redefinition of the LTXexample environment
% with the new syntax of the graphic key
\lstnewenvironment{LTXexample}[1][]
{%
  \@temptokena{#1}%
  \begingroup
    \advance\c@ltxexample\@ne \advance\c@lstlisting\@ne
    \expandafter\lstset\expandafter{\SX@explpreset,#1}%
    \edef\x{\endgroup
      \def\noexpand\SX@codefile{\SX@codefile}%
      \def\noexpand\SX@graphicname{\SX@graphicname}%
      \def\noexpand\SX@graphicparam{\SX@graphicparam}}%
  \x
  \xdef\SX@@explpreset{\the\@temptokena,codefile=\SX@codefile,
    graphic={\SX@graphicparam}{\SX@graphicname}}%
  \setbox\@tempboxa=\hbox\bgroup%
  \lst@BeginWriteFile{\SX@codefile}%
}
{%
  \lst@EndWriteFile\egroup
  \SX@put@code@result
}
\makeatother

\begin{document}
\begin{LTXexample}[graphic={width=5cm}{demo}]
change 
graphic={[width=5cm]demo}
to 
graphic={width=5cm}{demo}
\end{LTXexample}
\end{document}

Edit (following OP's comment)

You can, if you want, define a new environment based on LTXexample but using the custom syntax of your graphic key. However, you'll run in trouble if you try to use both that new environment and LTXexample because the latter still relies on the old graphic syntax. One way around is to define a new key (called mygraphic below) instead of redefining the graphic key.

\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{showexpl}

\makeatletter


% definition of a new key based on showexpl's graphic key
% but those syntax is {...}{...} instead of {[...]{...}}
\lst@Key{mygraphic}{}[]{%
  \lstKV@TwoArg{#1}{%
    \edef\SX@graphicparam{##1}\edef\SX@graphicname{##2}%
  }%
}%

% definition of a new environment based on LTXexample
% but using the new syntax of the graphic key
\lstnewenvironment{MYexample}[1][]
{%
  \@temptokena{#1}%
  \begingroup
    \advance\c@ltxexample\@ne \advance\c@lstlisting\@ne
    \expandafter\lstset\expandafter{\SX@explpreset,#1}%
    \edef\x{\endgroup
      \def\noexpand\SX@codefile{\SX@codefile}%
      \def\noexpand\SX@graphicname{\SX@graphicname}%
      \def\noexpand\SX@graphicparam{\SX@graphicparam}}%
  \x
  \xdef\SX@@explpreset{\the\@temptokena,codefile=\SX@codefile,
    mygraphic={\SX@graphicparam}{\SX@graphicname}}%
  \setbox\@tempboxa=\hbox\bgroup%
  \lst@BeginWriteFile{\SX@codefile}%
}
{%
  \lst@EndWriteFile\egroup
  \SX@put@code@result
}
\makeatother

\begin{document}
\begin{MYexample}[mygraphic={width=5cm}{demo}]
change 
graphic={[width=5cm]demo}
to 
graphic={width=5cm}{demo}
\end{MYexample}

\begin{LTXexample}[pos=r]
change graphic={[width=5cm]demo}
to
graphic={width=5cm}{demo}
\end{LTXexample}
\end{document}
7
  • Many thanks, I need change the syntax of the graphic key for compatibility whit tcblisting in one package for examples (and future regex in script perl). Saludos – Pablo González L Apr 22 '14 at 12:38
  • If try to change LTXexample for MYexample not found...?? – Pablo González L May 14 '14 at 4:53
  • @PabloGonzálezL See my edit. – jub0bs May 14 '14 at 8:23
  • OK, but does cause interference with "LTXexample" show error ], i need the modification only for MYexample. – Pablo González L May 14 '14 at 11:26
  • @PabloGonzálezL What do you mean? – jub0bs May 14 '14 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.