[texhax] Trying to find if a listing is continued with listings package

Uwe Lueck uwe.lueck at web.de
Tue Dec 22 17:44:17 CET 2015


At 2015-12-24 10:35 UTC, Adrian Clark wrote:
> I'm trying to do something literate with listings.sty. I'm using
> \lstnewenvironment to define a new environment which receives a name as
> an argument, which is passed on via "\lstset{name=}". I would like to
> do something slightly different in a continued listing of the name and
> the first time it appears. I have tried things along the lines of
> 
> \expandafter
> \ifx
>   \csname \@lst @SaveFirstNumber\endcsname
>   \undefined
>   it's a new listing
> \else
>   it's a continued listing
> \fi 
>
> but none of my attempts have worked. Can anyone point me in the right
> direction? I've attached a minimal example.

Phil seems not to be sure that he knows the direct answer, 
and Reinhard discusses principles instead of answering the 
request directly. 1.) Phil's answer essentially is that the 
previous code always expands to the \else part (as long as 
knowbody \let's \undefined \relax, which could be another, 
while not recommendable, solution), because the token that 
\csname \@lst @SaveFirstNumber\endcsname becomes by 
\expandafter \ifx either is \ifx-equivalent to \relax or to 
whose name "\@lst @SaveFirstNumber" expands -- where I 
confuse code and tokens as almost everybody does (critized 
in the documentation of the dowith package and a talk).
2.) Phil's idea of replacing \undefined by \relax solves 
the issue indeed. 3.) In cases where \else and/or \fi are 
preceded by something that examines the next token, 

    \@ifundefined{\@lst @SaveFirstNumber}{<yeah>}{<oh>}

is superior to

    \expandafter
    \ifx
      \csname \@lst @SaveFirstNumber\endcsname
      \relax
      <yeah><maybe-%>
    \else
      <oh><maybe-%>
    \fi

and as Reinhard has told, this is almost certainly true 
when listings.sty is used (in a different thread, I have 
tried to load a heavily LaTeX package with plain.tex by 
supplying LaTeX definitions beyond miniltx.tex). The 
\@ifundefined version also is better readable, while it 
results in the same number of tokens (7 plus arguments) 
and needs three more expansion steps (\@ifundefined, 
\expandafter, \@<first-second>oftwo; the latter more 
clearly is the token named either "@firstoftwo" or 
"@secondoftwo", see the latex.ltx snippet Reinhard has
provided).

Cheers,

    Uwe.


More information about the texhax mailing list