Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
TeX
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Typesetting system== TeX commands commonly start with a [[backslash]] and are grouped with [[Bracket|curly braces]]. Almost all of TeX's syntactic properties can be changed on the fly, which makes TeX input hard to parse by anything but TeX itself. TeX is a [[Macro (computer science)|macro]]- and [[token (parser)|token]]-based language: many commands, including most user-defined ones, are expanded on the fly until only unexpandable tokens remain, which are then executed. Expansion itself is practically free from side effects. [[Tail recursion]] of macros takes no memory, and if-then-else constructs are available. This makes TeX a [[Turing completeness|Turing-complete]] language even at the expansion level.<ref>{{Citation | first = Alan | last = Jeffrey | url = http://tug.org/TUGboat/Articles/tb11-2/tb28jeffrey.pdf | title = Lists in TeX's Mouth | journal = TUGboat | volume = 11 | number = 2 | date = 1990 | pages = 237β45}}</ref> The system can be divided into four levels: in the first, characters are read from the input file and assigned a category code (sometimes called "catcode", for short). Combinations of a backslash (actually, any character of category zero) followed by letters (characters of category 11) or a single other character are replaced by a control-sequence token. In this sense, this stage is like lexical analysis, although it does not form numbers from digits. In the next stage, expandable control sequences (such as conditionals or defined macros) are replaced by their replacement text. The input for the third stage is then a stream of characters (including the ones with special meaning) and unexpandable control sequences (typically assignments and visual commands). Here, the characters get assembled into a paragraph, and TeX's paragraph breaking algorithm works by optimizing breakpoints over the whole paragraph. The fourth stage breaks the vertical list of lines and other material into pages. The TeX system has precise knowledge of the sizes of all characters and symbols, and using this information, it computes the optimal arrangement of letters per line and lines per page. It then produces a [[DVI file format|DVI]] file ("DeVice Independent") containing the final locations of all characters. This DVI file can then be printed directly given an appropriate printer driver, or it can be converted to other formats. Nowadays, [[pdfTeX]] is often used, which bypasses DVI generation altogether.<ref>{{Cite web|url=https://ctan.org/pkg/pdftex?lang=en|title=CTAN: Package pdftex|website=ctan.org|access-date=2019-07-21}}</ref> The base TeX system understands about 300 commands, called ''primitives''.{{Sfn|Knuth|1984|p=9}} These low-level commands are rarely used directly by users, and most functionality is provided by format files (predumped memory images of TeX after large macro collections have been loaded). Knuth's original default format, which adds about 600 commands, is Plain TeX.<ref>{{citation | title = Plain TeX | type = source code | url = ftp://tug.ctan.org/pub/tex-archive/systems/knuth/dist/lib/plain.tex | archive-url = https://web.archive.org/web/20210509074312/https://tug.ctan.org/pub/tex-archive/systems/knuth/dist/lib/plain.tex | archive-date = 2021-05-09 | url-status = dead | publisher = CTAN}}</ref> The most widely used format is [[LaTeX]], originally developed by [[Leslie Lamport]], which incorporates document styles for books, letters, slides, etc., and adds support for referencing and automatic numbering of sections and equations. Another widely used format, [[AMS-TeX]], is produced by the [[American Mathematical Society]] and provides many more user-friendly commands, which can be altered by journals to fit with their house style. Most of the features of AMS-TeX can be used in LaTeX by using the "AMS packages" (e.g., <code>amsmath</code>, <code>amssymb</code>) and the "AMS document classes" (e.g., <code>amsart</code>, <code>amsbook</code>). This is then referred to as [[AMS-LaTeX]].<ref>{{Cite web|url=https://texfaq.org/FAQ-AMSpkg|title=What are the AMS packages (amsmath, etc.)?|date=2018-05-27|website=The TeX FAQ|access-date=2019-07-21|archive-date=28 April 2019|archive-url=https://web.archive.org/web/20190428184756/https://texfaq.org/FAQ-AMSpkg|url-status=dead}}</ref> Other formats include [[ConTeXt]], used primarily for desktop publishing and written mostly by Hans Hagen at [[PRAGMA Advanced Document Engineering|Pragma]]. ===How it is run=== [[File:LaTeX sample.png|thumb|A sample page produced using TeX with the [[LaTeX]] macros]] A sample [[Hello world program]] in plain TeX is: <syntaxhighlight lang="latex"> Hello, World \bye % marks the end of the file; not shown in the final output </syntaxhighlight> This might be in a file ''myfile.tex'', as ''.tex'' is a common [[file extension]] for plain TeX files. By default, everything that follows a percent sign on a line is a comment, ignored by TeX. Running TeX on this file (for example, by typing <kbd>tex myfile.tex</kbd> in a [[command-line interpreter]], or by calling it from a [[graphical user interface]]) will create an output file called ''myfile.dvi'', representing the content of the page in a '''d'''e'''v'''ice '''i'''ndependent format (DVI). A DVI file could then be either viewed on screen or converted to a suitable format for any of the various printers for which a device driver existed (printer support was generally not an operating system feature at the time that TeX was created). Knuth has said that there is nothing inherent in TeX that requires DVI as the output format, and later versions of TeX, notably pdfTeX, XeTeX, and LuaTeX, all support output directly to [[Portable Document Format|PDF]]. ===Mathematical example=== {{Anchor|math mode|mathematics mode|mathematical mode}} TeX provides a different text syntax specifically for mathematical formulas. For example, the [[quadratic formula]] (which is the solution of the [[quadratic equation]]) appears as: {| ! Source code !! Renders as |- |<syntaxhighlight lang="latex"> The quadratic formula is $-b \pm \sqrt{b^2 - 4ac} \over 2a$ \bye </syntaxhighlight> |<math>\hbox{The quadratic formula is } \textstyle{-b \pm \sqrt{b^2 - 4ac} \over 2a}</math> |} The formula is printed in a way a person would write by hand, or typeset the equation. In a document, entering ''mathematics mode'' is done by starting with a $ symbol, then entering a formula in TeX syntax, and closing again with another of the same symbol. Knuth explained in jest that he chose the dollar sign to indicate the beginning and end of mathematical mode in plain TeX because typesetting mathematics was traditionally supposed to be expensive.{{Sfn | Knuth | 1984 | loc = Ch. 16: Typing Math Formulas | p = 127}} ''Display mathematics'' (mathematics presented centred on a new line) is similar but uses $$ instead of a single $ symbol. For example, the above with the quadratic formula in display math: {| ! Source code !! Renders as |- |<syntaxhighlight lang="latex"> The quadratic formula is $$-b \pm \sqrt{b^2 - 4ac} \over 2a$$ \bye </syntaxhighlight> |<math>\begin{matrix} \text{The quadratic formula is} \\ \displaystyle{\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}} \end{matrix}</math> |} (The examples here are not actually rendered with TeX; spacing, character sizes, and all else may differ.)
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)