0

I want to solve Exercise 5.7 in the TeXBook by Donald E. Knuth.

Define control sequences **\beginthe**<block name> and **\endthe**<block name>
that provide a “named” block structure. 

In other words,

**\beginthe{beguine}\beginthe{waltz}\endthe{waltz}\endthe{beguine}**

should be permissible, but not

**\beginthe{beguine}\beginthe{waltz}\endthe{beguine}\endthe{waltz}.**
  1. How do I even create my own csname \beginthe?

I have tried this and it kind of works:

\def\beguine#1{\csname beginbeguine \endcsname}

\begin{beguine} Hello world! \end{beguine}

which gives

ello World!

However, this does not work

\def\beguine#1{\csname beginthebeguine \endcsname}

\beginthe{beguine} Hello World! \endthe{beguine}

  1. How do I make the \beginthe{waltz}...\endthe{waltz} environment nested inside the \beginthe{beguine}...\endthe{beguine} environment?
3
  • Take a look at LaTeX's \begin/\end ;)
    – Joseph Wright
    Aug 1, 2023 at 16:14
  • 4
    you know the texbook has an answers appendix? :-) Aug 1, 2023 at 16:19
  • @DavidCarlisle Thanks! I will look into that :)
    – GarretW
    Aug 2, 2023 at 11:05

1 Answer 1

3

You say

I have tried this and it works:

\def\beguine#1{\csname beginbeguine \endcsname}

\begin{beguine} Hello world! \end{beguine}

But it should give an error that \begin is not defined. Do not use latex for testing plain tex code, many commands have different definitions, even if they have the same name.

The TeX Book gives the answer:

\def\beginthe#1{\begingroup\def\blockname{#1}}

\def\endthe#1{\def\test{#1}%
  \ifx\test\blockname
    \endgroup
  \else
    \errmessage{You should have said \string\endthe{\blockname}}%
  \fi}

Note Knuth's answer here just provides named groups, unlike LaTeX \begin it does not use \csname to attach specific code to the start and end of the group.

2
  • Thanks for the explanation, makes sense, so you would test it in the plain tex code in command prompt? i.e. write tex in the command prompt and type \relax and then execute the code you have written above.
    – GarretW
    Aug 2, 2023 at 11:12
  • well simpler to put in a .tex file (end with \bye) and process with tex (or more likely pdftex) so you have a copy for future use @GarretW Aug 2, 2023 at 11:28

You must log in to answer this question.

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