Wednesday, August 29, 2007

Word Processing (with Latex) in the Command Line

In my never-ending quest to prove that you can do anything in the CLI that you can do in a GUI, this is my attempt at word processing in the command line. There is no true word processor for the CLI, that I'm aware of, but the results of using the Latex typesetting language create a good enough end result. If your not familiar with Latex typesetting language, you can sort of compare it too html markup language. With Latex you use code to assign your document layout, font type, font size and other document characteristics. A skilled Latex artist can create absolutely beautiful, high quality and professional documents.

In the GUI, I'd recommend using Lyx a great WYSIWYG Latex editor, but we're in CLI land here so we'll be using vim-latexsuite a fantastic plug-in to a brillant text editor. In Ubuntu, just 'sudo apt-get install vim-latexsuite' and you are good as gold. The vim-latex-suite has a very in-depth guide built-in. You can access the guide once in vim by typing:
:h latex-suite

This blog will walk you through creating a simple document and serve as just an introduction to the vim-latex-suite. For more a more comprehensive explanation of the latex-suite please see the guide mentioned above.

Open a vim session by typing:
vim texdoc.tex

Now press the 'i' key to change vim to 'insert mode' and type or copy the following into vim:

\documentclass[12pt]{article}\title{My First \LaTeX{} Document!}
\author{Jared Bernard}
\date{December 25 2010}
\begin{document}
\maketitle Ubuntu is my favorite Linux distribution of all time! I can do anything in the CLI that you can do in a GUI plus more. So there..... (sticks tongue out at GUI).
\end{document}

Everything that starts with a '\' is what defines the format of the document. Here is a basic description of what we just created.

  • This document is an article, using a 12 pt font. (There are several types of document classes. 'report' is another common class.)
  • the Title of this document is 'My First Latex Document!'
  • Its author is Jared Bernard. (me)
  • It was written in December 25 2010.
  • Everything else is the content of the document

Now that we know what we created, press the 'esc' key to change to 'normal mode' in vim. Then type ':w' (w/o quotes) to save the document.

Now here comes the fun part, HOLD down the '\' key while pressing the 'l' (el) key twice. This is described in the vim-latex-suite guide (mentioned at the beginning of this blog) as \ll. This will compile the .tex document. If your typeset language has mistakes you will see an error message at the bottom of vim session. If this occurs verify that your typeset language is exactly as the example given and make the appropriate changes. Otherwise, if everything worked out well, you should see no evidence of anything happening :).

Now exit vim, by typing:
:wq

Now that you are at the command line type:
ls

You should now see some new files that where created, texdoc.aux, texdoc.dvi, texdoc.log and of course texdoc.tex. These are your data files for your new document. The .dvi file is your main latex file. There are several ways to view your newly created document, but first you must install some CLI .dvi viewers. Type 'sudo apt-get install dvi2tty dvifb dvipng fbi' to install the viewers which are discussed below.

The first way is using dvi2tty, so type:
dvi2tty texdoc.dvi

This is not my preferred way to view your document as the formatting isn't always accurately displayed, but is does give you a quick look at what you have just created without the typeset language.

A Better option to view your document is using dvifb. To view, type:
dvifb texdoc.dvi

Isn't that beautiful! :) If you copied the typeset exactly as I have it you will notice the word 'Latex' in the title and the unique format I designed for it. You can see the difference as it's displayed with dvi2tty compared to dvifb.

You can also convert your .dvi file to a png image, by typing:
dvipng texdoc.dvi

This will create a file named 'texdoc.png'. You can view this image using fbi by typing:
fbi texdoc.png

Now that's great, but what if I want to email my document to my gui-loving and windows-loving friends? How can they view my new beautiful document. They should have no problem viewing your newly created .dvi file using any pdf viewer like xpdf or Adobe's Acrobat Reader or viewing the .png file in any image viewer.
If you need to make changes to your document, just open the original .tex file and make the necessary changes, save the document, compile the document using the \ll command and you should be good to go.

Let me know what you think. If there is ANYTHING you thought couldn't be done on the Command Line that can be done on the GUI, let me know. I'll try to figure it out and blog about it.

5 comments:

Richard Parsons said...

I'm much more familiar with HTML than I am with Latex. Do you think that it be possible to write a document in vi in HTML, and then print it through Samba/CUPS from the linux command line?

Jared said...

Richard,
Vi is a great HTML editor! I know several people who use it specifically for HTML. Vi is available for most platforms (Linux, Unix, Windows, MAC) and there are GUI front-ends for those shy to the CLI. You can definitely print your HTML from the command line and view your HTML using one of the many CLI browsers such as w3m, links and lynx. Look for my blog on printing from the command line coming soon.

just a man at a keyboard said...

Dear Jared, thank you for this useful post, I used to write .tex files with a GUI on Windows but i find it is so much more fulfilling to use CLI environment on Linux to do that
Thanks again, keep up

Pietro

Unknown said...

Dear Jared,
are you still there?
I just installed vim-latexsuite,
but when I do \ll,
nothing happens...

Unknown said...

Dear Jared,
are you still there?
I just installed vim-latexsuite on my debian crunchbang,
but when I do \ll nothing happens!
Can you help me, please?