Core TeX "Hello, world!"

Firstly, let's start with a simpler plain TeX file that does not use the \bye macro:

Hello, world!
\end

This already works fine when run with tex (produces a DVI file containing “Hello, world!”), and (as \end is a TeX primitive and not something defined in plain.tex) can be run with tex -ini without (visible) errors, but producing a “blank” DVI file.

To see what's going on, we'd like to turn on tracing, maybe \tracingall if we don't know what sort of tracing we'd like. This \tracingall is defined in plain.tex, and copying the relevant parts from plain.tex exactly as you did, gives:

\catcode`\{=1%
\catcode`\}=2%

\catcode`@=11%
\chardef\@ne=1
\chardef\tw@=2
\def\loggingall{\tracingcommands\tw@\tracingstats\tw@
  \tracingpages\@ne\tracingoutput\@ne\tracinglostchars\@ne
  \tracingmacros\tw@\tracingparagraphs\@ne\tracingrestores\@ne
  % \showboxbreadth\maxdimen\showboxdepth\maxdimen}
  \showboxbreadth100\showboxdepth100}
\def\tracingall{\tracingonline\@ne\loggingall}
\catcode`@=12%

\tracingall

Hello, world!
\end

(I cheated a bit: the commented out line above with \showboxbreadth\maxdimen\showboxdepth\maxdimen is the original; I just used \showboxbreadth100\showboxdepth100 to avoid having to pull in definitions of \maxdimen which needs \newdimen which needs \alloc@ etc.) Anyway, with this it's easier to see the issue in the output: lines like

{the letter H}
{horizontal mode: the letter H}
Missing character: There is no H in font nullfont!
{the letter e}
Missing character: There is no e in font nullfont!

and so on. Actually you can get this output even with a smaller .tex file:

\tracingonline=1
\tracingcommands=2
\tracinglostchars=1
Hello, world!
\end

Anyway, now that you know that the issue is fonts, you can copy over the relevant bits from plain.tex (again I've replaced a few lines from plain.tex (commented out) with simpler equivalents, to avoid copying a whole lot):

\catcode`\{=1 % left brace is begin-group character
\catcode`\}=2 % right brace is end-group character

\font\tenrm=cmr10 % roman text

% \def\rm{\fam\z@\tenrm}
\def\rm{\fam0\tenrm}
% \normalbaselines\rm % select roman font
\rm % select roman font

Hello, world!
\end

Running this with tex -ini typesets the characters in the output, but causes an interesting issue: the resulting dvi file has two pages, one containing “Hello,” and one containing “world!”

But the original issue is resolved, so I'll leave it to you to figure out this mystery. :-)


Edit: For completeness here is a minimal .tex file that when run with tex -ini produces an identical .dvi file (identical except for the timestamp comment of course) as a .tex file containing Hello, world!\bye does when run with tex:

\catcode`\{=1 % left brace is begin-group character
\catcode`\}=2 % right brace is end-group character

\hsize=6.5in
\vsize=8.9in
\parindent=20pt

\topskip=10pt
\parfillskip=0pt plus 1fil

\font\tenrm=cmr10 % roman text

\def\line{\hbox to\hsize}

\countdef\pageno=0 \pageno=1 % first page is number 1

\output{\plainoutput}
\def\plainoutput{\shipout\vbox{\makeheadline\pagebody\makefootline}}
\def\pagebody{\vbox to\vsize{\boxmaxdepth\maxdepth \pagecontents}}
\def\makeheadline{\vbox to0pt{\vskip-22.5pt \line{\vbox to8.5pt{}\hfil}\vss}}
\def\makefootline{\baselineskip24pt\lineskiplimit0pt\line{\hss\tenrm\number\pageno\hss}}
\def\pagecontents{\unvbox255}

\tenrm % select roman font
Hello, world!
\end

Every line (before the last two) is either from plain.tex or a simplified version of one, and removing any line makes the DVI output no longer identical. (Of course to make it minimal I've removed a lot that is useful such as the instruction in \plainoutput that advances the page number, so this will not work when you have more than one paragraph or even more than one line, let alone more than one page.)


Skip TeX, write a PDF directly, it's a compiler-free solution. (just compiled the examples that I've learned from this blog). Paste it in a text editor and save it as a PDF file.

%PDF-1.1
%¥±ë

1 0 obj
  << /Type /Catalog
     /Pages 2 0 R
  >>
endobj

2 0 obj
  << /Type /Pages
     /Kids [3 0 R]
     /Count 1
     /MediaBox [0 0 100 15]
  >>
endobj

3 0 obj
  <<  /Type /Page
      /Parent 2 0 R
      /Resources
       << /Font
           << /F1
               << /Type /Font
                  /Subtype /Type1
                  /BaseFont /Times-Roman
               >>
           >>
       >>
      /Contents 4 0 R
  >>
endobj

4 0 obj
  << /Length 55 >>
stream
  BT
    /F1 18 Tf
    0 0 Td
    (Hello World) Tj
  ET
endstream
endobj

xref
0 5
0000000000 65535 f 
0000000018 00000 n 
0000000077 00000 n 
0000000178 00000 n 
0000000457 00000 n 
trailer
  <<  /Root 1 0 R
      /Size 5
  >>
startxref
565
%%EOF

enter image description here


Here's a minimal core TeX manuscript that typesets "Hello, world!".

\font\myfont=cmr10\myfont
    % When the font is unspecified, it defaults to the nullfont,
    % in which glyphs have no visual representation.
    % Without this line the dvi page will appear empty.
%
\hsize=10cm
    % When \hsize is unspecified, it defaults to 0pt,
    % which causes TeX to break page at every whitespace.
    % Without this line the words will be placed on separate pages.
%
\parfillskip=0pt plus 1fil
    % When \parfillskip is unspecified,
    % the whitepasce between words will stretch
    % to fill up as much line space as possible
    % while keeping all words on a single line.
    % The total line width is given by \hsize.
    % Without this line the two words will be positioned
    % at the opposite sides of a line that is 10cm wide.
%
Hello, world!
\end

* This answer is based on the contributions of cfr's comment, touhami's comment, ShreevatsaR's answer, and Joseph Wright's comment.

Tags:

Tex Core