Compile LaTeX File Using Command Line

Jan. 16, 2025

Before, I get used to using TeXstudio to compile LaTeX files. Actually, like other programming languages, we can compile LaTeX source files using command line1. For example, if we have created a file named doc.tex2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
\documentclass{article} % Starts an article
\usepackage{amsmath} % Imports amsmath
\title{\LaTeX} % Title

\begin{document} % Begins a document
\maketitle
\LaTeX{} is a document preparation system for
the \TeX{} typesetting program. It offers
programmable desktop publishing features and
extensive facilities for automating most
aspects of typesetting and desktop publishing,
including numbering and cross-referencing,
tables and figures, page layout,
bibliographies, and much more. \LaTeX{} was
originally written in 1984 by Leslie Lamport
and has become the dominant method for using
\TeX; few people write in plain \TeX{} anymore.
The current version is \LaTeXe.

% This is a comment, not shown in final output.
% The following shows typesetting power of LaTeX:
\begin{align}
	E_0 &= mc^2 \\
	E &= \frac{mc^2}{\sqrt{1-\frac{v^2}{c^2}}}
\end{align} 
\end{document}

then we can compile it using the command latex:

1
latex doc.tex

so, we’ll get some printed information during the compilation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2021/W32TeX) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(./doc.tex
LaTeX2e <2021-11-15> patch level 1
L3 programming layer <2021-11-22>
(c:/texlive/2021/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(c:/texlive/2021/texmf-dist/tex/latex/base/size10.clo))
(c:/texlive/2021/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(c:/texlive/2021/texmf-dist/tex/latex/amsmath/amstext.sty
(c:/texlive/2021/texmf-dist/tex/latex/amsmath/amsgen.sty))
(c:/texlive/2021/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(c:/texlive/2021/texmf-dist/tex/latex/amsmath/amsopn.sty))
(c:/texlive/2021/texmf-dist/tex/latex/l3backend/l3backend-dvips.def)
No file doc.aux.

LaTeX Warning: No \author given.

[1] (./doc.aux) )
Output written on doc.dvi (1 page, 1768 bytes).
Transcript written on doc.log.

and three output files:

1
2
3
doc.aux
doc.dvi
doc.log

where doc.dvi is the compiled file and the file extension .dvi stands for “device independent file format”34.

Or, use the command pdflatex:

1
pdflatex doc.tex

and then we have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2021/W32TeX) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./doc.tex
LaTeX2e <2021-11-15> patch level 1
L3 programming layer <2021-11-22>
(c:/texlive/2021/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(c:/texlive/2021/texmf-dist/tex/latex/base/size10.clo))
(c:/texlive/2021/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(c:/texlive/2021/texmf-dist/tex/latex/amsmath/amstext.sty
(c:/texlive/2021/texmf-dist/tex/latex/amsmath/amsgen.sty))
(c:/texlive/2021/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(c:/texlive/2021/texmf-dist/tex/latex/amsmath/amsopn.sty))
(c:/texlive/2021/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def)
No file doc.aux.

LaTeX Warning: No \author given.

[1{c:/texlive/2021/texmf-var/fonts/map/pdftex/updmap/pdftex.map}] (./doc.aux) )
<c:/texlive/2021/texmf-dist/fonts/type1/public/amsfonts/cm/cmex10.pfb><c:/texli
ve/2021/texmf-dist/fonts/type1/public/amsfonts/cm/cmmi10.pfb><c:/texlive/2021/t
exmf-dist/fonts/type1/public/amsfonts/cm/cmmi7.pfb><c:/texlive/2021/texmf-dist/
fonts/type1/public/amsfonts/cm/cmr10.pfb><c:/texlive/2021/texmf-dist/fonts/type
1/public/amsfonts/cm/cmr12.pfb><c:/texlive/2021/texmf-dist/fonts/type1/public/a
msfonts/cm/cmr17.pfb><c:/texlive/2021/texmf-dist/fonts/type1/public/amsfonts/cm
/cmr5.pfb><c:/texlive/2021/texmf-dist/fonts/type1/public/amsfonts/cm/cmr7.pfb><
c:/texlive/2021/texmf-dist/fonts/type1/public/amsfonts/cm/cmsy10.pfb>
Output written on doc.pdf (1 page, 88375 bytes).
Transcript written on doc.log.

and three files:

1
2
3
doc.aux
doc.log
doc.pdf

including the compiled file doc.pdf.

As can be seen, although commands latex and pdflatex use the same TeX engine (pdfTeX) and same formatting (LaTeX), the compiled file formats are different. This is the difference of these two commands56.

If we compile doc.tex in the TeXstudio, we’ll get an additional zip file, doc.synctex.gz7, compared to the case of using pdflatex command:

1
2
3
4
doc.aux
doc.log
doc.pdf
doc.synctex.gz

For the case of using command latex, we can take a step further. There are two options to convert the .dvi file to a .pdf file156:

  1. Use the command dvips -o doc.ps doc.dvi to convert doc.dvi file into a PostScript file doc.ps, and then the command ps2pdf doc.ps to convert doc.ps to doc.pdf;
  2. Or, directly use the command dvipdfm doc.dvi to convert doc.dvi into doc.pdf.

The workflow of combining using commands latex and dvips is the oldest way (but it’s not that suitable for Chinese documents) and looks sort of complicated. That being said, there are still some western journals adopting this kind of method56.


References

  1. Compiling a (LaTeX) Document˄ ˄2

  2. LaTeX˄

  3. Device independent file format˄

  4. TeX: 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). ˄

  5. LaTeX入门, 刘海洋编著, pp. 31. ˄ ˄2 ˄3

  6. An XeLaTeX Compilation Error: Undefined control sequence. \pdfglyphtounicode˄ ˄2 ˄3

  7. MLTeX EncTeX and SyncTeX TeX extensions: What does SyncTeX do?˄