What exactly does "present" mean in the phrase "\commandtwo
present in the file"?
Assume the .tex-input-file in question (or some other file processed via \input
or \include
, belonging to the document in question?) contains
\verb|\commandtwo...|
and/or
% This is a comment containing \commandtwo
.
Would the condition of \commandtwo
being present in the file be fulfilled in this case?
If you only wish to know whether \commandtwo
was already carried out/was not(yet) carried out while assuming that there are no other ways of defining \@ScndArg
but carrying out \commandtwo
, then you can check if \@ScndArg
is defined/is undefined.
Werner already showed in his answer how to do this via \ifcsname..\endcsname
in case ε-TeX-extensions are available.
For the sake of having fun you can implement testing whether a macro argument has a first token which is a control sequence which is undefined by checking whether the \meaning
of that token contains the phrase "undefined". This way it is not needed to rely on the availability of whatsoever TeX-extension:
\errorcontextlines=10000
\documentclass{article}
\makeatletter
%%=============================================================================
%% Paraphernalia:
%% \UD@firstoftwo, \UD@secondoftwo, \UD@Exchange, \UD@CheckWhetherNull,
%% \UD@stopromannumeral
%%=============================================================================
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@Exchange[2]{#2#1}%
\@ifdefinable\UD@stopromannumeral{\chardef\UD@stopromannumeral=`\^^00}%
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is empty>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral\expandafter\UD@secondoftwo\string{\expandafter
\UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
\UD@secondoftwo\string}\expandafter\UD@stopromannumeral\UD@secondoftwo}%
{\expandafter\UD@stopromannumeral\UD@firstoftwo}%
}%
%%=============================================================================
%% Check whether brace-balanced argument has a first token which is an
%% undefined control sequence.
%% (Only with undefined control sequences the \meaning has the leading phrase
%% "undefined"; besides this \meaning never delivers no tokens at all)
%%.............................................................................
%% \CheckWhetherLeadingUndefined{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case <argument
%% which is to be checked> has a 1st token
%% which is undefined>}%
%% {<Tokens to be delivered in case <argument
%% which is to be checked> does not have a 1st
%% token which is undefined>}%
\begingroup
\def\CheckWhetherLeadingUndefined#1{%
\endgroup
\newcommand\CheckWhetherLeadingUndefined[1]{%
\romannumeral\expandafter\UD@secondoftwo\expandafter{\expandafter{%
\string##1X}\expandafter\UD@firstoftwo\expandafter{\expandafter
\UD@secondoftwo\string}\expandafter\UD@stopromannumeral\UD@secondoftwo}{%
% Let's nest things into \UD@firstoftwo{...}{} to make sure they are nested in braces
% and thus do not disturb when the test is carried out within \halign/\valign:
\expandafter\UD@firstoftwo\expandafter{%
\expandafter\expandafter\expandafter\UD@stopromannumeral
\romannumeral\expandafter\UD@secondoftwo
\string{\expandafter\UD@CheckWhetherLeadingUndefinedB\expandafter.\meaning##1X#1}{}%
}{}%
}%
}%
\@ifdefinable\UD@CheckWhetherLeadingUndefinedB{%
\long\def\UD@CheckWhetherLeadingUndefinedB##1#1{%
\expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo{}##1}%
{\UD@Exchange{\UD@firstoftwo}}{\UD@Exchange{\UD@secondoftwo}}%
{\expandafter\expandafter\expandafter\UD@stopromannumeral
\expandafter\expandafter\expandafter}%
\expandafter\UD@secondoftwo\expandafter{\string}%
}%
}%
}%
\escapechar=-1\relax
\expandafter\CheckWhetherLeadingUndefined\expandafter{\string\undefined}%
\makeatother
\begin{document}
\medskip\hrule\medskip
The sequence \verb|abc|
\CheckWhetherLeadingUndefined{abc}{does have}{does not have}
a leading token which is an undefined control sequence.
\medskip\hrule\medskip
The sequence \verb|\TeX abc|
\CheckWhetherLeadingUndefined{\TeX abc}{does have}{does not have}
a leading token which is an undefined control sequence.
\medskip\hrule\medskip
The empty sequence
\CheckWhetherLeadingUndefined{}{does have}{does not have}
a leading token which is an undefined control sequence.
\medskip\hrule\medskip
The blank sequence \verb*| |
\CheckWhetherLeadingUndefined{ }{does have}{does not have}
a leading token which is an undefined control sequence.
\medskip\hrule\medskip
The sequence \verb|\else|
\CheckWhetherLeadingUndefined{\else}{does have}{does not have}
a leading token which is an undefined control sequence.
\medskip\hrule\medskip
The sequence \verb|{\UnDefIneD} abc|
\CheckWhetherLeadingUndefined{{\UnDefIneD} abc}{does have}{does not have}
a leading token which is an undefined control sequence.
\medskip\hrule\medskip
The sequence \verb|{}\UnDefIneD abc|
\CheckWhetherLeadingUndefined{{}\UnDefIneD abc}{does have}{does not have}
a leading token which is an undefined control sequence.
\medskip\hrule\medskip
The sequence \verb*| \UnDefIneD abc|
\CheckWhetherLeadingUndefined{ \UnDefIneD abc}{does have}{does not have}
a leading token which is an undefined control sequence.
\medskip\hrule\medskip
The sequence \verb|\UnDefIneD \fi \csname abc|
\CheckWhetherLeadingUndefined{\UnDefIneD \fi \csname abc}{does have}{does not have}
a leading token which is an undefined control sequence.
\medskip\hrule\medskip
The sequence \verb|\UnDefIneD abc|
\CheckWhetherLeadingUndefined{\UnDefIneD abc}{does have}{does not have}
a leading token which is an undefined control sequence.
\medskip\hrule\medskip
\end{document}
Another approach could be re-reading the entire input-file in question under verbatim-catcode-régime for checking via delimited arguments if the phrase \commandtwo
is present. But this could be outmanoeuvred by having a comment containing that phrase.
In case of having input file A and input file B, both belonging to the same document and input file A being processed before processing input file B, this could within input file B also be outmanoeuvred by input file A containing \let\foobar=\commandtwo
and input file B containing \foobar
instead of \commandtwo
.
Therefore I am not elaborating on this unless you write a comment which indicates that you are interested in this approach despite its shortcomings.