TransWikia.com

Are ( and ) preferable to dollar signs for math mode?

TeX - LaTeX Asked by Mark Meckes on June 13, 2021

Along the lines of Why is [ … ] preferable to $$ … $$?, what reasons are there (if any) to favor ( ... ) over $ ... $?

9 Answers

( ... ) is LaTeX syntax. $ ... $ is TeX syntax.

plainTeX only allows $. In LaTeX you can use both, but ( ... ) will give less obscure error messages when there is a mistake inside it.

Both are shortcuts to start inline math environments.

Correct answer by Dima on June 13, 2021

There are some good 'meta' reasons for using ( ... ) in some circumstances. Environments such as Fancy Verbatim and alltt will allow ( and ) to act as math mode, but $ will be interpreted as a literal.

Another convenience is that some editors have an easier time with pair matching ( ... ) than $.

Answered by MattoxBeckman on June 13, 2021

Anyone who's ever tried writing a simple perl (or whatever) script to quickly parse a LaTeX document and do something in maths mode but not in text mode will agree that ( .. ) is the only way to go! Trying to get the pattern matching right against $a^2 + b^2$$c^2 + d^2$ is a nightmare.

(Before anyone asks, yes I have, and it's called mathgrep. The pain and agony of writing that convinced me more than anything else of the value of making everything a configurable macro so it can be easily changed later.)


Another reason: looking at the code, ( and ) actually checks that TeX is in the right mode before starting. So hello (world (can) we) have some tea? will produce an error, whereas hello $world $can$ we$ have some tea? will not.

Answered by Andrew Stacey on June 13, 2021

Configuring ( and ) for inline math tags in MathJax is beneficial as you don't need to escape $ in your HTML contents.

Note: MathJax is a javascript library to render LaTeX commands on the browser.

Answered by xport on June 13, 2021

There is actually a very good reason to prefer ( ... ) over $ ... $, but it's relevant only in one specific context I can think of: If you have a macro that applies an argument like this $#1$, supplying an empty argument when using the macro (which might make, depending on the command, perfect sense) would lead to an obscure error message, whereas (#1) will not.

An update (thanks to a comment by @BrunoLeFloch): As the above problem can be circumvented by writing $ #1$ instead, a better way of thinking about this is the following: It is better to teach people ( ... ) than $ ... $, though technically the disadvantages of using the latter are minor and can be circumvented with enough knowledge. Teaching people the former means that they need to pay attention to at least one fewer trap caused by the fact that TeX practically treats both $ and $$ as tokens.

Answered by Lover of Structure on June 13, 2021

Interestingly, nobody mentioned that the mathtools package has a very nice feature of adding italic correction in some places at the boundary between text and math mode (see its documentation for details, but aesthetically this really matters!), and this feature works only with (...).

enter image description here

The first line is with correction, the second without.

Answered by mbork on June 13, 2021

For users of the package soul:

The soul manual states that its macros* accept math mode within their arguments, but only if $...$ is used; i.e. (...) interestingly doesn't work there.

*The manual's example is so{foo$x^3$bar}, which uses the spacing-out macro so, and I've tested it with the strikeout macro st.

The manual's example

Answered by Lover of Structure on June 13, 2021

It is instructive to examine the exact definitions of ( and ) that are provided by the LaTeX kernel (contained in the file latex.ltx), version early 2016:

DeclareRobustCommand({%
  relaxifmmode@badmathelse$fi}%
DeclareRobustCommand){%
  relaxifmmodeifinner$else@badmathfielse@badmathfi}%

The main thing to note is that ( and ) act as "wrappers" around the "$" token:

  • ( checks first if it occurs in ("inner" or "outer") math mode. If that's the case, it issues an error message, viz., "LaTeX Error: Bad math environment delimiter", as it can't be correct to initiate inline math mode if TeX is already in math mode. If it's not the case, i.e. if ( is encountered while TeX is not in math mode, a single $ token is inserted and TeX's inline math mode is thereby entered.

  • Likewise, ) checks first if it occurs in "inner" math mode. If that's not the case, i.e., if TeX is either in "outer" math mode or not in math mode at all, a "LaTeX Error: Bad math environment delimiter" error message is generated. If, on the other hand, TeX is in inner math mode, a single $ token is generated and inline math mode is thereby terminated.

Moreover, observe that both ( and ) are declared to be "robust". This means, inter alia, that both macros can be used inside "moving" arguments, e.g., inside the arguments of sectioning commands and captions. (Aside: Robustness of these two macros was not the default in versions of the LaTeX kernel prior to 2015, necessitating the loading of the fixltx2e package -- which is something, I think is fair to say, few people ever did...)

Do these observations imply that using ( ... ) is preferable to typing $ ... $? Not necessarily! First, the LaTeX error message one gets from typing (say) [a) -- "! LaTeX Error: Bad math environment delimiter." -- is not necessarily more informative or helpful than the TeX error message that results from typing [a$ -- "! Display math should end with $$." Second, if your inline math expressions contain a lot of opening and closing round parentheses, i.e., ( and ), the presence of ( and ) can be quite distracting if one needs to debug an inline equation and locate where an opening or closing parenthesis may be missing.

Separately, depending on the exact layout of your keyboard, typing ( and ) may also entail (marginally) more effort than typing $ does. Of course, the input effort will also depend importantly on the editor that's being used.

Answered by Mico on June 13, 2021

While no one has mentioned it, it's best to use ( ... ), that is because ChkTeX for automated linting recommends using it over $ ... $, which is a good indicator probably.

Answered by Doesbaddel on June 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP