Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Random LaTeX question: Does anyone have any tips for writing LaTeX code to avoid typos or other mistakes? I.e., using the fact that LaTeX is a programming language and not just markup language to catch mistakes?

I started adding what basically are assertions to my TeX files. These have caught a fair number of errors. I also periodically run "static analysis" scripts to catch writing and coding errors but these tends to have many false positives in my experience. E.g., chktex and these: http://matt.might.net/articles/shell-scripts-for-passive-voi...

But I'm thinking there may be good coding styles or habits that could prevent errors too. Any ideas?



My observation is that TeX/LaTeX are designed for use by humans but unlike other programming, the literal content of the document occupies more of the source being generated by the author than the elements used to control the layout (macros, elemental operators, etc.); consequently, errors like improper nesting of environments can produce puzzling error messages. An XML style markup language would produce easier to understand error messages, but I believe that TeX/LaTeX makes the proper trade off by reducing the authoring effort to create content at the expense of harder to debug documents when complex formatting is being done.

Another important observation is the TeX itself is as free of errors as any piece of software you are ever likely to use. LaTeX and many packages can sometimes have quirks because of a bug, but the underlying engine works the way it is intended to work, always.

So knowing this, my approach is to use a system that continuously shows the typeset output as I type, if I can. That way errors are usually obvious and easy to find. If that's not practical with your favorite editor, just manually run LaTex on your source frequently to see how your document is progressing. The times I've had to do serious puzzling over what was happening were almost always because I had pages of complex text entered before checking to see the output.

TeX most of the time doesn't care about whitespace. It makes its own, excellent, decisions on where to to wrap lines according to font size and margins, etc. So this means that you can just start every sentence on a new line without worrying about the right margin; TeX doesn't care. This makes editing and proofing your documents much easier, especially if your editor just autowraps the contents of long sentences without inserting line breaks: then it's easy to cut and paste and rearrange entire sentences while writing. So a three sentence paragraph would look like this in the source:

    Don't go around saying the world owes you a living.
    The world owes you nothing. 
    It was here first. [1]
Rarely, whitespace can matter, for example when defining special environments and macros for typesetting mathematics or a programming sample. In this case you can make use of the % (percent character) which starts a comment that continues to the end of line to cut off trailing whitespace. I find this handy when defining my own commands to keep the LaTeX easy to read by breaking it up into lines without worrying about extra spaces being inserted. (Don't be scared by this next example; it's not something that ordinary users would create. The point is it is possible and can be found when needed on tex.stackexchange.com. It's very fancy wizard level typesetting. TeX/LaTeX allows crazy powerful manipulation of text. See [2] to see the results that this produces--really, take a look.) Note the use of %.

    \newcommand\overlineset[2]{%
      \stackengine{2pt}{$#1$}{\makebox[\widthof{$#1$}]{%
          $\scriptscriptstyle\hrulefill\,#2\,\hrulefill$}}%
              {O}{c}{F}{T}{S}%
    }
Don't fuss with margins, heading font, caption location for figures, and so forth while creating your masterpiece. Get the content down while making sure that LaTeX is formatting it without error. Afterwards, since everything is parameterized, you can go back and modify the appearance and get it to look just right and it will be consistent across the entire document. I find this so very superior to the What-You-See-Is-All-You-Get style of document preparation in Word.

Because TeX/LaTeX is a markup language, it's very easy to generate form letters, labels, etc. by writing a small python script. I've done this to generate spine labels for my books containing a 2-D barcode, Dewey Decimal call number, author, ISBN, and my name for my books. The python code looks up the dimensions of the book on the internet to control the best layout. The TeX/LaTeX part wasn't hard, and with TeX's Turing complete macro language I could have programmed almost all of this in pure TeX, but I found the combination of using python as a preprocessor for a TeX/LaTeX backend very powerful.

Fancy graphics are a challenge in any document, but a fantastic package for graphics is available for LaTeX called TikZ. It is a macro package, written in modern TeX/LaTeX macros. It, like most LaTeX packages, has excellent documentation. See the TikZ package documentation at [3]. Photos and other graphics can be directly inserted into documents too, but TikZ is integrated with LaTeX so margins, reflowing text, captions, fonts come out perfectly.

For academic and more serious writing learn BibTeX, a bibliography generator/database. BibTeX citation information is widely available for the papers I care about so I don't even have to enter the Author/Title/Publisher/etc. myself.

I waste too much time thinking about Fonts because I like them. Once upon a time, when TeX was created, there were very few fonts available for digital use. Today, Font creation should probably be left to professionals and serious hobbyists so I don't use MetaFont anymore. It is a font creation system and has been a part of TeX from the very beginning and is still available to those that would like to design their own fonts. I used it to create my company's first Logo (I only needed 5 upper case letters). Today, there are so many great fonts available, and TeX/LaTeX can work with all modern fonts so making your own isn't a good use of your time. Make sure to consult up to date documentation on font handling because there has been a great deal of evolution in the use of fonts in TeX/LaTeX over the past few decades.

TeX is now 40 years old and has had many extension and additions so it is no fun to build from scratch. Fortunately for the Mac there is the comprehensive MacTex distribution containing every standard/optional part that you should need if you are using MacOS; on Windows there is proTeXt, but I haven't used it. Both MacTeX and proTeXt are just system specific distributions of the TeXLive distribution available on Linux that is updated every year.

Programmers are probably most productive in their favorite editors and these are likely to have a LaTeX mode. Emacs has a very capable mode, AUCTeX, for editing LaTeX documents. I even generate PDFs from my org-mode files by using org-mode support for LaTeX. However, I do this only for documents that are org-mode to begin with, not for ordinary documents.

There are also some nice standalone programs for preparing LaTeX documents. I've used the program TeXShop that comes with the bundle of TeX related stuff that is installed when you install the MacTeX distribution. It probably runs on Windows and Linux as well. This is a good TeX editor, but programmers will likely miss the power and flexibility of using a programming editor (git integration, custom key-bindings, etc.)

To try out LaTeX without needing to install any software there is the excellent web based LaTeX system call Overleaf [4]. It also supports collaborative editing.

[1] attributed to Mark Twain

[2] https://tex.stackexchange.com/questions/122117/overline-cont...

[3] https://www.bu.edu/math/files/2013/08/tikzpgfmanual.pdf

[4] https://www.overleaf.com


Maybe you want to check out proselint? [0]

[0] https://github.com/amperser/proselint


My life has gotten easier since I started using ShareLatex (now Overleaf). It's kind of like Google Docs for LaTeX, and it checks your code for errors as you type.


The old ispell used to be able to deal with TeX/LaTeX input. Though that is for the text, not {La,}TeX code. You can try using TeXworks. You can try typesetting when you want to test and see the output in a pdf preview window. tex.stackexchange.com is a good resource.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: