First, Mike's answer is quite good. I will mostly expand on it and provide more details.
TeX
TeX is a language (a full programming language, actually) for typesetting documents. It originally output to a format called DVI which could then be converted to PostScript, PDF, etc.; more recent versions can output directly to PDF. You write a document with TeX instructions in it, and the TeX system will convert it into printable material.
TeX is used for a wide variety of documents, particularly in science and academia. Most people use it for things that other people would likely use Word for; however, the quality of its results are more on a par with InDesign or other major document layout packages, far superior what word processors generally yield. Designing specialized or ad-hoc document formats such as brochures, however, is probably easier with InDesign or QuarkXPress (although it is not impossible to do so in TeX/LaTeX).
TeX itself is quite low-level.
LaTeX
LaTeX is a macro package written in and for TeX that provides commands and defaults for writing larger documents at a higher level, taking care of things like sectioning, tables of contents, etc. In my experience, most TeX users do not write low-level TeX directly, but rather use LaTeX. LaTeX is not the only such package, though; ConTeXt is another macro package with a different design philosophy, but it sits at a similar level to LaTeX.
Usage
TeX and LaTeX are very widespread in some portions of academia, such as mathematics and computer science, due to its superb support for mathematical formulas. I have also heard that it is popular in some other disciplines as well, such as linguistics.
At the LaTeX level, a counter is created using
\newcounter{mycounter}
This creates a counter initialised at zero which can then be set using
\setcounter{mycounter}{4} % Or whatever
or manipulated using \stepcounter
and \addtocounter
\setcounter{mycounter}{0} % Value is 0
\stepcounter{mycounter} % Value is 1
\stepcounter{mycounter} % Value is 2
\addtocounter{mycounter}{3} % Value is 5
There are then some methods to get the counter value back out. LaTeX creates a \the...
function for each counter, which will print the current value. In places where TeX expects a number, there is also the \value
function:
\themycounter % Prints the current value
\ifnum\value{mycounter} > \value{myothercounter}%
% Do stuff!
\fi
LaTeX's counters are set globally. That makes them good for tracking something that covers the entire document, but not as good for localised calculations.
A TeX count is created using
\newcount\mycount
where the name is a name including a backslash. Setting a count is done very simply: there is no set function
\mycount 4\relax
Notice the \relax
here. Without it, TeX will continue to look for the number in the next thing it finds. This can have some odd effects, and is best avoided. Altering the value can then be carried out using \advance
\mycount 0\relax % Value is 0
\advance\mycount 1\relax % Value is 1
\advance\mycount 1\relax % value is 2
\advance\mycount 3\relax % Value is 5
A similar termination is brought about by having a space after the number
\mycount 4 % Comment used to show that there is a deliberate space
The value of a count register can be recovered using \the
or \number
, and the name itself can be used where TeX expects a number.
\the\mycount % Prints the current value
\number\mycount % The same result
\ifnum\mycount > \myothercount
% Do stuff!
\fi
The big difference is that TeX sets count registers locally. So to do a global assignment you have to do it deliberately
\global\mycount 3\relax
As LaTeX is built on TeX, you might guess that LaTeX's counters are an interface to TeX’s count registers, but it's not immediately obvious how this is done. The way it works is that LaTeX prefixes all of the counter names with c@
, so that if I did
\newcount\c@mycounter
\newcounter{mycounter}
LaTeX would issue an error message: the counter is already defined. The other LaTeX functions then build on this, so that they manipulate the internal counters. This is all done globally and with some error checking. For example, the definition of \addtocounter
is
\def\addtocounter#1#2{%
\@ifundefined{c@#1}%
{\@nocounterr{#1}}%
{\global\advance\csname c@#1\endcsname #2\relax}}
This checks the counter exists, and if it does globally advances it.
Best Answer
It is no different to asking why two java executables use the same java runtime. plain tex and latex both need a tex engine to execute the code, they differ in which format file (ie a memory dump of definitions) they load. Classically that was specified by a commandline argument
tex &plain
vtex &latex
but as a convenience web2c implementations can use the program name to default the format. Similarly the output format (dvi or pdf) can be specified on the command line (or using tex syntax within the file) but the program name can be used to default it.Note it's really not two different executables packaged as one, it is just defaulting the commandline arguments, for example
or
use commandline arguments to produce dvi files using pdflatex.
The behaviour is identical to
It is just that
--output-format
gets a different default if the command name islatex
.