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

Reinhard Kotucha reinhard.kotucha at web.de
Tue Dec 22 01:34:16 CET 2015

On 2015-12-20 at 09:50:10 +0000, Philip Taylor wrote:

> Reinhard Kotucha wrote:
>
> > Adrian is using listings.sty, hence I assume that he's using LaTeX.
> > LaTeX users should better use \@ifundefined instead of TeX primitives
> > or plain TeX macros.
>
> Adrian may well be using "listings.sty", but there is very little
> LaTeX-ish about his code :
>
> > \expandafter
> > \ifx
> >   \csname \@lst @SaveFirstNumber\endcsname
> >   \undefined
> >   it's a new listing
> > \else
> >   it's a continued listing
> > \fi
>
> so suggesting the use of \relax in this context would seem quite
> reasonable to me ...  And does \@undefined map to \relax in such a
> context, as it would need to if his test were to perform as intended ?

Hello Phil,
here is the definition (see texmf-dist/source/latex/ltdefns.ltx):

% \begin{macro}{\@ifundefined}
% \changes{LaTeX2e}{1993/11/23}{Redefined to remove a trailing \cs{fi}}
%    Check if first arg is undefined or \cs{relax} and execute second or
%    third arg depending,
%    \begin{macrocode}
\def\@ifundefined#1{%
\expandafter\ifx\csname#1\endcsname\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}
%    \end{macrocode}
% \end{macro}

So if you're using \@ifundefined exclusively you'll never run into the
\relax vs. \undefined problem.  If you don't need \csname \endcsname
in order to construct a control sequence name, you might think that
you can write

\ifx\csname\undefined

but the result is unpredictable.  Maybe a package you loaded before
did the same test with \@ifundefined and \csname is now mapped to
\relax.  It's much safer if LaTeX users use LaTeX code exclusively.

BTW, the fact that \csname \endcsname always sets the control sequence
to \relax, even if you are just teststing whether it exists at all, is
quite unfortunate.  It adds something to the hash table which can
never be removed and thus stays there forever.  This slows down table
lookups and blows up the string pool.  When I created huge files with
pgfplots I already had to increase the string pool.  In this case
however, I believe that there is a bug in one of related the packages.

Phil, we discussed this issue a few years ago and you forwarded a mail
from someone who explained *why* TeX cannot remove unused csnames from
the hash table.  Unfortunately I can't find this mail anymore.  Do you
remember it and can you forward it to me again?

Regards,
Reinhard

--
------------------------------------------------------------------
Reinhard Kotucha                            Phone: +49-511-3373112
Marschnerstr. 25
D-30167 Hannover                    mailto:reinhard.kotucha at web.de
------------------------------------------------------------------