4

Can someone figure out a conditional \end or \bye in TeX-Code? (plain tex)

I want to put in several lines into a TeX-file, each ending with \end , to be compiled before the document is complete, to gain and see the "status quo" of the document, later to be finally compiled using the last stated `\end``command.

Example:

This is the end \end This is the other end \end This is the last end \end

The first and second \end command should be deactivated or deleted, as long as another \end command follows, only the third (or last) \end command shall be expanded or used.

  • 1
    It's not really clear what you want to achieve. Why not using a different command, finishing with \bye? – egreg Jan 6 '15 at 0:15
  • @egreg What different command could this be? TeX finishes compiling when reaching the \end command, ignoring the rest of the file. I want to compile the file to its very end, ignoring every \end that occurs before the last one. – M.Borck-Elsner Jan 6 '15 at 0:27
  • 1
    @M.Borck-Elsner What's the meaning of the occurrences of \end before the end of the file? In any case, in plain you really want \bye, surely? – Joseph Wright Jan 6 '15 at 7:19
12

This works, so long as the file has no empty line at the very end.

\let\TeXend\end
\def\blurb{}
\everyeof{\blurb}
\def\end{\futurelet\next\checkend}
\def\checkend{%
  \ifx\next\blurb
    \csname TeXend\expandafter\endcsname
  \fi
}

This is the end \end This is the other end \end This is the last end \end

But it's error prone and, in my opinion, useless. Requires e-TeX.

However, I don't see any usefulness of this. I'd rather use

\def\END{}
%\let\END\bye % when you want to remove the final notes

The document text

This is the end \END
This is the other end \END
This is the last end \END

\bye

If you know in advance the number of \end commands to stop after, then it's fairly easy:

\newcount\ends
\def\END{\advance\ends-1 \ifnum\ends=0 \csname bye\expandafter\endcsname\fi}


\ends=3

ABC \END DEF \END GHI \END JKL \END MNO \END PQR \END STU \END
\bye

This will print

ABC DEF GHI

TeX refuses to process the token \bye in skipped over text in a conditional, but it doesn't balk if it skips over \csname bye\endcsname. The \expandafter ensures that we don't end with an unfinished conditional, by expanding \fi before executing \bye.

1

Example example.tex:

This \end is \end the \end

Compile with:

pdftex -jobname example "\let\TeXend\end\let\end\relax \input example \TeXend"

But I don't really see why would you want that.

0

Will \csname end\endcsname do any good? Like, for example,

\def\temporalend{\csname end\endcsname}
%\def\temporalend{}

Section 1
\temporalend

Section 2
\bye
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. In all honesty, your answer looks like a non-sense to me. Maybe it was meant as a comment to either the question or the egreg's answer? – yo' Jan 7 '15 at 10:12
0

Thanks for the answers. I solved this with numeral conditions.

\newcount\ends
\newcount\lastends
\ends=0
\lastends=3
\def\myend{\advance\ends by 1 \ifnum\ends=\lastends \end \else \fi}
ABC \myend DEF \myend GHI \myend JKL \myend MNO \myend PQR \myend STU \myend
\bye

By giving a value to \lastends, the document is compiled at the point, \ends reaches the value of \lastends , finally it is forced to be compiled by reaching the \bye command . The command \bye does not work in the conditionals, so I used \end.

  • You might have explained that you know in advance the number of \end commands to ignore. – egreg Jan 7 '15 at 23:52
  • @egreg You are right. Eventually, I might not know the number of \end commands to ignore, but this may lead to infinite loop. Any solutions? – M.Borck-Elsner Jan 8 '15 at 0:21
  • I still don't know what's your aim. – egreg Jan 8 '15 at 0:24
  • @egreg As mentioned in the question, I want to append lines or paragraphs or whatever to a document, each ending with an \end command, to be compiled before the document is complete, to see the current output. For example, I want to use echo "First argument \myend ">>test.tex to add some lines to the document, to compile it and see the output. All this should happen without opening the .tex-file. – M.Borck-Elsner Jan 8 '15 at 1:06

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.