1

Need to show compilation error if "\filename{XYZ-2100011}" and "\include{XYZ-2100011}" input filename mismatch.

For example, if \filename{ABC-2100012} and \include{XYZ-2100011} strings filename both are different need to show error message while compilation.

Please find MWE file as follows:

\documentclass{article}

\makeatletter
\def\filename#1{\gdef\@filename{#1}}

\def\include#1{\relax
  \ifnum\@auxout=\@partaux
    \@latex@error{\string\include\space cannot be nested}\@eha
  \else \@include#1 \fi}

\makeatother

\filename{XYZ-2100011}

\begin{document}

\include{XYZ-2100011}

\end{document} 
5
  • I don't understand your question, but you should not redefine \include, you are breaking the new hooks like this. Jun 22, 2021 at 11:38
  • \include definition just copied from latex.ltx not modified anything, my question is both file name should be unique \filename{XYZ-2100011} and \include{XYZ-2100011},
    – Rajesh N
    Jun 22, 2021 at 11:45
  • well not from a current ltx, the definition is different now. Jun 22, 2021 at 11:47
  • Suppose I have updated \filename{ABC-2100011} mistakenly, instead of \include{XYZ-2100011} need to show compilation error. "Please update correct file name what we have given in the \include{XYZ-2100011}.
    – Rajesh N
    Jun 22, 2021 at 11:51
  • I take it that using \include{\csname @filename\endcsname} is too easy. Jun 22, 2021 at 12:08

1 Answer 1

1

The key is to use \ifx to compare string values. Thus, for \include, set #1 into \@tmp macro and then \ifx\@filename\@tmp.

And, obviously, I don't advocate redefining core macros like \include, so change the name.

If the goal is to avoid having filenames match then this:

\documentclass{article}

\makeatletter
\def\filename#1{\gdef\@filename{#1}}

\def\myinclude#1{\relax
  \def\@tmp{#1}%
  \ifx\@filename\@tmp
    \@latex@error{\string Duplicate filename}\@eha
  \else
    \ifnum\@auxout=\@partaux
      \@latex@error{\string\include\space cannot be nested}\@eha
    \else \@include#1 \fi
  \fi}

\makeatother

\filename{XYZ-2100011}

\begin{document}

\myinclude{XYZ-2100011}

\end{document} 

enter image description here

If the goal is to force the filenames to match, then this:

\documentclass{article}

\makeatletter
\def\filename#1{\gdef\@filename{#1}}

\def\myinclude#1{\relax
  \def\@tmp{#1}%
  \ifx\@filename\@tmp
    \ifnum\@auxout=\@partaux
      \@latex@error{\string\include\space cannot be nested}\@eha
    \else \@include#1 \fi
  \else
    \@latex@error{\string Filenames don't match}\@eha
  \fi}

\makeatother

\filename{XYZ-2100011}

\begin{document}

\myinclude{XYZ-210x0011}

\end{document} 

enter image description here

3
  • We almost near, here \filename{XYZ-2100011} and \myinclude{XYZ-2100011} both are same file name, here do not show error message. If \filename{ABC-2100011} is different from \myinclude{XYZ-2100011} in that case need to show error message.
    – Rajesh N
    Jun 22, 2021 at 12:09
  • @RajeshN I have edited my answer to provide both alternatives: to force the filenames not to match, or to force them to match. Jun 22, 2021 at 12:14
  • Excellent Answer Segletes, Thank you very much.
    – Rajesh N
    Jun 22, 2021 at 12:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .