11

One of the easiest tasks in almost all programming languages is to write a program that writes "Hello World" into the terminal. Something similar in TeX is:

tex '\empty Hello world! \bye'

However, this creates a dvi file in which Hello World is written but does not write "Hello World" in the terminal.

Hence my question, what does a real Hello World program (that prints to the terminal) look like in TeX?

3
  • 4
    tex '\message{Hello world!} \bye' Commented Dec 17, 2020 at 13:42
  • Phelype Oleinke answered my Question: tex '\message{Hello world!} \bye' is what I was looking for, thanks
    – M0M0
    Commented Dec 17, 2020 at 13:51
  • helloworldcollection.de/#TeX mentions the equivalent \immediate\write16{Hello World!}\end.
    – Marijn
    Commented Dec 17, 2020 at 14:02

1 Answer 1

21

TeX is a typesetting program, so if you “just write” something, it will go into the output DVI or PDF file. To get it to write to the terminal you need to explicitly tell it to. There are a few ways you can do that, which produce slightly different results, depending on what exactly you need. Here are a few options just to show some text:


The primitive \message{<text>} expands <text> and writes it to the terminal with no leading or trailing newlines, but with spaces around the message.

tex '\message{<}\message{Hello world!}\message{>} \bye'

prints:

This is TeX, Version 3.14159265 (TeX Live 2020) (preloaded format=tex)
< Hello world! >
No pages of output.
Transcript written on texput.log.

The \write<stream>{<text>} primitive will expand <text> and write it to the <stream> in a new line. If the <stream> is not open to a file, TeX will write <text> to the terminal. The stream 17 cannot be opened to a file (except in LuaTeX), so you can use it to write to the terminal (any other stream will do, as long as it's not open). \immediate makes the \write be executed immediately, rather than when the next page is shipped out.

tex '\message{<}\immediate\write17{Hello world!}\message{>} \bye'

prints:

This is TeX, Version 3.14159265 (TeX Live 2020) (preloaded format=tex)
<
Hello world!
>
No pages of output.
Transcript written on texput.log.

Also, in most current implementations, writing to the stream 18 will have the <text> be executed as a shell command (most of them require the -shell-escape flag). Writing to -1 when running TeX on an input file will write to the .log file (only).


The primitive \errmessage{<text>} expands <text> and writes it to the terminal with an error message, thus TeX prompts you for interaction after the message is printed.

tex '\errmessage{Hello world!} \bye'

prints:

This is TeX, Version 3.14159265 (TeX Live 2020) (preloaded format=tex)
! Hello world!.
<*> \errmessage{Hello world!}
                              \bye
? 
No pages of output.
Transcript written on texput.log.

You must log in to answer this question.

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