8

Recently, I learned that for a long time I have been doing all of my mathematics in LaTeX incorrectly!

I was using the $ TeX commands instead of the \( and \) LaTeX commands! I immediately went back and fixed my error, but it got me wondering, is there a way to make the LaTeX compiler and TexStudio (Or another editor) not use the TeX commands in order to preserve proper typesetting?

Furthermore, would it possible to write a macro to automatically make the $ signs into the respective \( and \)? I've tried

\renewcommand{$}{\relax\ifmmode\)\else\(\fi}

But this is full or errors due to the handling of the special signs.

There are probably many places where I may be making similar mistakes, so I was also wondering if there was a LaTeX syntax checker sort of like the W3C HTML validator for websites, or an IDE that would force me to use proper commands.

  • 3
    There's the nag package which is supposed to warn a user if they use obsolete things... ctan.org/pkg/nag I never used it, though, so I can't tell how reliable it is – clemens Dec 3 '13 at 21:26
  • 1
    For your macro, what about a regex replace in your document: s/\$([^$]*)\$/\\(\1\\)/g. Making $ active and defined as you suggest above is not a great idea. Even if you insisted, a bit better definition would be \catcode`\$\active\def$#1${\(#1\)}, but IMHO it shouldn't really be used. I stuck with TeXy $...$ for inline and LaTeXy \[...\] for display, and I'm quite happily living :) – yo' Dec 3 '13 at 21:29
  • 5
    No, you can't do \renewcommand{$}{...}. This wouldn't solve the problem anyway. Don't be afraid of using $ for inline formulas: it's not wrong. To the contrary, it is wrong using $$ for display math. – egreg Dec 3 '13 at 21:30
  • 4
    \usepackage[all]{onlyamsmath} destroys a number of obsolete environments. It leaves $ unchanged, which is correct as others have pointed out. See the package documentation for more details. – Ian Thompson Dec 3 '13 at 23:21
  • @Ian Thompson, you should make an answer with what you have put in that comment. – user2987828 Dec 4 '13 at 9:34
10

As already mentioned in comments, the nag package does its best to pick up not only use of TeX rather than LaTeX syntax but also deprecated LaTeX stuff. That is the best you can do with LaTeX2e because of the way the kernel itself and packages are written: they use TeX constructs without 'LaTeX wrappers'.

One of the aims of the LaTeX3 Project is to develop a new LaTeX which provides these low-level constructs with 'proper' LaTeX (code) interfaces. However, at this stage this does not reach the point of having a fully working format, so the restrictions of LaTeX2e remain.

On the specific question of $...$ versus \( ... \), note that the TeX-like syntax is explicitly allowed by LaTeX2e: it's in LaTeX: A Document Preparation System (2nd ed., page 39). Thus quite apart from any technical discussions, disabling $ really doesn't make sense as you (and by extension package authors) are allowed to use it for inline math mode. (This does not extend to $$ for display math mode, for which the official LaTeX interface is \[ ... \].)

8

Where did you "learn" that using $ as a switch to initiate and terminate inline math mode is incorrect for LaTeX users?

I'd go even further than @egreg does in his comment and state that not only is using $ to start and end inline math mode not wrong, it has certain advantages over \( and \). Consider the following MWE:

\documentclass{article}
\begin{document}
\tableofcontents
\section{\(1+1=2\)}
\end{document} 

If you compile it twice, you'll get the following, not particularly enlightening error message:

>  LaTeX Error: Bad math environment delimiter.  

The reason one gets this error message is that \( and \) are not "robust" (in the TeX/LaTeX sense of the word), which trips up the LaTeX code that generates the table of contents. To avoid getting this error message, you could type

\section{\protect\(1+1=2\protect\)}

but I dare-say that you'll soon decide it's preferable to write

\section{$1+1=2$}

Addendum: It may be instructive to examine how \( and \) are defined in the LaTeX kernel (see, e.g, the file latex.ltx):

\def\({\relax\ifmmode\@badmath\else$\fi}
\def\){\relax\ifmmode\ifinner$\else\@badmath\fi\else \@badmath\fi}

where \@badmath is defined as

\gdef\@badmath{\@latex@error{Bad math environment delimiter}\@eha}

Thus, \( and \) both eventually invoke $, with some error checking thrown in: \( throws an error if it's encountered while already in math mode, and \) generates an error if it's encountered while not in so-called "inner" math mode.

In practice, unless the macros \( and \) are made robust in the LaTeX sense of the word (e.g., by loading the package fixltx2e), using them may lead to other error messages -- see the example above -- that are just as confusing as those that tend to arise when $ is used incorrectly.

  • 12
    \( is also fixed if you \usepackage{fixltx2e} (which is a good idea in any case) – David Carlisle Dec 3 '13 at 21:57
  • 1
    @mrc see tex.stackexchange.com/questions/510/… – clemens Dec 3 '13 at 22:16
  • 1
    @mrc - See the answers to the posting Are ( and ) preferable to dollar signs for math mode?. Joseph Wright suggests that if it's necessary to hook into some external code, using \( and \) is preferable to $. On the other hand, other comments note that use of $ makes the source code more easily legible to humans. – Mico Dec 3 '13 at 22:21
  • 1
    No $ is correct input, the intended advantage of \( is you get better error diagnostics if they are mis-matched, but having them fragile rather spoils that as it introduces more errors than it improves:-) – David Carlisle Dec 3 '13 at 22:30
  • 2
    @Manuel yes. KOMA-Script does not make any changes to \( and \) nor does it load fixltx2e. (It does other things from fixltx2e like defining \textsubscript, though) – clemens Dec 3 '13 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.