5

Editing a huge document is painfully slow. So one splits it into pieces and has a main document that does \input this and \input that. But: While I'm editing this.tex I want a \bye at the end to make a preview work right, but if the file ends with \bye the compilation of the master document stops after \input this.

There must be a standard solution; the title is conjecturing maybe there's a way to say "\bye, unless this file is being inputted"...

7

One solution might be to define a custom \bye command, for example \mybye, in the main document. In the subdocument you can perform a check if this command is defined, if not then you define it to be \bye. The definition in the main document can be empty.

Checking if a command exists can be done with \ifcsname (which is an e-TeX macro).

MWE, main document:

Hello from master document\par
\def\mybye{}
\input subdocument
more text in master document
\bye

subdocument:

\ifcsname mybye\endcsname\else\def\mybye{\csname bye\endcsname}\fi
Hello from subdocument\par
\mybye
| improve this answer | |
  • Thx. I have the impression I understand how that works - spooky... – David C. Ullrich Jun 1 at 18:23
  • 3
    Surely the command should have been called \maybye... – moewe Jun 2 at 6:04
3

You can use a macro only defined in the main file.

file dcu.tex

\def\ABCDEFGHIJK{}

\input dcu1

\input dcu2

\bye

File dcu1.tex

This is the first file

\ifx\ABCDEFGHIJK\undefined\csname bye\expandafter\endcsname\fi

File dcu2.tex

This is the second file

\ifx\ABCDEFGHIJK\undefined\csname bye\expandafter\endcsname\fi
| improve this answer | |
0

If you are using latex, you can use expl3 to define a boolean \l_bye_bool (initially set to false) to judge if an input should be done.

  • \bye set \l_bye_bool to true
  • \myinput judge whether to input a file according to \l_bye_bool

Then if a1.tex contains a \bye, following \myinput will do nothing.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\bool_new:N \l_bye_bool
\bool_gset_false:N \l_bye_bool
\cs_new_protected:Nn \l_bye_cs: {\bool_gset_true:N \l_bye_bool}
\cs_new_protected:Nn \l_input_cs:n {
  \if_bool:N \l_bye_bool {}
  \else: {\input{#1}}
  \fi:
}
\NewDocumentCommand {\bye} { } {\l_bye_cs:}
\NewDocumentCommand {\myinput} { m } {\l_input_cs:n {#1}}
\ExplSyntaxOff

\begin{document}
\myinput{a1.tex}

\myinput{a2.tex}
\end{document}
| improve this answer | |
  • 2
    Your code \if_bool:N \l_bye_bool {} \else: {\input{#1}} \fi: inputs the file in a group, which is not necessarily bad, but it's better to avoid. I'd suggest using the branching conditional version: \bool_if:NTF \l_bye_bool { } { \input {#1} }. Also you use \bool_gset_(true|false):N in a local variable. You should rename \l_bye_bool to \g_bye_bool. – Phelype Oleinik Jun 1 at 19:52
  • Thanks very much to point out – ZhiyuanLck Jun 2 at 1:31

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.