答案1
以下内容摘自TeXbook 代码并转化为一个最小示例。为了避免影响其他类似列表的环境,我将其放在一个有限范围的组中 ( \begingroup
... \endgroup
):
\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}
您可以编写类似的\demoletter{<letter>}
宏来将其内容放入框中而不打印它:
\newcommand{\demoletter}[1]{{\setlength{\fboxsep}{-\fboxrule}\fbox{\phantom{#1}}}}
\demoletter{O}\demoletter{n}\demoletter{e}
\demoletter{l}\demoletter{i}\demoletter{n}\demoletter{e}\demoletter{.}
答案2
该特定部分的代码TeXbook内容如下:
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
所以你可以看到 Knuth 告诉你输入:
\vbox{\hbox{Two lines}\hbox{of type.}}
但实际上他输入的是:
\vbox{\hbox{\demobox{Two lines}}\hbox{\demobox{of type.}}}
在练习 11.5 中,Knuth 建议你自己构建\demobox
宏(当然,我不会在这里展示 :-)。构建完成后,你可以像这样使用它:
\vbox{%
\hbox{\demobox{Go try to solve}}%
\hbox{\demobox{Exercise 11.5}}%
\hbox{\demobox{by yourself :-)}}%
}