1

I'm trying to use xkeyval to create a custom option (\codeblock) for my lstinputlisting (specifically for the linerange part). This is my MWE:

\makeatletter

\newcommand{\code}{}

\define@key{code}{block}{\renewcommand\codeblock{#1}}

\setkeys{code}{block=}
{}
\savekeys{code}{block}

\newcommand\code[1][]{
\setkeys{code}{#1}

\lstinputlisting[linerange=\codeblock,rangeprefix=//---,includerangemarker=false]{./listingwithoutcomments}  

}
\makeatother

I get the error:

! Improper alphabetic constant.
<to be read again>
                   \code
l.4 \code[block=inout]

This comes from using:

\linerange=\block

So I tried:

\linerange=\\block

... but then the resulting listing is completely empty.

I don't think there's anything wrong with my xkeyval syntax but I'm not sure how to get this working?

7
  • You're showing fragments only and it should by linerange=\maxtestblock most likely. You have \newcommand{\code}... twice as well... and autogobble is no key for listings environments
    – user31729
    Jul 12, 2017 at 14:06
  • Sorry but how on Earth did you know about "maxtestblock"? I deliberately removed all instances of that and changed them all to "codeblock"
    – Ben A
    Jul 12, 2017 at 14:57
  • re. autogobble=true: tug.ctan.org/macros/latex/contrib/lstaddons/lstautogobble.pdf
    – Ben A
    Jul 12, 2017 at 15:01
  • The lstaddons stuff was not in your post above
    – user31729
    Jul 12, 2017 at 15:04
  • 1
    An example of usage with (an approximation of) the expected output would make the question answerable.
    – egreg
    Jul 12, 2017 at 18:03

1 Answer 1

0

I got around this with:

\edef\dolisting{\noexpand\lstinputlisting[linerange=\codeblock,rangeprefix=//---,includerangemarker=false,language=maxj,frame=single]{./listingwithoutcomments}}
\dolisting

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.