0

I've tried an example from The TeXbook but I do not get what Knuth says: see the snippet below. I should have got some rectangles but instead I've got the aligned text which has letters exactly as typed in the source code. What I'm doing wrong here?

enter image description here

  • That's just an example. Later on he proposes this as an exercise (and provides his answer at the end). – Phelype Oleinik Oct 11 at 18:27
2

The following is taken from the TeXbook code and transformed into a minimal example. To avoid influencing other list-like environments, I've placed it inside a limited-scope group (\begingroup...\endgroup):

enter image description here

\documentclass{article}

\begin{document}

\begingroup

\def\dolist{\afterassignment\dodolist\let\next= }
\def\dodolist{\ifx\next\endlist \let\next\relax
  \else \\\let\next\dolist \fi
  \next}
\def\endlist{\endlist}
\def\hidehrule#1#2{\kern-#1%
  \hrule height#1 depth#2 \kern-#2 }
\def\hidevrule#1#2{\kern-#1{\dimen0=#1
    \advance\dimen0 by#2\vrule width\dimen0}\kern-#2 }
\def\makeblankbox#1#2{\hbox{\lower\dp0\vbox{\hidehrule{#1}{#2}%
    \kern-#1 % overlap the rules at the corners
    \hbox to \wd0{\hidevrule{#1}{#2}%
      \raise\ht0\vbox to #1{}% set the vrule height
      \lower\dp0\vtop to #1{}% set the vrule depth
      \hfil\hidevrule{#2}{#1}}%
    \kern-#1\hidehrule{#2}{#1}}}}
\def\maketypebox{\makeblankbox{0pt}{1pt}}
\def\makelightbox{\makeblankbox{.2pt}{.2pt}}
\def\\{\if\space\next\ % assume that \next is unexpandable
 \else \setbox0=\hbox{\next}\maketypebox\fi}
\def\demobox#1{\setbox0=\hbox{\dolist#1\endlist}%
  \leavevmode\copy0\kern-\wd0\makelightbox}

Q: Construct a \verb|\demobox| macro for use in writing manuals
like this, so that an author can write `\verb|\demobox{Tough exercise.}|'
in order to typeset `\thinspace\demobox{Tough exercise.}\thinspace'.

A: The following solution is based on a general \verb|\makeblankbox|
macro that prints the edges of a box using rules of given thickness
outside and inside that box; the box dimensions are those of\/ \verb|\box0|.

\endgroup

\begin{verbatim}
\def\dolist{\afterassignment\dodolist\let\next= }
\def\dodolist{\ifx\next\endlist \let\next\relax
  \else \\\let\next\dolist \fi
  \next}
\def\endlist{\endlist}
\def\hidehrule#1#2{\kern-#1%
  \hrule height#1 depth#2 \kern-#2 }
\def\hidevrule#1#2{\kern-#1{\dimen0=#1
    \advance\dimen0 by#2\vrule width\dimen0}\kern-#2 }
\def\makeblankbox#1#2{\hbox{\lower\dp0\vbox{\hidehrule{#1}{#2}%
    \kern-#1 % overlap the rules at the corners
    \hbox to \wd0{\hidevrule{#1}{#2}%
      \raise\ht0\vbox to #1{}% set the vrule height
      \lower\dp0\vtop to #1{}% set the vrule depth
      \hfil\hidevrule{#2}{#1}}%
    \kern-#1\hidehrule{#2}{#1}}}}
\def\maketypebox{\makeblankbox{0pt}{1pt}}
\def\makelightbox{\makeblankbox{.2pt}{.2pt}}
\def\\{\if\space\next\ % assume that \next is unexpandable
 \else \setbox0=\hbox{\next}\maketypebox\fi}
\def\demobox#1{\setbox0=\hbox{\dolist#1\endlist}%
  \leavevmode\copy0\kern-\wd0\makelightbox}
\end{verbatim}

\end{document}

You could write a similar \demoletter{<letter>} macro to put its contents into a box without printing it:

enter image description here

\newcommand{\demoletter}[1]{{\setlength{\fboxsep}{-\fboxrule}\fbox{\phantom{#1}}}}

\demoletter{O}\demoletter{n}\demoletter{e}
\demoletter{l}\demoletter{i}\demoletter{n}\demoletter{e}\demoletter{.}
6

The code for that specific part of The TeXbook reads:

For example, you can say
\begintt
\vbox{\hbox{Two lines}\hbox{of type.}}
\endtt
and \TeX\ will convert this into
\begindisplay%
  \setbox0=\vbox{\hbox{\demobox{Two lines}}\hbox{\demobox{of type.}}}
$\vcenter{\hbox{\makelightbox\kern-\wd0\box0}}$\qquad
  i.e.,\qquad$\vcenter{\vbox{\hbox{Two lines}\hbox{of type.}}}$
\enddisplay

so you can see Knuth told you to type:

\vbox{\hbox{Two lines}\hbox{of type.}}

but he actually typed:

\vbox{\hbox{\demobox{Two lines}}\hbox{\demobox{of type.}}}

In Exercise 11.5, Knuth proposes you to build yourself the \demobox macro (which I will not show here, of course :-). After you do, you can use it like this:

\vbox{%
  \hbox{\demobox{Go try to solve}}%
  \hbox{\demobox{Exercise 11.5}}%
  \hbox{\demobox{by yourself :-)}}%
}

enter image description here

  • Still, it would be useful if you copied the macro \demobox here, after all. I've tried to copy it and also correct it from Knuth's exercise, but it gives an error: This is XeTeX, Version 3.14159265-2.6-0.999991 (MiKTeX 2.9.7050 64-bit) entering extended mode ! Parameters must be numbered consecutively. <to be read again> l l.74 \def\hidehrule#l #2{\kern-#1% ? ! Emergency stop. <to be read again> l l.74 \def\hidehrule#l #2{\kern-#1% No pages of o. Perhaps I'm not running TeX but XeTeX? – user2925716 Oct 11 at 19:26
  • 3
    @user2925716 No, the error is because you probably copied from the PDF. You have \def\hidehrule#l #2 (with an L and a space), whereas it should be \def\hidehrule#1#2. Copy from the TeXbook source instead. I didn't show the code because the idea is for you to learn to write the code, not copy it. The code works with either engine. – Phelype Oleinik Oct 11 at 19:33

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.